top of page
Writer's pictureSiddhesh Kadam

Automating AWS EC2 Instance Provisioning with Ansible


Automating AWS EC2 Instance Provisioning

In today's rapidly evolving IT landscape, automation is key to efficiently managing infrastructure and accelerating deployment processes. Ansible, a popular configuration management tool, provides a seamless way to automate tasks across multiple systems. In this blog post, we'll explore how to leverage Ansible to automate the provisioning of Amazon EC2 instances, demonstrating step-by-step instructions along with practical examples.


Setting Up AWS CLI and Ansible Galaxy:

Before diving into Ansible playbooks, it's essential to configure AWS CLI credentials and install the necessary Ansible collection. Let's start with the setup:


[root@siddhesh ~]# aws configure
AWS Access Key ID [****************B839]:
AWS Secret Access Key [****************D9er]:
Default region name [ap-south-1]:
Default output format [None]:
[root@siddhesh ~]# ansible-galaxy collection install amazon.aws
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading https://galaxy.ansible.com/api/v3/plugin/ansible/content/published/collections/artifacts/amazon-aws-7.3.0.tar.gz to /root/.ansible/tmp/ansible-local-19113nwa7nrht/tmp4ybw4dao/amazon-aws-7.3.0-vz1xj43g
Installing 'amazon.aws:7.3.0' to '/root/.ansible/collections/ansible_collections/amazon/aws'
amazon.aws:7.3.0 was installed successfully
[root@siddhesh ~]#

With the AWS CLI configured and the Ansible AWS collection installed, we're now ready to automate EC2 instance provisioning.


Creating an EC2 Instance with Ansible:

Ansible provides modules specifically designed for AWS resource management. Let's create a playbook to launch an EC2 instance:

[root@siddhesh ~]# cat create_ec2_instance.yml
---
- hosts: localhost
  connection: local
  tasks:
    - name: Launch EC2 instance
      amazon.aws.ec2_instance:
        name: "my-ansible-instance"
        image_id: "ami-0763cf792771fe1bd"
        count: 1
        region: ap-south-1
        instance_type: t2.micro
        tags:
          Name: "My Ansible Builddevops Instance"
[root@siddhesh ~]#

In this playbook:

We specify the target host as localhost since we're running the playbook locally.

The amazon.aws.ec2_instance module is used to provision an EC2 instance.

Key parameters such as name, image_id, count, region, instance_type, and tags are provided to configure the instance.

We define tags for the instance to organize resources efficiently.


Running the Playbook:

Now, let's execute the playbook and observe the instance creation process:

[root@siddhesh ~]# ansible-playbook create_ec2_instance.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *******************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************
ok: [localhost]
TASK [Launch EC2 instance] *********************************************************************************************************************************
changed: [localhost]
PLAY RECAP *************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[root@siddhesh ~]#

Let's break down the output:

[root@siddhesh ~]# - This is the command prompt, indicating that the user is logged in as the root user on a system named "siddhesh".

ansible-playbook create_ec2_instance.yml - This is the command to execute the Ansible playbook named create_ec2_instance.yml. Ansible is a tool for automation of tasks on multiple servers. Playbooks are files written in YAML format that contain a set of tasks to be executed.

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' - This is a warning message from Ansible indicating that no hosts were specified to run the playbook against. As a result, Ansible is only using the local machine (localhost) to execute the tasks defined in the playbook.

PLAY [localhost] - This indicates that the playbook is targeting the localhost, i.e., the local machine.

TASK [Gathering Facts] - This is a task in the playbook to gather system facts from the target host. It's a common first task in many Ansible playbooks as it collects information about the target system.

ok: [localhost] - This indicates that the task "Gathering Facts" completed successfully on the localhost.

TASK [Launch EC2 instance] - This is another task in the playbook, presumably responsible for launching an EC2 instance.

changed: [localhost] - This indicates that the task "Launch EC2 instance" caused a change on the localhost. In this case, it likely means that the EC2 instance was successfully launched.

PLAY RECAP - This is a summary of the playbook execution.

localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 - This line breaks down the summary:

ok=2: Two tasks were executed successfully.

changed=1: One task caused a change (likely the "Launch EC2 instance" task).

unreachable=0: No hosts were unreachable.

failed=0: No tasks failed.

skipped=0: No tasks were skipped.

rescued=0: No tasks were rescued.

ignored=0: No tasks were ignored.


AWS console would look something like this :


Automating AWS EC2 Instance Provisioning
AWS Console

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page