AWS EC2 Instance Creation Using Ansible Playbook Automation

Amazon Web Services is introduced term called “Infrastructure as a Code” where you no need to provisioning and maintenance manually everything is going to be peace of code. In this case Ansible AWS EC2 Instance creation using ansible playbook which provides automated provisioning of EC2 instances.

No need of manual login to AWS EC2 console and clicking and creating instances, use feature to provision/create ansible is the power full tool.

AWS EC2 Instance Creation Using Ansible

Preparing Environment before invoking playbook

I am using Centos 7.4 Operating System version as Ansible main node. To communicate with AWS we are going to use boto / boto3 aws.

# yum install python python-setuptools* ansible git curl wget
# curl -O
# python 

$ python --version
Python 2.7.5

$ pip --version
pip 18.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)

# pip install boto 
# pip install boto3

Use this Amazon lab practice guide to create IAM user with programmatic access and user should have access to create/launch EC2 instance

Create a boto file with access key and access secret id to authenticate to aws

# vi ~/.boto

aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

Write Ansible Playbook to launch aws ec2 instance

  1. Define Variables
  2. Tasks
    1. Create New Security Group
    2. Launch EC2 Instance
    3. Add Tags for identification
# git clone cd ansible-playbooks

Look for spinawsec2.yml file

  - name: Provision an EC2 Instance
    hosts: localhost
    connection: local
    gather_facts: False
    tags: provisioning

      instance_type: t2.micro
      security_group: webservers
      image: ami-0080e4c5bc078760e
      region: us-east-1
      keypair: sshkeypair
      count: 1


      - name: Create New security group with below given name
          module: ec2_group
          name: "{{ security_group }}"
          description: Security Group for Newly Created EC2 Instance
          region: "{{ region }}"
            - proto: tcp
              from_port: 22
              to_port: 22
            - proto: tcp
              from_port: 80
              to_port: 80
            - proto: all

      - name: Launch the new t2 micro EC2 Instance
        local_action: ec2
                      group={{ security_group }}
                      instance_type={{ instance_type}}
                      image={{ image }}
                      region={{ region }}
                      keypair={{ keypair }}
        register: ec2

      - name: Wait for EC2 Instance to Spin-up and ready for SSH access
        local_action: wait_for
                      host={{ item.public_ip }}
        with_items: "{{ ec2.instances }}"

      - name: Adding Tags to Identify
        local_action: ec2_tag resource={{ }} region={{ region }} state=present
        with_items: "{{ ec2.instances }}"
            Name: Web Server
            Owner: Ravi Kumar
            PurPose: Testing EC2 Instance From Ansible

Pro’s and Con’s

Using this ansible playbook aws ec2 instance creation can be done, however every time when you want to launch remember to change below variable values

  • AMI ID
  • Region
  • Instance Type
  • Security Group Name
  • SSH Key Pair Name
  • Count of instances to be created

To make play book more flexible and interactive delete vars section and pass the same variables on playbook execution

      instance_type: t2.micro
      security_group: webservers
      image: ami-0080e4c5bc078760e
      region: us-east-1
      keypair: NVirginia
      count: 1

Example of passing variables while running ansible playbook

ansible-playbook spinawsec2.yml -e instance_type=t2.micro -e security_group=WebServers -e image=ami-0080e4c5bc078760e -e region=us-east-1 -e keypair=NVirginia -e count=1

That’s about aws ec2 instance creation using ansible playbook.

Related Articles

First Configuration After Ansible Installation

Installing Python virtual Ansible Environment

How To Write Ansible Playbook

Configure SNMP Using Ansible Playbook

Copying SSH Keys Using Ansible

Install Ansible tower

Ansible-Doc Accessing Documentation

Lab Setup Using Docker Containers

Thanks for your wonderful Support and Encouragement

Ravi Kumar Ankam

My Name is ARK. Expert in grasping any new technology, Interested in Sharing the knowledge. Learn more & Earn More

1 Response

  1. This guy says:

    I’m running this playbook from within an EC2 instance, which keypair should I use exactly?

Leave a Reply

Your email address will not be published. Required fields are marked *