Launching AWS EC2 Instances with Ansible: A Step-by-Step Guide

mdshamsfiroz

Ansible is a powerful automation tool that can simplify the process of provisioning and managing AWS EC2 instances. In this guide, we’ll walk through the steps to launch an EC2 instance using Ansible.

Prerequisites

Before we begin, make sure you have:

  1. Ansible installed on your local machine
  2. AWS account with appropriate permissions
  3. AWS CLI configured with your credentials

Step 1: Set Up Your Ansible Environment

First, create a directory for your Ansible project:

mkdir ansible-ec2-launch
cd ansible-ec2-launch

Create an ansible.cfg file:

[defaults]
inventory = ./inventory
host_key_checking = False

Create an inventory file named inventory

[local]
localhost ansible_connection=local

Step 2: Create the Ansible Playbook

Create a file named launch_ec2.yml with the following content:

---
- name: Launch EC2 Instance
hosts: localhost
connection: local
gather_facts: false
vars:
region: us-west-2
instance_type: t2.micro
ami_id: ami-0c55b159cbfafe1f0 # Amazon Linux 2 AMI ID
key_name: your-key-pair-name
tasks:
- name: Provision EC2 instance
amazon.aws.ec2_instance:
instance_type: "{{ instance_type }}"
image_id: "{{ ami_id }}"
region: "{{ region }}"
key_name: "{{ key_name }}"
wait: yes
network:
assign_public_ip: yes
tags:
Name: Ansible-Managed-Instance
register: ec2
- name: Print EC2 Instance Details
debug:
msg: "EC2 Instance {{ ec2.instances[0].instance_id }} has been launched with public IP {{ ec2.instances[0].public_ip_address }}"

Step 3: Run the Playbook

Execute the playbook with the following command:

ansible-playbook launch_ec2.yml

Understanding the Playbook

Let’s break down the key components of our playbook:

  1. Hosts and Connection: We’re running this playbook on the localhost.
  2. Variables: We define variables for the region, instance type, AMI ID, and key pair name.
  3. EC2 Instance Task: We use the amazon.aws.ec2_instance module to launch the EC2 instance.
  4. Debug Task: We print out the instance ID and public IP address of the launched instance.

Customizing Your EC2 Instance

You can easily customize your EC2 instance by modifying the variables in the playbook:

  • Change the region to launch in a different AWS region.
  • Adjust the instance_type for different performance levels.
  • Update the ami_id to use a different Amazon Machine Image.
  • Modify the tags to better organize your EC2 instances.

Best Practices

  1. Use Variables: Store sensitive information like AMI IDs and key names in separate variable files or use Ansible Vault for enhanced security.
  2. Idempotency: The ec2_instance module is idempotent, meaning you can run the playbook multiple times without creating duplicate instances.
  3. Error Handling: Add error handling tasks to manage failures gracefully.
  4. Cleanup: Consider creating a separate playbook for terminating instances to manage your AWS resources effectively.

Conclusion

Using Ansible to launch EC2 instances provides a repeatable, version-controlled way to manage your AWS infrastructure. This approach allows for easy scaling, consistent configurations, and integration into larger automation workflows.
By following this guide, you’ve taken the first step towards infrastructure as code with Ansible and AWS.
Experiment with different EC2 configurations and explore other AWS modules to expand your automation capabilities.

Remember to always follow AWS best practices and monitor your resource usage to optimize costs and performance.
So, whether you’re a tech enthusiast, a professional, or just someone who wants to learn more, I invite you to follow me on this journey. Subscribe to my blog and follow me on social media to stay in the loop and never miss a post.

Together, let’s explore the exciting world of technology and all it offers. I can’t wait to connect with you!”

Connect me on Social Media: https://linktr.ee/mdshamsfiroz

Happy coding! Happy learning!

Sign up to discover human stories that deepen your understanding of the world.

mdshamsfiroz
mdshamsfiroz

Written by mdshamsfiroz

Trying to learn tool by putting heart inside to make something

No responses yet

Write a response