Configuration of Apache WebServer On AWS EC2 Instances Using Ansible

Abhishekkumawat
4 min readOct 26, 2020

--

Task Description :-

♦️Provision EC2 instance through ansible.
♦️Retrieve the IP Address of instance using dynamic inventory concept.
♦️Configure the web server through ansible!
♦️Create role for webserver to customize the Instance and deploy the webpage to root directory.

Amazon Web Services:

Amazon Web Services (AWS) is a subsidiary of Amazon providing on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis. These cloud computing web services provide a variety of basic abstract technical infrastructure and distributed computing building blocks and tools. One of these services is Amazon Elastic Compute Cloud (EC2), which allows users to have at their disposal a virtual cluster of computers, available all the time, through the Internet.

Steps for provisioning of EC2 instance through ansiblle :

  1. Installing boto/boto3 :
    Ansible depends on the Python module boto3 to communicate with AWS API. So, boto3 needs to be installed on your machine.
pip3 install boto boto3

2. Create ec2 instance on aws from control node.We have to write ec2.yml file as below :

Storing your keys in Ansible vault :

ansible-vault create awspass.yml

Once open, add the following to it:

myuser: *************************
mypass: **********************************

Once you save the file, all the content will be encrypted. You can verify it as:

3. Now run ec2.yml file to launch ec2 instance.

Now we Can see that Instance On AWS Has Been Launched

4. Now For Proceeding we need IP Of Our Instance Launched on aws . so we can just see and type the ip here in ansible inventory file but i am using dynamic inventory concept using this concept we can get the ip of launched instance on aws using one python program that u can get from ansible github.

Steps to retrieve the IP Address of instance using a dynamic inventory concept.

  1. First we have to download python code from github using wget which fetch ip addresses.
wget https://raw.githubusercontent.com/sanket3122/Ansible_Task2/master/ec2.py

now make it executable :

chmod +x ec2.py

2. For initiating ec2.py file we have to export region, access key, secret key as:

export AWS_REGION='ap-south-1'
export AWS_ACCESS_KEY_ID="aws_access_key"
export AWS_SECRET_ACCESS_KEY="aws_secret_key"

3. Now configure the inventory as :

4. Now checking all hosts by:

5. Now checking the connectivity between controller node and managed node :

Steps for configuring the webserver through ansible.

  1. Now For Configuring webserver in aws instance we need to do ssh and for this we will require private key.

♦️Transfer your private key to the controller node by using winscp software.
♦️After this you need to make it accessible.

chmod 600 task2.pemcat task2.pem

2. write playbook for webserver configuration on ec2 instance as:

3. Run the above playbook as:

4. Now, You can check httpd service is active or not on ec2 instance by ssh :

  • The above picture show you have successfully deployed your server .

Thanks, hope you guys will like my article. if you have any suggestion or query will free to ask.

#Happylearning #keepsharing #ansible #automation#aws #cloud

--

--

No responses yet