Pushkar sinha
4 min readJan 2, 2022

--

I had always this thought what if I can automate the infrastructure? as a CS engineer always try to make automate all the processes. Always and I love to work with AWS as my primary cloud service provider using AWS with Terraform. For Continuous Integration, I cant always log in to the AWS console and provisioning the servers. So I made an Infrastructure as code using Terraform.

What is Terraform?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. Configuration files describe to Terraform the components needed to run a single application or your entire data-centre.

The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.

My Aim for This Project :

To Create an Infrastructure as Code to Start an AWS instance and Run apache server. Allow access to HTTP and SSH. The Images of Website should be stored in AWS S3 bucket with AWS CloudFront and The Files of Website should be Shored in Another EBS Storage that works as Backup. All with Terraform.

Context

  1. Add Provider to Terraform
  2. Add Keys And Security Group that allow HTTP and SSH
  3. To Create Instance In EC2 and install Required Software in it. i.e. httpd, git, PHP
  4. To Create an EBS Storage as Backup and Mount it to /var/www/html
  5. Create an S3 Bucket and store the Images
  6. Make a CloudFront URL for that Images

Lets Go :-

Add Provider to Terraform

Here I added the path of My Credentials file for Access code And Secret Code of AWS. Alternatively  We Can add "Profile" Attributes Too asProfile = "Name_of_profile"

Add Keys And Security Group that allow HTTP and SSH

-Here I added Key and Security Group for My EC2 Instance
-The InBound Rule Are for SSH and HTTP Only And Outbound Rule for Every Protocol

LET’S Create Instance In EC2 and install Required Software in it. i.e. httpd, git, PHP

-Here I Used 3 Attributes in Resource :
-AWS_Instance -For Provisioning a EC2 Instance
-Connection -For Connecting To instance Using SSH
-Provisioner "Remote-exec" -For Running Command in the Remote Server i.e. Instance

To Create an EBS Storage as Backup and Mount it to /var/www/html

-I Created An EBS Volume of 1GB in the Same Availability_zone as Instance Then Attached it To that Instance-I Created A null Resource For Mounting my EBS Volume to the Default Apache server  location.
-Which is Dependent on EBS Attachment.
-Where I also Run git Clone Command to get My Website Content to Apache server.

Created an S3 Bucket and store the Images

-Created An S3_Bucket to Save Images of My Website

Make a CloudFront URL for that Images

-Created a CloudFront URL for S3 Bucket Which Contains Image

Connect with me:https://www.linkedin.com/in/pushkar-sinha-b208631b7/

--

--