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.

ansible docs


  1. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    Best Hadoop Training Institute In chennai

  2. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here. aws training in chennai

  3. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here. aws training in chennai

  4. Nice post keep do posting The Info was too good, for more information regarding the technology Click
    Best Amazon web Services Training Hyderabad
    Best Salesforce Training
    Professional Salesforce CRM Training

  5. Necessary to write that you simply almost no phrase to be able to many thanks all over again about the great ideas here Hosting Wordpress Aws you’ve added the following.

  6. Let's play together on the website of the online casino BGAOC. great gambling sites Do not miss the chance to win with us.

  7. This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb.
    This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolites festivity to pity. I appreciated what you ok extremely here 

    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

  8. Качественная лед лента разных цветов, герметичные и нет, я обычно беру в Экодио

  9. We are a part of the success story for many of our customer's successful cloud Migrations.
    Cloud Migration services

    Aws Cloud Migration services

    Azure Cloud Migration services

  10. Thank you for the informative post about Security challenges in AWS , Found it useful . cloud migration services have now become secured and with no-risk

    Vmware Cloud Migration services

    Database Migration services

  11. I have gone through your post and I found it very helpfull. Looking forward to see more post from you.

    Lia Infraservices

  12. I am really impressed with the way of writing of this blog. The author has shared the info in a crisp and short way.

    Cloud Migration services

    Best Cloud Migration Tool

  13. Such a useful blog with required information. Thanks for sharing this amazing blog.
    AWS Certification in Chennai
    DevOps Course in Chennai

  14. Great post! I really enjoyed reading it. Keep sharing such articles. Looking forward to learn more from you.Thanks for sharing.
    Reactjs Training in Chennai |
    Best Reactjs Training Institute in Chennai |
    Reactjs course in Chennai

  15. Extraordinary post I should state and a debt of gratitude is in order for the data. Instruction is unquestionably a clingy subject. Be that as it may, is still among the main subjects within recent memory. I value your post and anticipate more. data science course in london

  16. This comment has been removed by the author.

  17. I owe you a debt of gratitude because the article you've posted here is fantastic about Integrated Payment Systems Singapore. I learned something useful from this article. Thank you for sharing that. Continue to post.

  18. You have given essential data for us. about best hr software in uae It is excellent and good for everyone. Keep posting always. I am very thankful to you.


Post a Comment

Popular posts from this blog

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

How to use s3 bucket with node.js