AUTOMATED AWS INSTANCE WITH TERRAFORM

Rishabh Jain
9 min readAug 28, 2020

Hey! folks there is something wonderful here I am going to show here that how you can launch your instance, creating volume through EFS (Elastic File System) ,connecting it to VPC and subnets , creating S3 bucket , creating Security Group, Cloudfront and moreover launching the Web Server without going accessing the instance and all the setup just with the Terraform Code in AWS Cloud.

First let me introduce you with some important Terminologies and then we will jump to create this awesome automated system.

1. EFS => EFS stands for Elastic File System which is actually a file system service provided by the AWS cloud which is by default encrypted and contains a pre created volume and can be connected with VPC and subnets and can be launched in any of the availability zone and region.It is configured with at its best and user gets almost every faculty that user wishes to have and hands on.

2. VPC => It is a logically isolated section of the AWS Cloud where you can launch AWS resources in a virtual network that you define. You have complete control over your virtual networking environment, including selection of your own IP address range, creation of subnets, and configuration of route tables and network gateways. You can use both IPv4 and IPv6 in your VPC for secure and easy access to resources and applications.

3. S3 => Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. This means customers of all sizes and industries can use it to store and protect any amount of data for a range of use cases, such as websites, mobile applications, backup and restore, archive, enterprise applications, IoT devices, and big data analytics.

4. Security Group => A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic. Inbound rules control the incoming traffic to your instance, and outbound rules control the outgoing traffic from your instance. … If you don’t specify a security group, instance in cloud uses the default security group.

5. Subnets => It is a logical subdivision of an IP network. The practice of dividing a network into two or more networks is called subnetting. AWS provides two types of subnetting one is Public which allow the internet to access the machine and another is private which is hidden from the internet.You can understand it as the Availabilty Zones that has their own systems and their IP to make use of them and contributing them in the setup or network.

6. CloudFront => It is a content delivery network (CDN) offered by Amazon Web Services. Content delivery networks provide a globally-distributed network of proxy servers which cache content, such as web videos or other bulky media, more locally to consumers, thus improving access speed for downloading the content.

7. EC2 Instance => Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction. It provides you with complete control of your computing resources and lets you run on Amazon’s proven computing environment.

8. Web Server => A web server is software and hardware that uses HTTP (Hypertext Transfer Protocol) and other protocols to respond to client requests made over the World Wide Web. The main job of a web server is to display website content through storing, processing and delivering webpages to users.

9. Terraform => Terraform is an open-source infrastructure as code software tool created by HashiCorp. Users define and provision data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON. Basically it is used to write the code and as an output the coded stuff output is made inside the cloud itself.

Before going if you are very new to the AWS and its service you should first go through my this blog.

Now, let’s go for it !! Every Terraform code written below will be available for your reference in my GitHub Repository. I will provide the link at the end of the blog.

STEP-1 Login to your AWS account through CLI in your profile as shown below.

If you have not done it before you can refer to my blog given here.

STEP-2 After logging in we will first create a Security Group here through the Terraform code giving the provision to access the users through SSH and TCP Protocol.

SECURITY GROUP

After writing the code save it with the extension of .tf and afyrt that go to cmd (location where your file is saved)

Now

  1. terraform init => Used to intialize the terraform for the file.
  2. terraform validate => Used to check the syntax in the file.
  3. terraform apply or terraform apply -auto-approve => Used to generate the output in the cloud.

as shown below.

TERRAFORM INIT
TERRAFORM VALIDATE
TERRAFORM APPLY

Your security group will appear in the cloud as shown below.

SECURITY GROUP IN AWS CLOUD

You can use terraform destroy or terraform destroy -auto-approve to destroy the output made in the cloud.

STEP-3 After the Security group is created let us move to make a key through a Terraform Code.

After writing the code just do the following:

  1. terraform init
  2. terraform validate
  3. terraform apply or terraform apply -auto-approve

as shown below.

TERRAFORM INIT
TERRAFORM VALIDATE
TERRAFORM APPLY

Your key is now made in cloud and shown below.

KEY PAIR IN AWS

You can use terraform destroy or terraform destroy -auto-approve to destroy the output made in the cloud.

STEP-4 Let us go and now create out S3 bucket and upload an object(can be image or something else) in it.

CODE FOR S3

TERRAFORM CODE FOR S3

After writing the code just do the following:

  1. terraform init
  2. terraform validate
  3. terraform apply or terraform apply -auto-approve

as shown below.

TERRAFORM INIT
TERRAFORM VALIDATE
TERRAFORM APPLY

Your S3 bucket will be made in the cloud as shown here.

S3 BUCKET

You can use terraform destroy or terraform destroy -auto-approve to destroy the output made in the cloud.

Now before moving ahead click on the Bucket name in the Cloud andyou will be get entered in the bucket as shown below. Just click on the Object name there(the image you uploaded).

Click on the Make Public Option there .This option is made so that it can be accessed from other services or by a URL.

Once done if you scroll down you will see there a URL if you click on it you will be able to access your object from there.

STEP-5 Now let us move to create the CloudFront through the Terraform code.

CODE FOR CLOUDFRONT :

After writing the code just do the following:

  1. terraform init
  2. terraform validate
  3. terraform apply or terraform apply -auto-approve

as shown below.

TERRAFORM INIT
TERRAFORM VALIDATE
TERRAFORM APPLY

Your CloudFront is now created in the AWS Cloud.

CLOUDFRONT CREATED

Now copy the domain name and paste it in the browser with / name of your image with extension. You will be able to access your S3 object.

You can use terraform destroy or terraform destroy -auto-approve to destroy the output made in the cloud.

Now before moving to the instance creation let us first upload the webserver code to the GitHub and paste the domain name with object in the Github Repository as shown below.

PASTED CLOUDFRONT LINK

STEP-6 Now let us move to make the instance and attach it with the VPC and subnets.

You can get the default VPC from the cloud as shown here. Although you can create your own.

DEFAULT VPC IN CLOUD

You can take the subnet as same the VPC there are default subnets also available in the cloud or you can create your own.

SUBNETS IN CLOUD

Let us write the code to make the instance through Terraform code.

Terraform Code to Create Instance:

INSTANCE WITH EFS , VPC AND SUBNET CONFIGURATION — 1
INSTANCE WITH EFS, VPC AND SUBNET CONFIGURATION — 2

In this VPC IP and SUBNET id and IP is used to connect the EFS with own network and subnet to set a data center fot the Instance to be launched. It is highlighted below.

HIGHLIGHTED VPC AND SUBNET IP
INSTANCE WITH EFS , VPC AND SUBNET CONFIGURATION — 3

After writing the code just do the following:

  1. terraform init
  2. terraform validate
  3. terraform apply or terraform apply -auto-approve

as shown below.

TERRAFORM INIT
TERRAFORM VALIDATE
TERRAFORM APPLY

After the process is completed automatically a web page will open in your browser and the object with the respective code will be appearing there. The awesome setup is now ready and launched.

An EFS will be created in Cloud as shown below.

EFS CREATED CONNECTED WITH VPC AND SUBNETS

The webpage appeared is shown here.

WEB SERVER LAUNCHED

You can use terraform destroy or terraform destroy -auto-approve to destroy the output made in the cloud.

Here is the GitHub Link given below where you will get every Terraform code.

I have also put the file to just create the ElasticFile System attached with VPC and Subnets.

Thanks a lot ! Hope you enjoyed the reading.

--

--

Rishabh Jain

I am a tech enthusiast, researcher and an integration seeker. I love to explore and learn about the right technology and right concepts from its foundation.