End-to-End AWS-Terraform complete Automation

Anshika Sharma
6 min readJun 14, 2020
AWS-Terraform Integration

Hello guys, today i am going to show a great setup of Automated AWS and Terraform integration.

What is Terraform?

Terraform is a multi-cloud (IaC) Infrastructure as Code software by HashiCorp written in Go Language using (HCL) HashiCorp Config Language. An open source command line tool that can be used to provide an infrastructure on many different platforms and services such as IBM, AWS, GCP, Azure, OpenStack, VMware and more. By using a plugin-based model to support providers and provisioners, giving it the ability to support almost any service that exposes APIs. It is a cloud-agnostic tool that elegantly defines your IaC, simple and yet powerful; Terraform allows you to build complex, version controlled, collaborative, heterogeneous and disposable systems with very high productivity. It provides built-in dependencies between each tier that handle automatic provisioning of infrastructure, both in the public and private cloud.

Through this, we have a plan to make AWS services automated through Terraform. The services which we are going to create and automate are s3, EC2, CloudFront, Key-Pair, Security Groups, EBS volume ,Snapshots and some operations on these services.

To have a basic about these services please refer the below link :

Today’s our plan is to create such a environment that will integrate GitHub, AWS and Terraform, to create a complete automation. Our Task is :

Step 1: Create the key and security group which allow the port 80.

Step 2: Launch EC2 instance.

Step 3: In this Ec2 instance use the key and security group which we have created in step 1.

Step 4: Launch one Volume (EBS) and mount that volume into /var/www/html

Step 5: Developer have uploded the code into github repo also the repo has some images.

Step 6: Copy the github repo code into /var/www/html

Step 7: Create S3 bucket, and copy/deploy the images from github repo into the s3 bucket and change the permission to public readable.

Step 8: Create a Cloudfront using s3 bucket(which contains images) and use the Cloudfront URL to update in code in /var/www/html .

Architecture

So lets start the automation integration process. We will create each and every service using terraform.

  1. Creating the profile of the AWS account, the code will be :
Creating Profile

2. Specifying the Provider,

Specifying the provider, her AWS is the provider

3. Creating a Key-Pair, through Terraform :

Creating key named mykey111access01

4. Creating a Security group, with rules that allow ssh and http traffic.

Here, ingress is specifying the Inbound Rules or we can simply say the traffic input.

5. Creating S3 bucket, with terraform :

S3 bucket named bucketanshika23

6. Cloning the Github images to the local system :

Cloning github images

7. Uploading the Images to S3 bucket :

Uploading image to the Bucket

8. Creating a CloudFront distribution :

Hence we have created a cloud front distribution, now we can access our images through this CloudFront domain_name globally.

9. Creating an EC2 Instance,

creating an instance named web01

We have created an instance and now we will create a EBS volume.

10. Creating an EBS volume, in the same Availability Zone in which our instance has been launched.

Creating volume and accessing its ID

11. Attaching this external volume to the Instance, as soon as the volume is attached , I have created a null resource that will mount the volume on a particular folder so that the data will be secure even if the instance get terminated.

Attaching the Volume
Provisioning the partition

12. Creating the Snapshot, so that we can create any image or volume from this snapshot, we can think it as a backup file of the volume.

Creating Snapshot

13. Now at last we will automatically launch the Web Page through the link of the web-server , i.e.,

Opening the Web Page

Hence we have created an environment that with a single command it will start all the services mentioned above, a fully automated Environment.

14. We will use terraform command to deploy this web-server. Firstly we will Initialize the terraform or we can say we are adding some necessary plug-ins that are required.

terraform init command
initializing terraform

15. We will validate our code , through terraform validate (this command will check the syntax of our code):

terraform validate command
code is Valid

16. Finally to start the services, we use terraform apply wuth -auto-approve option so that it will not ask for confirmation, and it will be automatically approved.

terraform apply comand
started Applying the code to AWS
Creating the Instance
Launching the Web server

17. Now it will automatically open the static web server in the chrome with the start chrome command.

Launched web server with the s3 bucket image
Complete info from the web page

Hence we get our web server with a single command of terraform. Also we have successfully accessed the image that we have uploaded in the S3 bucket.

18. Even the beauty of terraform is it destroy all the services also with a single command, i.e., terraform destroy -auto-approve, again -auto-approve is for automatic approval of destroy command.

terraform destroy -auto-approve
Successfully destroyed the services

Terraform is a great technology, with which we can work on multiple cloud like Azure, OpenStack etc. This was just AWS but we can work on multiple cloud Environment with this single tool and not require the hands-on for each and every cloud’s CLI commands. With single set of commands of Terraform we can work on multiple clouds.

Hence, this was an Exciting and a successful implementation of such a great integration of AWS with Terraform with GitHub.

Thank you !!

--

--

Anshika Sharma

I am a tech enthusiast, researcher and work for integrations. I love to explore and learn about the new technologies and their right concepts from its core.