Launch AWS ec2 instance using Ansible:

Anu Chundawat
2 min readMay 19, 2021

Ansible is an automation tool that is written in python. it is an open-source software used for provisioning, configuration management, and application deployment.

First we requires aws credentials to connect to aws to launch ec2 instance.

Go to IAM service in aws and click add user:

Give the programmatic access and then click next and attach a policy which give access to the user to access aws services.

Now click next and create the user then copy the credential which we will use in next steps.

Go to Linux Vm:

To setup launch EC2 instance using Ansible-playbook first we have to install ansible:

We will install ansible by pip3 so we have to first install python:

yum install python3

Now install ansible using pip3

pip3 install ansible

Now export the credentials which you got after creating user in aws:

export AWS_ACCESS_KEY= <access_key>
export AWS_SECRET_KEY=<secret_access_key>

Now create a ansible playbook let ec2.yml which will run on the localhost

- hosts: 127.0.0.1
tasks:
- name: "Launching EC2-Instance on aws"
ec2:
image: <ami-id>
instance_type: "t2.micro"
region: ap-south-1
count: 2
vpc_subnet_id: <subnet_id>
assign_public_ip: yes
group_id: <security_grou>
instance_tags:
Name: Webserver_OS
os: webserver
key_name: <key>

Now run the ec2.yml playbook it will launch an ec2 instance on the aws cloud.

ansible-playbook ec2.yml

Thank you for reading!!!!

--

--