Ansible

Can Ansible Create EC2 Instances?

Chances are, if you are looking at this tutorial, you do not need an introduction to Ansible. However, it does not hurt to make a quick recap.

Ansible is the most popular and probably powerful configuration management tool. It is built to facilitate the management and configuration of remote hosts using a set of commands defined as tasks.

It works by using modules developed for specific tasks such as managing users, managing files, installing and removing software packages, and many more.

This guide will use the Ansible AWS module to provision and manage an EC2 instance. We will start with the basics of setting up an AWS account, installing Ansible, and finally cover how to work with the Ansible AWS module.

What is an EC2 Instance?

The first question to tackle is: What is an EC2? If you are new to cloud computing and AWS, this may sound new to you.

Amazon Elastic Compute Cloud, or Amazon EC2 for short, is a service provided by Amazon Web Services (AWS) to allow you to create and manage server instances on the AWS Cloud.

The ability to spin up remote servers configured with features such as CPU, memory, disk, networks, Operating System, and more by a few clicks is very beneficial. It removes the need to invest in hardware and eliminates worrying about wiring the devices up.

Amazon EC2 instances are one of the most fundamental building blocks of AWS cloud computing.

We will not dive deep into how it works or the pricing and configuration models for AWS. Check the AWS site to learn more.

Environment Setup

The first step is to set up our environments. We will need an AWS account, Ansible, and Python for this guide.

We recommend using the latest version of both Ansible and Python.

Install Ansible

This tutorial will show the installation and setup of Ansible on a Debian-based system. Check our other tutorials to learn how to install it on other distributions.

Open the terminal and edit your software repositories.

sudo vim /etc/apt/sources.list

Add the following entry to the file:

deb http://ppa.launchpad.net/ansible/ansible/ubuntu focal main

Next, run the commands as shown below:

sudo apt-get install gnupg
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
sudo apt-get update
sudo apt-get install ansible

Once you have Ansible installed, we can set and configure Python.

Install Python3, Python3-Pip

To install Python3 and pip on Debian 11, run the commands as shown:

sudo apt-get update
sudo apt-get install python3.9 python3-pip -y

The above commands will install Python version 3.9 and the pip3 on your system.

Install BOTO

The next step is to install BOTO. BOTO is a powerful python package that provides an interface to interact with the AWS services using Python.

To install the latest version of the boto package using pip as shown in the command below:

sudo pip3 install boto3

Once all the tools and packages have been installed successfully, we can create Ansible playbooks to initialize an EC2 instance.

Create Ansible EC2 Playbook

Open the terminal and create an Ansible playbook ending with a .yml extension. Edit the file with your favorite text editor and add the playbook as shown below:

---
- name: Ansible create ec2 instance
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Provision an EC2 instance
      ec2:
        region: us-east-2
        key-name: ansible-ec2-ssh-key
        instance_type: t2.micro
        image: am1-123456
        wait: yes
        count: 1
        group: sample_servers
        assign_public_ip: yes
      register: amazon_ec2
      delegate_to: localhost

Create SSH key pair

Once the playbook is set up, create an SSH key pair to log in to the EC2 instance once provisioned. Ensure to create a key with a similar name as the one defined in the key-name parameter in the playbook above.

ssh-keygen -t rsa -b 4096 -f ~/.ssh/ansible-ec2-ssh-key

Adding AWS Access and Secret Key

The next step is to add our AWS credentials to our playbook. For simplicity, we will add the credentials to the playbook in plain format.

NOTE: Do not expose your AWS Access and Secret key inside a playbook in real life. Consider using environment variables or tools such as Ansible vault.

---
- name: Ansible create ec2 instance
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Provision an EC2 instance
      ec2:
        region: us-east-2
        key-name: ansible-ec2-ssh-key
        instance_type: t2.micro
        image: am1-123456
        wait: yes
        count: 1The
        group: sample_servers
        assign_public_ip: yes
        vpc_subnet_id: default
        aws_access_key: *************XXXX
        aws_secret_key: *****************
      register: amazon_ec2
      delegate_to: localhost

The above shows the new playbook with the AWS credentials exposed. Avoid this!!

Once you have the playbook all setup, execute it using the command:

ansible-playbook create-ec2.yml

The above command should run the playbook above and create an EC2 instance.

Understanding the Playbook

Let us now understand the playbook provided in the examples above. Note, we will only focus on the ec2 part of the playbook.

  1. Region – This parameter defines the AWS region when creating the instance. You can check the available regions to use in the following resource.
  2. Key-name defines the SSH key pair to use on the created instance. Ensure the key already exists.
  3. Instance_type – defines the type of instance to create. Check the following resource to learn more.
  4. Image – sets the AMI ID to use when creating the instance.
  5. Wait – A boolean value to determine if Ansible should wait for the instance to be in the desired state before return.
  6. Count – the total number of instances to create.
  7. Group – sets the security groups for the EC2 instance.
  8. Assign_public_ip – Boolean value to define if the instances should be assigned a public IP address within the VPC.
  9. Vpc_subnet_id – defines the subnet ID under which the EC2 instance should be launched.

Using the Ansible AWS module, you can use the above arguments to create an EC2 instance.

Closing

This guide provides a tutorial on creating an Amazon EC2 instance using the Ansible AWS module.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list