Complete end to end Infrastructure Automation using terraform

Posted By : Rishabh Jain | 22-Mar-2022

Terraform is a IAC ie. Infastructure as a Code tool which is used for building, changing, and versioning of infrastructure very safely and efficiently. This can us with multi-cloud by having one workflow for all clouds which may be AWS, Azure, GCP etc. The infrastructure manages can be hosted on both the public clouds like Amazon Web Services(A.W.S) , Microsoft Azure, and GCP, or on private clouds such as OpenStack from redhat, or CloudStack. Terraform treats this infrastructure as code (IaC) so that we never have to worry about our own infrastructure getting away from its own desired configurations.

What to plan?

say our plan is to build an automated and complete end-to-end infrastructure with the help of IaC (Infrastructure as code) on Amazon Web Services Cloud. For this infrastructure, our first need is to generate a key-pair for secure login and a security group in EC2, so as to launch the EC2 instance with an extra attached EBS ie. Elastic Block Storage Volume for persistent storage. Then, our second need is  to launch a webserver over the EC2 instance which will deploy the whole website code from your Github Repository. Now, we will launch a S3 bucket in which we will put some static content which may be  image/videos as an Object and which will be deployed again from our Github Repository, and, also this same bucket will act as origin for the Cloudfront Service. Cloudfront will provide us with a unique URL which will deploy the static content from our S3 bucket to the launched webserver over the EC2 instance in a very few amount of time in seconds and with very low latency due to which we can access webpage easily and earlier. This complete infrastructure will be automated and built using Terraform over the AWS cloud.

Note: Make sure that Terraform is successfully installed on your system and its path is also set in environment variables before going through the mentioned steps.

Steps to be followed up:-

  1. Configuration of user's profile:
  • AWS CLI must be installed on your system to run these commands on the prompt.
  • Enter your AWS Access Key ID and AWS Secret Access Key for the respective profile here. (I have already entered mine)

   

    2. Create a separate folder to store all the required data in one place and then write the code in steps in .tf file created here :

  • A text file named ‘ec2.tf’ will now avaialable on the screen. Now, we can start to write the code in file.

    3. Specify the  provider ie AWS.

code to specify provider

 STEPS FROM HERE HAVE THE GLIMPSES OF THE RESULTS ON AWS WebUI AS WE WILL GO THROUGH THEM ONE BY ONE.

    4. Launching a key-pair for secure login:

  • A key-pair (public-key as well as private key) is generated using the following command on prompt.

ssh-keygen -f key_name

  • we have already created our key-pair named as ‘ myawskey ’ using the above command. Now, launch this key-pair on AWS as well :

code to launch a key-pair

launched a key-pair on AWS WebUI successfully

    5. Launch a Security group:

code to launch a security group and allow port number 80, SSH and git

launched  security-group successs fully on AWS WebUI successfully

     6. Launched an EC2 Instance with key-pair and security groups created previously:

code to install the required softwares like git, httpd and also to start the httpd web-server

EC2-Instance has been launched on AWS WebUI successfully and configured.

     7. Launch the EBS volume in the same availability zone as that of the EC2 instance :

code to launch the ebs volume in same zone.

EBS volume has been launched successfully in the same availability zone ie ap-south-1a as that of ec2 instance successfully.

    8. Attaching EBS volume with the EC2 instance and also mounting the volume to folder /var/www/html :

this is the code to attach the EBS volume the instance and also to mount it to location /var/www/html present in instance.

ebs volume has been attached successfully to the ec2 instance

ebs volume has been  mounted successfully to folder at location /var/www/html

    9. Creating a S3 bucket :

This is the code used to create a S3 bucket and also to make a directory on the baremetal local system where all the images will be downloaded ie cloned from github repository and then this directory will be automatically removed as soon as the infrastructure is destroyed.

s3 bucket has been created succesfully.

    10. Creating a S3 bucket object which is publically readable with permissions provided:

This is the code to upload the static content i.e image from git repository as s3 object in bucket which is publically readable

s3 object with permission to be read publically has been successfully created and readable

    11. Create a Cloudfront distribution with S3 as Origin :

code to create a cloudfront distribution with S3 service as the origin and using the cloudfront URL to update the code in the folder  /var/www/html

cloudfront distribution has been created succesfully with s3 as the origin.

    12. Creating a null resource to execute the command to display our webpage on the local system.

This will display our webpage on chrome browser.

BASIC TERRAFORM COMMANDS :-

1. terraform init : this will initializes a working directory containing the Terraform configuration files.

2. terraform apply :  this is used to apply the changes required to reach the desired state of the configuration.

3. terraform validate : this validates the configuration files in a directory, referring only to the configuration and not accessing any remote services such as remote state, provider APIs, etc.

4. terraform apply -auto-approve : this will skip interactive approval of plan before applying.

5. terraform destroy : this is used to destroy the Terraform-managed infrastructure.

6. terraform destroy -auto-approve : this is used to destroy the infrastructure without asking for confirmation.

All the steps have been succesfully run now and the whole infrastructure has been created.

Code to intiliaze the script:

successfully ran command : terraform init

successfully ran command : terraform apply -auto-approve

Now, our webpage is been deployed and can be seen on webpage.

We can destroy the infrastructure using the following command:-

So, this is how we can build an entire end-to-end complete automated infrastructure on AWS using Terraform Script.

About Author

Author Image
Rishabh Jain

He is a Devops person who loves Automation and have skills in various technologies like Docker, Linux, Kubernetes, Ansible, Terraform and Many More.

Request for Proposal

Name is required

Comment is required

Sending message..