Configuring Ansible For AWS Management
Tags: ansible, aws, devops, automation
Published: 2023-10-08 14:30:32

---
title: Configuring Ansible For AWS Management
published: true
date: 2023-10-08 13:18:25 UTC
tags: #ansible #aws #devops #automation
canonical_url: https://anujdube.com/configuring-ansible-for-aws-management
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zy31y01iihifh0v8xznw.png
---

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696764712986/fc5a6390-2090-4120-99b7-1f7937ff17f7.png)

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696764894472/12f80996-b91c-4275-b31a-fa9692765fbd.png)

On this page if you move down You will see an option called "Access Keys".

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696765106386/6e5a4416-7c50-448c-94cf-9db6dd2783da.png)

Click on it and select first option "Command Line Interface"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696765155260/a9e08823-4f49-467a-aaea-047d25f4009b.png)

Then Just Click next and create your access key.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696765235778/63958e6c-f4da-4bd2-914b-c4bbcfd78813.png)

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696765744852/b7ac7299-a6e4-4fc5-a13b-e9ff3ecb4698.png)

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696767771383/0f7d7ab8-3442-4523-8c2e-bc6f73e325ca.png)

to create another type of instance you can go to "AMI catalog" and you can get image\_id from there.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696768226505/80826478-19e2-44b8-8f32-61ab7cc1b917.png)

vpc\_subnet\_id: In Networking Tab

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696767889194/fe51361c-8949-4309-99c2-993a6ff4438c.png)

security\_group: In the Security tab

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696767990066/5fc3fcd8-5a7b-494a-a75b-2fc4e06cab60.png)

instance\_type: In Details Tab

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696768514824/b82dae23-280f-453b-80b9-3e082a634fa2.png)

To Get other instance types go to the "Instance Types" tab

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696768458620/09947019-7273-4c8d-ae28-41186085f1d4.png)

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696768954570/8912b1c5-cc1a-4bff-9753-0d379372f648.png)

Now Go To AWS EC2 Console You will see a new instance with the name "Created Using Ansible" will be created.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696769033521/6b104b9b-d9eb-4e77-b9ee-848d78b46b28.png)

### 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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696769346426/f634fd45-ce24-48c7-b654-304bdc83a052.png)

### 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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696769770374/cb002bbe-239f-4a75-b374-fe3a900a54fc.png)

* * *

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

[AWS Ansible Documentation](https://docs.ansible.com/ansible/latest/collections/amazon/aws/index.html)

Happy automating! 🤖