AWS ec2 instance configuration using Ansible

Goal is to connect to ec2 instance and configure it for future use.For simplicity i will just configure a small web server in this article.
First we need an ec2 instance , we can create it through ansible but it is not made for provisioning.For this we have other tools like terraform which we can integrate with ansible to make a complete automated system.

Anyways we assume we have ec2 instance running and now our task starts here.
So first we set up our inventory

Now we create our ansible config file.
Because AWS only uses key based authentication we need to import our private key from host(windows in my case) to Ansible Controller Node using WinSCP tool.And store it under ~/.ssh/id_rsa file.You can store it in any location but you have to update the same in ansible config file.

After this we need to setup some configuration in ansible.cfg file

Here we have given default username (ec2-user) which amazon image uses and we have used privilege escalation so as to run all commands as root user.

Now the configuration part is done and now we can connect to our ec2 instance.

Now we write our yaml code to setup web server in ec2 instance

This will isntall httpd on ec2 instance and will start the services and will copy our index.html file from Controller Node to ec2 instance

Here is how our index.html file looks like

Now time to run and test the code

Our code has been run successfully.

Now lets check ec2 instance.

Now lets check the website

Its working great.
This completes our task and thanks for reading …..!!!

When you want to know how things really work, study them when they’re coming apart.