There are many advantages of infrastructure automation. We can implement any software project without worrying about the system configuration.
This project utilizes ansible for creating an ec2 instance dynamically and then creates a docker instance on the remote ec2.
You can simply clone the repository in your local machine and create an ec2 instance just by changing the parameters.
Prerequisites
python3
ansible 2.5
ubuntu 18.04 LTS
STEP 1: Add AWS login details in the ‘vault.yml’ .
AWS login credentials are required to create the ec2 instance. These can be stored in the ~/.aws directory or this can be added into the ansible vault using ‘ansible-vault’ command.
Execute below command and add aws_access_key_id and aws_secret_access_key in the file created by the command.
$ sudo ansible-vault create vars/vault.yml
[default]
aws_access_key = XXXXXXXXXXX
aws_secret_key = XXXXXXXXXXXXXX
STEP 2: Initialize AWS variables for the configuration of ec2 instance.
vars/vars.yml contains variables like AWS- region id, VPC id, subnet id which is essential for creating ec2. These should be changed according to the AWS login.
aws_region_id : eu-central-1
aws_vpc_id : vpc-07db38c12d26359ac
aws_sub_id : subnet-042581e3da666d445
aws_ami_id : ami-050a22b7e0cf85dd0
aws_instance_type : t2.micro
aws_sec_key : abhishek-vpc
IdFile : /home/abhishek/.aws/abhishek-vpc.pem
default_image : jenkins
public_ip : ''
instance_id : ''
terminate : true
public_ip and instance_id are variables that is initialized at the runtime.
The variable ‘terminate’ can be set to true or false, depending on the final state of the instance.
The variable default_image can be changed based on the required image.
This image is pulled from https://hub.docker.com/
STEP 3: Execute the ansible-playbook to create docker on ec2.
$ sudo ansible-playbook create_ec2.yml -i hosts.ini --ask-vault-pass
The command will ask the vault password which is created in the STEP1. It will create a docker on ec2 based on the configuration available in create_ce2.yml.

Acknowledgments