Deploying Apache Web Server on AWS Instance through Ansible
By: Sachin Kumar
Tags: Rolê, Apache, Ansible, AWS, Vault

To perform the above scenario, let’s move ahead step by step. So let’s see.

Create an IAM Account

Create an IAM Account with programmatic access to get the access and secret key so that we can access the AWS services.

So you have to click next and next with proper access, your IAM account will be ready after a mintue but if you are no comfortable with this process then simply visit the below-mentioned article.

Configuration of the Controller node

After creating IAM Account, now you have to install ansible on your system that’s known as a controller node. So if your controller node isn’t configured then you can visit the below-mentioned article where you will find lots of information about the installation of Ansible.

Create a Vault

Ansible Vault is a feature of ansible that allows you to keep sensitive data such as passwords or keys in encrypted files, rather than as plaintext in playbooks or roles. Alternately, you may specify the location of a password file or command Ansible to always prompt for the password in your ansible. cfg file. So simply run the below command and put your access and secret key inside it which you download from IAM Account and then save it with your suitable password.

ansible-vault create vault_file_name.yml

Install boto library

As you can see in the above figure. That we can use our localhost IP address to behave as a managed node and we will use the SDK to launch the ec2 instance on AWS as Ansible is built on python language so we will be using boto. Boto as it an API so it has the capability to contact AWS.

sudo pip3 install boto3

Write a playbook for Instance

After installing the boto library, you can provision the EC2 instance through the Ansible playbook. So just create the playbook.

After creating the playbook, run it with your vault using the below command, and give te

ansible-playbook --ask-vault-password playbook_name.yml

So after running the playbook, let’s see your EC2 Dashboard and check the instance is provisioned or not. and write your private IP somewhere which you will get after running a playbook.

Copy a Local File to a Remote System with the scp Command

To copy a file from a local to a remote system run the following command:

scp -i key_name.pem remote_username@10.10.0.2:/remote/directory

Where key.pem is the name of the private key we want to copy, remote_username is the user on the remote server, 10.10.0.2 is the server IP address. The /remote/directory is the path to the directory you want to copy the file to. If you don’t specify a remote directory, the file will be copied to the remote user home directory

Give user login permission to managed node through Ansible. cfg

Now if you want to login to your EC2 instance dynamically, So write the below code in your ansible.cfg file. Because by default, login root has disabled. So you can’t log in with the root account. So go inside the below-mentioned location of the controller node and write the below syntax.

sudo vi /etc/ansible/ansible.cfg

Make an inventory and write the private IP of Instance

Now your config file has been configured in the controller node so you can do anything on your instance (managed node) whatever you want to do so write the private IP which you got when you run the playbook for provisioning the Instance.

And check that your managed node is available or not using the below command.

ansible all --list-hosts

Check your EC2 Instance (Managed node) is pingable or not.

ansible inventory_group -m ping

After checking all these things, create the role to deploy the webserver in the Managed node.

Create a Role for deploying the Webserver

Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users. Role directory structure. Storing and finding roles. So use the below command for creating the role.

ansible-galaxy init role_name

After running the above command, go inside your role and open the tasks folder, and write the below task inside the main.yml file.

after writing the tasks, open your files folder which is also inside your role, so put the webpage code here.

after completing these things, write the playbook to deploy the webserver with the role.

and run your playbook with the below command.

ansible-playbook playbook_name.yml

After running the playbook, take the public IP of your EC2 instance (Managed node) and browse your webpage which you deployed through the playbook.

So the webpage is also working well and looking properly.

Conclusion

Here, you have learned here about how to provision the EC2 instance and deploy the webpage with the role dynamically. So hopefully, you learned something from here and enjoyed this article.

I tried to explain as much as possible. Hope You learned Something from here. Feel free to check out my LinkedIn profile mentioned below and obviously feel free to comment. I write Cloud Computing, Machine Learning, Bigdata, DevOps and Web, etc. blogs so feel free to follow me on Medium.

Thanks, Everyone for reading. That’s all… Signing Off… 😊