Brings up an AWS instance using Ansible.

Abhishek Nanarkar
2 min readApr 13, 2020

I am using here own AWS account, I already have user-created into IAM section. I have activated ec2_access_key & ec2_secret_key.

I already have installed python in my machine. I installed python-pip and boto dependencies which are needed for Ansible AWS connection.

sudo apt install python-pip

pip install boto boto3

Created SSH key to connect ec2 instance after provisioning.

ssh-keygen -t rsa -b 4096 -f ~/.ssh/abhishek_aws

Created Ansible Directory Structure to place playbook and group vars.

Linux terminal

The ec2keys.yml is storing ec2_access_key & ec2_secret_key in encrypted way using Ansible-vault.

Ec2_creation.yml is an ec2 instance provision scripts. Please have a look at the attached scripts in the GitHub link: https://github.com/abhisheknanarkar24/AWS_ElasticSearch_Ansible

I have used Ansible tags into scripts which can be used to skip the particular from the playbook.

Ec2 Provision playbook brief explanation:

In the playbook, used connection locally to run the script on Ansible server.

In the vars section, I have passed values for ec2 provision we can use separate INI files to pass a variable from outside playbook also.

In the next section, I have used different tasks using the Ansible AWS module.

ec2_instance_info

ec2_key

ec2_group

ec2

Above module are used to gather information about aws resources and provisioning ec2 instance. Ec2 module is used to create instance use all passed parameter.

Command to run playbook:

ansible-playbook ec2_creation.yml --ask-vault-pass

ansible-playbook ec2_creation.yml --ask-vault-pass --tags create_ec2

AWS console

Connect ec2 instance :

ssh -i ~/.ssh/my_aws <public dns name>

--

--

Abhishek Nanarkar

DevOps Engineer with experience on AWS cloud services, CI-CD pipelines, Docker, SCM, Linux, and Terraform