Terraform Basics: 6 Steps Guide on Setting Up Terraform Infrastructure on AWS

Badal Mishra
3 min readNov 21, 2020

What is Terraform?

Terraform helps you describe Infrastructure using a high-level configuration syntax ( HashiCorp Configuration Language aka HCL) making Infrastructures shareable and reusable.

As Terraform makes it possible to manage Infrastructure as Code, we have the freedom to even maintain versions of different state of our infrastructure using version control tools. You can read more about Terraform’s features on their website here

Photo by Kevin Ku on Unsplash

Why Terraform ?

Many cloud infrastructure providers do have their own ways of managing Infrastructure as Code. But the issue is that we have to learn the different syntax and/or the different ways of doing that for every provider that we eish to work with. Well, Terraform solves this issue for us, as it supports a wide range of cloud infrastructure providers such as AWS, AZURE and HEROKU. You can check the entire list of cloud infrastructure providers supported by Terraform here.

Creating your first Terraform infrastructure on AWS

Before starting, we need to create a new user in the IAM Section on AWS . Here is link that will lead you there. Do select Programmatic access while you enter your user details. Finally get the Access key ID and Secret access key of that new User and store it somewhere safe.
Great! We are good to start now.

Step 1: Install Terraform

Download Terraform here and follow the guide here on how to install Terraform on your specific system.

Step 2: Create a file called instance.tf with the following code.

Paste this code in instance.tf

provider “aws” {   access_key = “ACCESS_KEY_HERE”   secret_key = “SECRET_KEY_HERE”   region = “us-east-1”}resource “aws_instance” {   ami = “ami-13be557e”   instance_type = “t2.micro”}

Break Down of above code

In the first line we specified that we are using the AWS provider plugin from Terraform. We provide the access key/secret from the user in IAM that we created. We also supply the region in which we want to make all changes. In this example, we are using “us-east-1”.

One the 6th Line we provided the resource identifier “aws_instance” to mention that we are trying to bring up an EC2 instance followed by the name identifier “example”. This can be anything you desire.

Then comes the AMI type on AWS. An AMI is an identifier specific to AWS for the image you wish to install on the instance (Ubuntu/Windows 32/64bit etc..). You can find the specific AMI you wish to deploy per region here.

Lastly we provide the “instance_type”. In this example, we will use a t2.micro instance type as it is supported by the AWS Free Tier package.

Step 3: Initialising Terraform
Now we will run the “terraform init” command where we created our instance.tf file to download and initialise the appropriate provider plugins. In this case, we are downloading the AWS provider plugin we specified in our instance.tf file.

Step 4: Run the “terraform plan” command

This will let us see what Terraform will do before we decide to apply it

Step 5: Run the “terraform apply” command

Once this executes successfully, go to EC2 section on the AWS console, you can see that a t2.micro instance was created successfully!

Note: Running “terraform apply” command again won’t create another instance, the number of instances ( resources ) created will always be same as the number of instances( resources ) mentioned in your *.tf file

Step 6: Execute Intial setup commands on the newly created instance (Resource)

The remote-exec provisioner invokes a script on a remote resource after it is created

Example,

resource "aws_instance" "web" {
# ...

provisioner "remote-exec" {
inline = [
"puppet apply",
"consul join ${aws_instance.web.private_ip}",
]
}
}

You can learn more about remote-exec provisioner here

Also, you can run the “terraform destroy” command to destroy the newly created Terraform infrastructure.

Thanks for reading. Please do follow if you find the content helpful.

--

--