Friday, July 9, 2021

Ansible Playbook for provisioning a new EC2 instance in AWS | Create new EC2 instance in AWS cloud using Ansible Playbook

We will learn how to create Ansible Playbook for provisioning a new EC2 instance in AWS cloud. Please follow the below steps in the machine where you have installed Ansible.

Watch here for YouTube Video:

Make sure you create an IAM role with AmazonEC2FullAccess policy and attach the role to EC2 instance.

Steps to create EC2 instance using Ansible:

Login to EC2 instance using Git bash or ITerm/putty where you installed Ansible. Execute the below command:

Edit Ansible hosts or inventory file
sudo vi /etc/ansible/hosts 
Add the below two lines in the end of the file:

cd ~
mkdir playbooks  
cd playbooks

Create Ansible playbook
sudo vi create_ec2.yml 
(copy the below content in green color)
edit the create_jenkins_ec2.yml to make sure you update the key which is red marked below:

 - name:  provisioning EC2 instances using Ansible
   hosts: localhost
   connection: local
   gather_facts: False
   tags: provisioning

     keypair: MyEC2Key
     instance_type: t2.small
     image: ami-020db2c14939a8efb
     wait: yes
     group: webserver
     count: 1
     region: us-east-2
     security_group: my-jenkins-security-grp

     - name: Task # 1 - Create my security group
         module: ec2_group
         name: "{{ security_group }}"
         description: Security Group for webserver Servers
         region: "{{ region }}"
            - proto: tcp
              from_port: 22
              to_port: 22
            - proto: tcp
              from_port: 8080
              to_port: 8080
            - proto: tcp
              from_port: 80
              to_port: 80
            - proto: all
       register: basic_firewall
     - name: Task # 2 Launch the new EC2 Instance
       local_action:  ec2 
                      group={{ security_group }} 
                      instance_type={{ instance_type}} 
                      image={{ image }} 
                      region={{ region }} 
                      keypair={{ keypair }}
       register: ec2
     - name: Task # 3 Add Tagging to EC2 instance
       local_action: ec2_tag resource={{ }} region={{ region }} state=present
       with_items: "{{ ec2.instances }}"
           Name: MyTargetEc2Instance

now execute the ansible playbook by
sudo ansible-playbook create_ec2.yml

Fix the warnings by executing below command
pip install --upgrade requests==2.20.1

If everything is good, you should see the new instance created on AWS console. make sure you are able to connect to that instance.

That's it!! That is how you create a new EC2 instance using Ansible.


  1. An exception occurred during task execution. To see the full traceback, use -vvv. The error was: botocore.exceptions.NoCre
    dentialsError: Unable to locate credentials
    fatal: [local -> localhost]: FAILED! => {"boto3_version": "1.20.30", "botocore_version": "1.23.30", "changed": false, "msg
    ": "Error in describe_security_groups: Unable to locate credentials"}

    Getting this error while use yoyr above script

    1. Did you attach your IAM role to Ansible machine ?

  2. TASK [Task] *******************************************************************************************************************************************
    fatal: [localhost]: FAILED! => {"msg": "The module ec2\n was not found in configured module paths"}


How to check whether my user data passing to EC2 instance is working | AWS EC2 bootstrap script log

The cloud-init output log file ( /var/log/cloud-init-output.log ) captures console output so it is easy to debug your scripts following a la...