Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

terraform
devops
kubernetes
aws
communitycreator

What is Terraform with AWS?

Arsh Sharma

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

This shot will be about what Terraform does, why you might need it, and a quick demo where we will use Terraform with AWS. Let’s begin!

What is Terraform?

Terraform’s official website says that:

Terraform is an open-source infrastructure code software tool that provides a consistent CLI workflow to manage hundreds of cloud services.

The first question you might have is, “What exactly is infrastructure as code?”

Well, whenever you use a cloud provider service to set up the infrastructure for your project, you typically create all the required resources using the web UI or the CLI for that particular service. Infrastructure as code is the process of defining all your infrastructure in a machine-readable format as code!

Terraform is simply a tool that takes the infrastructure you have defined as code, and brings it to life.

To simplify, let’s say you want to create an S3 bucket in AWS. You would write this instruction as code (which we will learn later), and then Terraform will execute the code, meaning it will create that bucket for you.

Pretty cool, right?

Why Terraform?

Now, you might be wondering why you would want to use Terraform, or this whole infrastructure, as a code approach over what you have been doing traditionally. You can read about the advantages below:

Bye bye errors!

When creating your infrastructure manually, there are many screens and wizards you have to go through to configure it, and many settings that you need to take care of. When there is so much to configure, it is quite easy to set something incorrectly by accident.

With Terraform, you write all of it as code, which means it will all be in one place for you to see. Therefore, the chance of you making a mistake while setting all this up is lower.

Reproducibility

While doing things the traditional way, it is quite difficult to create multiple, identical environments. These environments could be completely similar or have slight variations. If you’re doing this the traditional way, that means going through all those configuration screens again and remembering what slight changes you wanted to make.

But, with Terraform, it’s as simple as a Ctrl + C and Ctrl + V of your code that defines your infrastructure. This makes it easy to make all the slight changes you want in each environment, while keeping the environments identical to each other in the long run.

See the big picture

When using Terraform, you’ll have your entire infrastructure written out in front of you as code. This makes it easier to understand which components are present and make any changes you see fit.

These are some of the key reasons you might want to use Terraform over the traditional method of creating infrastructure.

There are solutions, like AWS CloudFormation, available that does the same thing for you. However, Terraform is useful because it is not a Cloud provider-specific solution and, therefore, comes in handy regardless of your Cloud provider.

Getting our hands dirty!

Now that you have an idea of what Terraform does, let’s see it in action. We will keep it simple for this one and learn how to create an S3 bucket in AWS using Terraform. But, remember, you can create your entire infrastructure regardless of the cloud provider with Terraform.

Steps

  1. Sign in to AWS and create a programmatic user that Terraform can use to talk to AWS. Make sure to give this user enough access to create the S3 bucket.

Once you finish creating this user, make sure to note the Access key ID and the Secret access key.

  1. Once you’re done with this, go install Terraform for your OS and run:
terraform -version

to make sure you’ve installed it correctly.

Now, we need to set up the environment variables to give Terraform access to our AWS account. To do this, simply run the following commands in your terminal:

export AWS_ACCESS_KEY_ID=YOURACTUALKEYID

export AWS_SECRET_ACCESS_KEY=YOURACTUALACCESSKEY

And that’s it! Terraform now has access to talk to your AWS account.

Note: These two variables only exist for the lifetime of the current terminal window; that is, if you close the window, you’ll have to type the above commands again.

Now, let’s get to writing our infrastructure as code!

If you’re using VSCode, you might want to grab these extensions: HashiCorp Terraform and Terraform Autocomplete.

Steps

  1. Create a main.tf file and open it with your favorite code editor.

Note: it is not necessary to call this file main.tf, but it is considered a best practice.

  1. Copy the following contents in the file.
provider "aws" {
    region = "eu-west-1"
}

resource "aws_s3_bucket" "my_bucket" {
    bucket = "my-first-bucket" 
}

The first thing we did was specify our provider (AWS in our case). Terraform has a large number of providers that give Terraform access to provider-specific resources. When we chose the provider, we also specify the region we want to use for this project in AWS.

The next thing we did was set up our S3 bucket. For this, we defined a resource.

A resource is simply something that maps to an item in the real world. In our case, it maps to something in our cloud provider.

In the first set of quotes, we specified the resource we want to use, an AWS S3 bucket. In the second set of quotes, we gave this bucket an identifier. An identifier should only be used inside Terraform projects, and has nothing to do with what will get created in AWS by Terraform.

After this, we provided the configuration for this resource. Resources may have any number of parameters for you to configure. This configuration will be used to set the resource up just like you want in AWS. In this example, we just set the bucket name.

Now, go back to your command line where you set up your AWS keys. You should see these keys when you run something like:

printenv | grep AWS

From this terminal, navigate to the folder that has the main.tf file and run:

terraform init

This will configure Terraform to run with your project, and will initialize a state file. Next, run:

terraform apply

This will take your main.tf file and apply that to AWS. When you run that command, you will see a plan that Terraform creates first before actually making changes to AWS. This is so you can see what Terraform will be creating in your Cloud provider.

After showing you the plan, Terraform will ask for your permission to apply the plan. Type yes to continue and you should see a success message.

Now, go to your Amazon S3 console, and you’ll see your bucket created by Terraform.

Run terraform apply again and nothing will happen. This is because you asked Terraform to ensure that there is one particular S3 bucket, and if that is already created, it will do nothing. Therefore, running terraform apply again will not create a new bucket.

Delete the bucket from the AWS console, then run terraform apply again, and you’ll see a screen again where Terraform is asking you to confirm the creation of the S3 bucket. Say yes and you’ll see your bucket again in the AWS console.

There is one more important command we should go over. Run:

terraform destroy

You will now see another plan that will show all the things Terraform will delete in order to destroy everything that was created by the main.tf file. Enter yes and go to your console – you should see the bucket gone!

Thanks for reading! You can connect with me on Twitter if you’d like to have a chat.

RELATED TAGS

terraform
devops
kubernetes
aws
communitycreator

CONTRIBUTOR

Arsh Sharma
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring