Table of contents
Amazon Web Services (AWS) offers a scalable environment for deploying applications, but as the infrastructure grows, so does the complexity of managing it. Organizations need an easy way to manage this infrastructure. they are using different tools to keep the infrastructure as code. Ansible is such an open-source tool. it can help you to create and manage your infrastructure in different cloud environments.
In this article, we will look into how to create and manage AWS ec2 instances using Ansible.
Creating Ansible Control Node
Step 1: Create an Ubuntu EC2 Instance on AWS
After the Creation of the instance SSH into it. we will be using this as a control node to create and manage other AWS resources
Step 2: Install Ansible
Update Repository and Upgrade Packages
sudo apt update
sudo apt upgrade
Sometimes You will be asked to restart
sudo reboot
Install Ansible
sudo pip install ansible
Step 3: Install amazon.aws Ansible Collection
Install amazon.aws ansible collection
ansible-galaxy collection install amazon.aws
amazon.aws Ansible Collection needs boto3 and botocore packages to connect to AWS for creating and managing infrastructure
First Install PIP
sudo apt install python3-pip
Install boto3 and botocore packages
pip install boto3 botocore
Step 4: Install and Configure AWS CLI
sudo apt install awscli
Configure AWS credentials using the AWS CLI
aws configure
You will be prompted to enter some details below
To Get These Details First go to AWS.
On the Right side, Click on your Profile. In Drop Down Menu You will see an option called "security credentials". Click On it
On this page if you move down You will see an option called "Access Keys".
Click on it and select first option "Command Line Interface"
Then Just Click next and create your access key.
Now You have "Access Key ID" and "Secret Access Key".
Please do not use keys from images They will not work I will be deleting this user and all his keys๐
To Get the Default Region name click on Drop Down on the left side of the profile. I have taken the region Mumbai so the name will be ap-south-1
After Entering all the details, We will check if we can connect to AWS or not
Run Below Command It will show information about your EC2 instances
if the below command fails then you must have made some mistake while entering configuration information please try again
aws ec2 describe-instances
Now Our Control Node is ready
Creating and Managing EC2 Instances
Example 1: Create a Single Ubuntu EC2 Instance with a Public IP Address
For This, we are going to use the module "ec2_instance"
Create a YAML File called create_ec2.yml
---
- name: Create Single Ubuntu EC2 Instance
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Create Ubuntu EC2 instace with public ip
amazon.aws.ec2_instance:
name: "Created Using Ansible"
key_name: "newUbuntu"
vpc_subnet_id: subnet-0bd155c67558454b1
instance_type: t2.micro
security_group: sg-076a5ea097f046d8e
network:
assign_public_ip: true
image_id: ami-0f5ee92e2d63afc18
tags:
Environment: Testing
register: ec2_node_info
- name: Display information
debug:
msg: "{{ec2_node_info}}"
"key_name": SSH Key you will be using to login into the created server, You can use the same key that we are using to log into the control node
You Can use the Same details as we are using in the control node
Image_Id: In Details Tab
to create another type of instance you can go to "AMI catalog" and you can get image_id from there.
vpc_subnet_id: In Networking Tab
security_group: In the Security tab
instance_type: In Details Tab
To Get other instance types go to the "Instance Types" tab
To Run the playbook use the below command
ansible-playbook create_ec2.yml
we are running it in localhost those options are mentioned in yml. so you can ignore warnings.
You will see output like the below as we are printing created instance details
Now Go To AWS EC2 Console You will see a new instance with the name "Created Using Ansible" will be created.
Example 2: Get a List of all running Instances
For This, We are going to "ec2_instance_info"
Create a YAML File called get_all_ec2.yml
---
- name: Get All Running EC2 instances
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Gather information about instances in states "running"
amazon.aws.ec2_instance_info:
filters:
instance-state-name: "running"
register: ec2_node_info
- name: Get Running Instances Count
debug:
msg: "Total running instances: {{ ec2_node_info.instances | length }}"
- name: Display information
debug:
msg: "{{ec2_node_info}}"
Run Using Command
ansible-playbook get_all_ec2.yml
You will see output like below giving all running ec2 instances details
Example 3: Terminate Every Instance From the Current AWS Region
This will terminate the control node also.
Create a File called terminate_all_ec2.yml
---
- name: Terminate Every Instance in Current AWS Region
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Terminate every running instance in a region. Use with EXTREME caution.
amazon.aws.ec2_instance:
state: absent
filters:
instance-state-name: running
Now Run this using below command
ansible-playbook terminate_all_ec2.yml
You will see That Your Control node is also terminated so you might be disconnected
You can Manage and control lots of other AWS Resources like S3, VPC, Lambda, RDS etc. using Ansible.
You can learn more about that using the below link
Happy automating! ๐ค