Using ansible for deploying applications to aws cloud

Hello guys, in one of my previous post I have explained how can you deploy your simple application to AWS. In that post, we have created an AWS cloud account and then ran an ec2 instance manually using the AWS console UI and then by doing ssh into the machine from our local, we were able to deploy our application to the AWS. But this is not always a best practice to follow. This will be best if we could do this just by running some script once.  Actually, there are a couple of services which actually does the same thing for us and makes our life much easier.

Ansible is one of such service which we can use to create & configure AWS. In this post, I will explain how we can use ansible to start an ec2 instance and then deploy our application in that instance and finally terminating the instance when we are done.

Now before we can proceed first we need to know some basic things about Ansible. It is open source automation platform. It is very simple, efficient and powerful. Every ansible script that we run is called ansible play and the yml script is called playbook. Another important thing that we need to know before starting to write a script is modules. Modules are the unit of work that ansible ships out to the remote machine. To configure different services there are different modules. Like for configuring clouds, there are Cloud Modules for configuring clusters there is separate Clusters Module. As in this tutorial, we will be configuring aws ec2 instance so we will be using  ec2 module which is listed under clouds module.

Now before we can start we should keep one thing in mind. Python is one of major dependency of ansible so in order to run ansible scripts on your new machine you should have python pre-installed on them. This can be easily done using an amazon machine image of an ec2 instance having python installed in it. So while creating a new ec2 instance using playbook we can give this machine image id which in turn will create our new instance with python installed.

Also, you should create an IAM user with aws_secret_key and aws_access_key which will be required in the scripts and give ansible access to run the scripts on our machine.

So after you have installed ansible on your local and have an aws machine image we can write our first script/playbook to create ec2 instance.

Here is how a basic playbook should look : -

So here in the above playbook there are two tasks first we will be creating an ec2 instance and as you can see it requires some basic parameters that we usually mention in the UI while creating an ec2 instance from the aws console. At the end of the first task, we are giving a command called register this is basically used to store all the ec2 instance details after creating in a variable ec2 which can be used in other tasks in the playbook. Like if in other tasks suppose if we need ip address of the instance just created we can use ec2. Now in the second task, we are waiting for some time so that ssh is up in our new ec2 instance. We can start this playbook by using the following command in our terminal.

ansible-playbook create.yaml

This will start the ec2 instance. You can verify this by going to the aws ec2 dashboard. Now when our ec2 instance has been created we can now deploy our application. One extra thing that we need to before we can run our second script is adding the IP address of our newly created instance in the Hosts file since this will be required in our next play while deploying our application. In this tutorial, we will be using a sample node app that runs on port 3000. We basically want following script to be executed on our machine so first, we need to save this in a file and then we will transfer this script file to our remote machine from local and which in turn will get executed there.

The above script will clone the sample application from github, install node and then run our application in the background.
To transfer and run this script on our remote machine we will use an ansible command called script this command basically do two things, first, this will transfer our file from local to the remote machine and then this will execute this command on our machine. So this proves to be a very handy tool. Here is our second playbook to install and start our application.
You can run this using this command from your terminal

ansible-playbook -i Hosts start-application.yaml

Now you can verify if your application is started or not by opening <ec2-ip-address>:3000 and your application should be running there.

Now only part left is terminating our instance using ansible. So we can use following script to stop our instance. Here in this script, we need to give id of the ec2 instance which you can from the ec2 console or this will be shown while running the first script to create an instance.


You can run the script using this command in your terminal

ansible-playbook terminate.yaml


This is all for this post.  Here is the entire source code for this post you can clone this and start playing around. I will try to write about another popular provisioning service Terraform in my upcoming tutorial.  Feel free to comment below if you have any query.

Reference
ansible docs

Comments

Popular posts from this blog

All about NIT Agartala

How to integrate a payment gateway into your WordPress website for donations without using woocommerce

Implementing a Self Organizing List in Go