AUTOMATE WEBHOSTING BY INTEGRATING ANSIBLE WITH TERRAFORM

Saptarsiroy
4 min readMar 24, 2021

Whenever there’s a possibility of automation, even for single and simple things, there comes along lots of tools and processes that can be utilized and get the use-case concluded. However, using a single tool for complex pipelines may lead to intensive silos — even automation might consume good weight of resources. So what’s the explication? Yes, integration! An amalgamation of technologies no doubt simplify the process of tracking down the pipeline and implementing the same efficiently and painstakingly.

Integration of Technologies? Is that even feasible!

A big YES — if and only if the integration is substantial enough. Tweaking of anything can be done with anything, but if that creates more of a mess, that’s not acceptable. So, the integrations must always be selected and able to lead through a proper sequence. The consideration to integrate the indigenous perspective will facilitate the reconceptualization of the use-case to be handled and result in an optimized, user-friendly solution. The competitive game has changed: the advantage now often goes to the companies that are most adapt at choosing the newest approach, rather than those who still belong to the old-school, although scientific pathway.

So what’s the demonstration in this article?

Here comes the essence of technology, let’s do some automation!

𝑾𝒆 𝒂𝒓𝒆 𝒈𝒐𝒊𝒏𝒈 𝒕𝒐 𝒉𝒐𝒔𝒕 𝒂 𝒘𝒆𝒃𝒔𝒆𝒓𝒗𝒆𝒓 𝒘𝒊𝒕𝒉 𝒂 𝒔𝒊𝒎𝒑𝒍𝒆 𝒕𝒆𝒔𝒕 𝒘𝒆𝒃𝒑𝒂𝒈𝒆 𝒐𝒏 𝑨𝑾𝑺 𝒗𝒊𝒂 𝑬𝑪2 𝒊𝒏𝒔𝒕𝒂𝒏𝒄𝒆. 𝑻𝒉𝒆 𝒕𝒂𝒔𝒌 𝒄𝒍𝒆𝒂𝒓𝒍𝒚 𝒉𝒂𝒔 𝒕𝒘𝒐 𝒑𝒂𝒓𝒕𝒔 — 𝒊) 𝑳𝒂𝒖𝒏𝒄𝒉𝒊𝒏𝒈 𝒕𝒉𝒆 𝑬𝑪2 𝒊𝒏𝒔𝒕𝒂𝒏𝒄𝒆 𝒘𝒊𝒕𝒉 𝒑𝒓𝒐𝒑𝒆𝒓 𝒄𝒐𝒏𝒇𝒊𝒈𝒖𝒓𝒂𝒕𝒊𝒐𝒏𝒔 𝒊𝒊) 𝑫𝒆𝒑𝒍𝒐𝒚𝒎𝒆𝒏𝒕 𝒐𝒇 𝒕𝒉𝒆 𝒘𝒆𝒃𝒔𝒆𝒓𝒗𝒆𝒓 𝒐𝒏 𝒊𝒕. 𝑻𝒉𝒆 𝒇𝒊𝒓𝒔𝒕 𝒕𝒂𝒔𝒌 𝒊𝒔 𝒈𝒐𝒊𝒏𝒈 𝒕𝒐 𝒃𝒆 𝒅𝒐𝒏𝒆 𝒃𝒚 𝑻𝒆𝒓𝒓𝒂𝒇𝒐𝒓𝒎 𝒂𝒏𝒅 𝒕𝒉𝒆 𝒏𝒆𝒙𝒕 𝒕𝒂𝒔𝒌 𝒘𝒊𝒍𝒍 𝒃𝒆 𝒄𝒂𝒕𝒆𝒓𝒆𝒅 𝒃𝒚 𝑨𝒏𝒔𝒊𝒃𝒍𝒆. 𝑩𝒐𝒕𝒉 𝒕𝒉𝒆𝒔𝒆 𝒕𝒆𝒄𝒉𝒏𝒐𝒍𝒐𝒈𝒊𝒆𝒔 𝒘𝒊𝒍𝒍 𝒃𝒆 𝒄𝒍𝒖𝒃𝒃𝒆𝒅 𝒕𝒐𝒈𝒆𝒕𝒉𝒆𝒓 𝒔𝒐 𝒕𝒉𝒂𝒕 𝒂 𝒔𝒊𝒏𝒈𝒍𝒆 𝒄𝒐𝒎𝒎𝒂𝒏𝒅 𝒘𝒊𝒍𝒍 𝒔𝒕𝒓𝒂𝒊𝒈𝒉𝒕-𝒂𝒘𝒂𝒚 𝒑𝒖𝒕 𝒖𝒑 𝒕𝒉𝒆 𝒄𝒐𝒎𝒑𝒍𝒆𝒕𝒆 𝒂𝒓𝒄𝒉𝒊𝒕𝒆𝒄𝒕𝒖𝒓𝒆!

TASK-1: Launch EC2 instance on AWS using terraform.
The architecture setup on this ec2 instance will be made highly available using the following configurations:-

  • Document root of webserver would be made persistent by mounting on attached EBS volume.
  • Static objects used in code such as pictures stored in S3
  • Setting up Content Delivery Network using CloudFront and using the origin domain as S3 bucket.
  • Save the CloudFront domain URL in another file which can be used as the variable file for in the webapp code for least latency.

TASK-2: Deployment of the webserver using ansible.
The playbook written for Ansible would do the following tasks —

  • Install HTTPD package in the instance and configure it as a webhost.
  • Allow http rule via firewall
  • Copy the Jinja template for the httpd configuration file to required destination.
  • Also copy the webpage code to the newly configured document root with proper permissions.
  • Finally restart httpd service (handlers to be used so that restarting could be made idempotent

To get a perspective of the folder structure and codes to be used, check out here!

Given that we are good-to go with our code, let’s start deploying!

 terraform init
terraform validate

And finally, time to run the Terraform code which has the ansible playbook clubbed into.

terraform apply -auto-approve
Running of terraform code
Running the ansible playbook on the same go

Once the apply is complete, all we need is just to jab down the public IP of the instance given as output, and…
…There we go!

Webhost deployed!

Two technologies trouped together lead into a swift automation, that’s the power!

Thank you all for giving a patient read, do leave a clap if this article interested you :)

--

--