Create an EC2 instance and S3 Bucket with Ansible Playbook

Venkat
4 min readJul 7, 2023

--

Creating EC2 instances manually it can be taking more time. Fortunately, Ansible provides a more efficient and automated way to create EC2 instances. In this article, we will go through the step-by-step process of creating an EC2 instance and S3 Bucket with Ansible Playbooks.

You will need to follow the below prerequisites:

Step 1: Sign In to the AWS Console https://aws.amazon.com/console/

Step 2: Launch an Ubuntu Machine.

Step 3. Create a IAM user and attach Administrator permissions to the user.

Step 4: Connect to ubuntu machine in AWS console.

Step 5: Update ubuntu machine default packages following the below command.

# sudo apt update

Step 6: Install ansible on ubuntu machine following the below command.

# sudo apt install ansible

Step 7: Install the ansible galaxy modules following the below command.

# ansible-galaxy collection install amazon.aws

Step 8: Install python and boto packages following the below command.

# sudo apt install python3-pip

# pip install — user boto3

Step 9: Configurre the AWS Credentials following the below command.

# aws configure

Step 10: Create Ec2instance-S3bucket.yml file.

— — —

- name: Create EC2 instance

hosts: localhost

gather_facts: yes

tasks:

- name: Create EC2 instance

community.aws.ec2_instance:

name: my-instance

instance_type: t2.micro

image_id: ami-0f5ee92e2d63afc18

key_name: test

security_groups: default

- name: Create S3 bucket

amazon.aws.s3_bucket:

name: my-bucket87653890-name

state: present

Let’s go through the contents of this ansible playbook step by step:

a. hosts: localhost specifies that the playbook will be run locally on the Ansible control machine.

b. gather_facts: no specifies that Ansible will not gather facts about the host.

c. EC2_Instance: ec2 is the main task that creates the EC2 instance. We specify the key name, instance type, AMI ID, region, security group, and count. The wait parameter specifies that Ansible will wait until the instance is created before proceeding.

d. Replace: Replace t2.micro with the desired instance type, ami-123456890 with the desired Amazon Machine Image (AMI), ap-south-1 with the desired region, key-name with the name of your SSH key pair, my-security-group with the name of your security group, and my-instance with the desired name of your instance.

Step 11: Execute the ansible playbook following the below command.

# ansible-playbook Ec2instance-S3bucket.yml

Post checks:

Step 1: Check whether Ec2 instance created or not?

Step 2: Check whether S3 Bucket created or not?

That’s it! You have successfully created an EC2 instance and S3 bucket with Ansible Playbook 😊

Thank You for reading….

Venkat Kumar

--

--