Setup AWS resources using Ansible Modules

Reading Time: 4 minutes

Hi Readers, In this blog we will learn that how we can create AWS resources using Ansible Modules. But before moving to core of the blog we will see few concepts related to Ansible, it’s installation and connecting our ansible with aws and few more things.

What is Ansible and Why do we need this?

Keeping it very brief, Ansible is an IT automation tools. It helps in,

  • Provisioning as create ec2 instance, vpc, s3 bucket etc.
  • Configuration as simple data description for the infrastructure for understanding of human as well as machine. Example configure all servers to run specific app version.
  • Deployment as deploy application on webserver like nginx, apache etc.
  • Manage as maintaining all resources from control node.

Ansible Installation and Prerequisite

To install Ansible you will need Python installed in your system. Once you have Python installed, run simple command from terminal, or see this.

sudo apt install ansible

Connecting Ansible with AWS using aws-cli

To connect Ansible with AWS you will need to generate Access Key ID and Secret Access Key from AWS console. Once you have these Id’s, install aws-cli using this simple command,

sudo apt-get install awscli

After this, run aws configure and enter your Access Key ID and Secret Access Key as prompted,

Now Let’s see how we can create AWS resources using Ansible Modules

What are Ansible Modules?

Modules in simple words are like generic template which enables the code re-usability in our code base. Instead of writing separate script for similar kind of task we can create our own modules and use it again and again with few customisation as per our use-case. Also we can use pre-existing modules as well.

Installing Module in your Local System

You can get several modules from ansible-galaxy. Below command will install all available amazon.aws modules in your local system which are ready to use.

ansible-galaxy collection install amazon.aws

Know where the Modules are being stored

To know where the above modules are being installed, run below command with any content of this module be it ec2_ami , ec2_vpc_net, aws_s3 etc. See other contents here.

Use Case

Consider a scenario, we want to write a idempotent(we will see this term in script) script that creates n number of ec2 instances, in a specific region and waits until the instance is in running state. And in the end we want to delete that instances as well.

create_ec2.yml
- name: Create a ec2 instance
  hosts: localhost

  tasks:
        - name: create instance using module
          ec2:
              id: ec2usingModule
              image: ami-083bbff3417d484ab
              instance_type: t2.micro
              region: ap-south-1
              count: 2
              state: present
              instance_tags:
                          web: server
              wait: yes 

From terminal run the below command,

ansible-playbook create_ec2.yml

The output would look like this from terminal,

AWS console would look something like this,

In above create_ec2.yml

  • id : A unique ID attached with the instance which enables idempotent behaviour, which means if by mistake we re-run this playbook, it won’t again create new instance but instead give task status as OK.
  • image: Ubuntu cloud images are uploaded and registered on Amazon EC2 cloud, known as AMI. Each AMI is a machine template which can be used to initialize new servers. Each AMI has it’s own unique ID. Locate it from here.
  • instance_type: It determines the hardware of the host computer used for your instance. They vary on the basis of storage, compute and memory capabilities.
  • region: It specifies in which region all around the globe you want to spin up your resource.
  • count: It specifies the number of ec2 instance you want to create in single go.
  • state: It specifies the status of instance, weather it is running or stopped.
  • instance_tags: This enables to attach some metadata to our instance to categorise it on basis of what is does and what it is for.
  • wait: This specifies to wait at the task until the status of instances are Running.

Now we want to change the status of ec2 instance to Stopped, so for that create a new playbook as below,

stop_ec2_instance.yml
- name: Stop ec2 instance
  hosts: localhost

  tasks:
        - name: Stop instance using module
          ec2:
              state: stopped
              region: ap-south-1
              instance_tags:
                          web: server
              wait: yes

From terminal run below command,

ansible-playbook stop_ec2_instance.yml

The output for this would look like this from terminal,

Now, AWS Console would look something like this,

That’s all for this blog, I hope you got a quick overview about how to create and stop ec2 instances on AWS using Ansible Modules with this blog. For the source code you check my github. If you still have any doubt, feel free to contact me nitin.mishra@knoldus.com.

Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and if you feel, give me suggestions on scope of improvements.

References

Written by 

Nitin Mishra is a Software Consultant at Knoldus Software LLP. He has done MCA from GGSIPU and completed Bachelor of Science in Computer Science from Delhi University. He is a tech enthusiast with good knowledge of Java. He is majorly focused in DevOps practice. On personal front he loves to travel mountains and writes poetry.

2 thoughts on “Setup AWS resources using Ansible Modules4 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading