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 0.0.0.0/0:
- hosts: localhost
- name: create a security group in us-east-2
description: an example ec2 group
- proto: tcp
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
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
– device_name: /dev/xvda
volume_type: gp2 #insert the volume code here
volume_size: 8 #size is in GB
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
size: 20 # determines the storage size in GB