Provisioning EC2 Instance and deploying Webserver on it using single ANSIBLE playbook

This article is about provisioning a AWS Instance using Ansible Playbook, and after its creation, same ansible playbook will deploy apache webserver on this EC2 instance and hence we can see our Webpage running on PublicIP of Instance.

Technologies Used :-

  • Ansible
  • AWS

Pre-requistes :- Basic Knowledge regarding ansible playbook (i.e how to write it), and basics of AWS EC2 Instance.

→ I have used IAM user “akhil” created in my AWS Account. For creating IAM user, follow this link below, to see how to create IAM user.

IAM user — akhil

I have created a folder “task2” which is the main folder and contains, private_key , main.yml, vault.yml .

task2 folder

— I have created a vault named as “vault.yml” which contains my access key and secret access key and these 2 are stored in ak and sak variables respectively.

This vault.yml file is encrypted using AES256 algorithm
Encrypting vault.yml

So, Let’s Start Building the Playbook : —

Installing boto and boto3

Initially in this playbook, we have installed boto, boto3 which are python libraries used to give interface to Amazon Web Services.

→ We have created our own Security Group for our instance using the ec2_group module.

Security Group
  • Name is “launch-wizard-1” and in deployed in default VPC in Mumbai region.
  • I have allowed SSH and HTTP port (22 and 80) respectively for ingress and allowed traffic from anywhere.

We will use this security group in our instance.

→ Now, we will provision our EC2 Instance using our playbook, and we will ec2 module for this.

Provisioning EC2 Instance
  • Again, we have provided access key and secret access key.
  • In this, we have given the details of our instance, such as instance_type, ami_id, region,no of instances to be created, security group, key_name, assigning public_ip and VPC_id.

We have to change the permissions of our private_key, to make it executable.

We want to create host group which will contain the IP Address of our Instance as our main task is to deploy webserver there. To do this, we will use add_host module.

I have used debug module to print the Public IP of our Instance.

add_host and debug module

Now, we want that after the creation of our instance, automatically, webserver is deployed on our instance. So, we want the IP Address of our Instance and then do ssh there to go inside and then deploy webserver there.

Deploying webserver
  • We have installed webserver and then Created a file index.html and copy the content part there in the file, and then started the service and then enabling it which will make the state of our webserver as started permanently.

Last step is to provide our private_key to ansible for ssh to instance as it is the only way for our authentication. For this, we have to add path of our key to ansible configuration file.

Ansible Configuration file
  • Privilege Escalation block is used to give special powers to ec2-user and hence can install webserver and start the service.

Now, everything had been setup, and good to go and Provision EC2 Instance.

Initially, no instances are running and only default security group is there.

We will run ansible command to run our playbook. Using Below Command :

# ansible-playbook — ask-vault-pass main.yml

As no error come in the output, it means that everything has been created successfully. We can run one time the same command and hence everything we will see be in the green color.

Now, we can see AWS Management Console to see deployment.

Instance created (a)
Instance created (b)
Security Group Created
Pre-created Private key

Now, we will do ssh using browser to see, whether our webserver is deployed or not.

Connecting using ec2-user
We can see that SSH is possible
Package is installed
File is also created and content is copied too
Webserver is running

Now, everything is deployed and running perfectly, now we check that if webpage is working or not.

Webpage is accessible

Ansible Docs link for reference :

  1. For EC2_Group module

2. For EC2 module

You can refer this site for more modules such as debug, package, copy, service, add_host, wait_for_connection and lots more :

GITHUB URL :-

“I have practiced and gained all knowledge of this project(task) under the mentorship of Mr. VIMAL DAGA Sir during the Ansible Training by Linux World India.”

I hope this article is Informative and Explanatory. Hope you like it !!!

For any suggestions or if any reader find any flaw in this article, please email me to “akhileshjain9221@gmail.com”

Thank You Readers!!!

I am a student and persuing under graduation in computer science and engineering.