Automation of Deployment the web Application by Integration of Ansible with AWS Cloud

Ansible + Amazon

Hello World! In this article, I am going to talk about an overview of Ansible and AWS Services. How we can use Ansible Scripts with AWS to the deployment of the webserver. To create this application we are using or writing as a script of Ansible Playbooks and also deploying the webserver on AWS. The first thing we should know, What is Ansible?, What is Amazon Web Services? , How we can deploy web servers on AWS Services using Ansible?

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

What is Ansible?

Ansible

Ansible is an open-source IT Configuration Management, Deployment & Orchestration tool. It aims to provide large productivity gains to a wide variety of automation challenges. This tool is very simple to use yet powerful enough to automate complex multi-tier IT application environments.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

What is Amazon Web Services?

AWS

Amazon Web Services (AWS) is the world’s most comprehensive and broadly adopted cloud platform, offering over 175 fully-featured services from data centers globally. Millions of customers — including the fastest-growing startups, largest enterprises, and leading government agencies — are using AWS to lower costs, become more agile, and innovate faster.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

How we can deploy web servers on AWS Services using Ansible?

There are some following steps to perform the practical are as follows.

1. Launch an AWS instance with the help of ansible.

2. Retrieve the public IP which is allocated to the launched instance.

3. With the help of the retrieved Public IP configure the web server in the launched instance.

So Let’s Start:

  1. To launch an Instance on AWS cloud using Ansible, we use the Ansible ec2 module by which Ansible going to launch ec2 instance on AWS. For AWS login we require two types of keys as public access key and secret access key. I make the use of IAM users to create security keys for authentication of the AWS account.
IAM

2. For a security perspective, it is always good to store security credentials in separate files and make the use of variables in the main playbook. Here the use of Ansible vault which encrypts the security credential file in which we keep our AWS security keys by specifying variables for it.

To encrypt the file using Ansible vault use command →

ansible-vault encrypt file_name

The output is:

Vault

3. By using the ec2 module in the playbook Ansible can provision ec2 Instance on AWS cloud by using the file named as ansible.yml containing security keys that are encrypted using vault for authentication on AWS cloud.

Ansible Playbook

The output is:

EC2

We see in AWS console that ec2 instance is successfully created by Ansible .

4. Now, we have to configure a web server into launched ec2 instance. For that, we have to do an SSH login to go inside ec2 Instance. All these things we going to do using Ansible which requires public IP of ec2 Instance and SSH Private key to log in. Since for Ansible ec2 Instance has become a managed node. To contact managed node Ansible using Inventory file which is provided in ansible.cfg file. But in this TASK we are going to add ec2 instance to the host group of Ansible dynamically using keyword add_host.

This add_host keyword used to add the hosts to ansible inventory file dynamically by creating a host group. By using these the IP of ec2 has fetched dynamically and set as host for the Ansible.

Here the use of keyword with_items gives the output in JSON format and use of keyword wait_for that is to wait for ec2 Instance that they first start port no 22 for SSH login.

5. The next step is to log in to ec2 Instance via SSH which requires SSH private key and Instance IP. For that, the configuration file of ansible should be provided with the private key file.

[defaults]

host_key_checking = FALSE

private_key_file = /root/Ansible-tasks/ansible_task2.pem

One more thing is to change the permission of the key file as it is by default restricted by some permissions under the root user. By changing the permission using the following command key file can be used for login by Ansible.

chmod 400 ansible_task2.pem

Using the private key file and retrieved public IP of ec2 Instance Ansible going to configure webserver on their manged node i.e ec2 Instance.

After successful completion of the Ansible tasks, we can see the output by using public IP of ec2 instance whether the webserver is successfully configured or not.

We run the ansible-playbook which done all the above tasks successfully by using the following command.

ansible-playbook  --ask-vault-pass  playbook_file_name

One-click the entire set up is done by the end to end automation.

Final Output

Thank you very much for reading my blog hope it will help you:

If you like my blog please like and hit a subscribe to my profile.

You can find me on LinkedIn:

Software Engineer || Mobile App Developer