Ansible for launching AWS EC2 instances

Using Ansible, one can launch AWS EC2, security group, and RDS.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform AWS related queries.

In this context, we shall look into how to use Ansible to launch AWS EC2 instances, security group, and RDS.

Benefits of using Ansible?

Generally, system administrators managed servers by hand. They would install software, change configurations, and administer services on individual servers.

As data centers grew, and hosted applications became more complex, administrators couldn’t handle manual systems management as fast as the applications they were enabling. That’s why server provisioning and configuration management tools came into the picture.

As we see above the tedious routine of administering a server where we need to keep updating, pushing changes, copying files on them, and so on. Moreover, these tasks make things very complicated and time-consuming.

But there is a solution for the above problem and that is Ansible.

Ansible is an open-source automation tool used for tasks such as configuration management, application deployment, and provisioning.

More information about about AWS instances, SG and RDS database ?

AWS instances are virtual environments that are isolated from the underlying base OS.

A security group acts as a virtual firewall for the EC2 instances to control incoming and outgoing traffic.

Amazon Relational Database Service (Amazon RDS) helps to easily set up, operate, and scale a relational database in the cloud.

Cloud formation restricts to only AWS services. However, Ansible provides more than 20 libraries which can do much than AWS resources.

Now let's see how to launch AWS EC2 instances, security group, and RDS. Before that, we first need to create an IAM user. Also, we would need the AWS Access Key ID and Secret Access Key.

How to create a Security Group in Ansible ?

The ec2_group module helps in managing security groups in AWS. In order to create a security group we first need to determine the region where we are going to host our services. The region code needs to be passed to the region parameter. We can find a list of region codes on the region page.

In the below example we are creating a security group in "us-east-2" allowing the port 80 with cidr_ip

- hosts: localhost
connection: local
gather_facts: false
- name: create a security group in us-east-2
name: dmz
description: an example ec2 group
region: us-east-2
aws_access_key: "AKIAIWJUADQPQB16LCFI"
aws_secret_key: "NCMx885+nNU51sKuprQeZeVsU9arRZc7hAX7Itez"
- proto: tcp
from_port: 80
to_port: 80
register: security_group

We store the output in the variable "security_group".

Also, we can access the following data using the output variable:

group_id: Security Group ID (Will use the group_id to assign the instance to it)
vpc_id: The unique ID of the VPC to which the security group belongs to.
ip_permissions: the inbound rules assigned to this security group.
description: of the security group.
tags: associated tags.
group_name: name of the security group.
ip_permissions_egress: outbound rules.
owner_id: AWS account ID

How to create an AWS EC2 Instance using Ansible ?

To create EC2 instances, we will make use of the ec2 module. The EC2 module allows us to start, stop, terminate, and stop the instances.

Here, we will create a free tier Linux EC2 instance in the us-east-2 region and assign it to the security group that we created earlier:

- name: create ec2 instance
aws_access_key: "AKIAIWJUADQPQB16LCFI"
aws_secret_key: "NCMx885+nNU51sKuprQeZeVsU9arRZc7hAX7Itez"
image: ami-caaf84af
wait: yes
instance_type: t2.micro
group_id: security_group.group_id
region: us-east-2
Name: apacheserver
exact_count: 1
register: ec2


How to launch an AWS EC2 Instance with SSD Volume ?

To select the volume type, we need to use the "volume" option:

– name: create an EC2 instance with SSD volume type
key_name: mykey
group: webserver
instance_type: c3.medium
image: ami-123456
wait: yes
wait_timeout: 500
– device_name: /dev/xvda
volume_type: gp2 #insert the volume code here
volume_size: 8 #size is in GB
group_id: security_group.group_id
Name: apacheserver
exact_count: 1

How to create a Free tier RDS Database instance in Ansible ?

In this example, we will launch an RDS instance in us-east-2 with a storage capacity of 20 GB:

– name: create RDS instance
command: create
region: us-east-2
instance_name: infinityppdatabase
db_engine: MySQL
size: 20 # determines the storage size in GB
instance_type: db.t2.micro
username: mysql_admin
password: 1nsecure
Environment: testing
Application: cms

[Still, not able to launch AWS EC2 instances, security group, and RDS – We are here to help you. ]


This article will guide you on how to use #Ansible to launch AWS EC2 instances, security group, and RDS.

First, you're limiting the scope of the playbook to the local #hosts #group. It contains localhost and this is the way Ansible will work with #EC2 instances. Behind the scenes, Ansible connects to Python boto on the local machine and use to establish connection with the AWS #API and issue the necessary #commands.

Then we specify the name of the security group that Ansible will create for us. A security group is like a virtual firewall that must be created for your EC2 instances. If you already have one created, you can associate it with the new EC2 instance. In our case, we’ll be creating a new one from scratch.

For Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on

Also for Tech related tips, Visit or General Technical tips on