Introduction to AWS With Terraform

Disclaimer

Aymen El Amri
HackerNoon.com
Published in
11 min readApr 10, 2018

--

This content is part of / inspired by one of our online courses/training. We are offering up to 80% OFF on these materials, during the Black Friday 2019.

You can receive your discount here.

Almost one month ago, I released Practical AWS, a training concerned With the actual use of AWS rather than with theory & ideas.

You can watch the demo video, or visit the training website.

More information on http://practicalaws.com

Even if the training is released, but since it will be lifetime updated with new contents for free, this blog post is a prototype of the new lesson that will be added to the training.

This blog post is an introduction to managing an AWS infrastructure using Terraform.

Downloading & Installing Terraform

Start by downloading Terraform from the official download page .

Choose your OS and CPU architecture and start the download.
Terraform is a single binary that you should move to /usr/bin and make it executable.

If you are using another OS, please refer to the documentation .

Configuring AWS

In order to follow the best practices, let’s create a user for Terraform. Go to your AWS console and create terraform_user user:

Give it the good rights. In my example, I need Terraform to be able to manage all of my AWS Cloud resources:

Don’t forget to store the AWS access key id and secret access key:

Copy them in your AWS credential file:

You can also execute aws configure to add a new user.

In both cases, your keys will be stored in the AWS credentials file:

Terraform Hello World !

Go to your workspace and create a folder called terraform:

Add these lines to main.tf :

The above is the configuration for AWS, adapt the credential file path to your own configuration, the profile name, as well as the region. In my example, I am using the Paris region.

In order to create our first AWS machine, let’s add these lines:

Our file main.tf will look like this:

In the example above, I am creating a machine on the region “eu-west-3” using the profile “terraform”.

My machine size is t1.micro and it is using the AMI ami-0e55e373 , which is a Ubuntu 17.04 image available for the region “eu-west-3”.

Note : Ubuntu 17.04 image doesn’t have the same AMI id in two different regions.

If you prefer using Ubuntu like in this example, you can visit cloud-images.ubuntu.com where you can find the id of the AMI you should use.

You can also use the CLI in order to describe AWS IAMs:

  • e.g: Describing Windows AMIs that are backed by Amazon EBS.
  • e.g: Describing Ubuntu AMIs

Note: Check the AWS cheat sheet that comes with this training in order to get more examples.

After choosing the AMI, go into the folder where you created main.tf and initialize Terraform:

You will see a similar output to this one:

Now execute:

This command will not create any resource on your AWS cloud. It lets you know what Terraform will do.

You will see this output:

Note that the (+) sign indicates that a resource will be created. In the other hand, when showing a minus sign (-), Terraform means that a resource will be deleted.

Working With Variables

Let’s discover how to use Terraform variables to write a cleaner configuration file.

We can consider that the AWS region could be variable, that’s why we are going to add this code to the main.tf file:

You can now call it from a Terraform file using:

This how our main.tf will look like:

Right ! Let’s do the same thing to “shared_credentials_file” and “profile”:

Using Terraform Maps

In my example, I am using the Paris region (eu-west-3) but what if I need to add new regions like Dublin (eu-west1) for instance !?

The above code will deploy an EC2 instance to a single region.

In order to seolve this problem, the first step to follow here, is finding the AMI we want to use (depending on the region) and then create a variable with the type “map”:

According to the used region Terraform should create an EC2 machine with a different AMI.

This is done by changing the old AMI line by changing ami = "ami-0e55e373" to ami = "${lookup(var.my_ami, var.region)}" .

To test this, type terraform plan and you will get this output:

If you manually change the region to eu-west-1 , you will notice that terraform plan will use the other AMI:

Using Input Arguments

In the latest example we changed the value of the region from “eu-west-3” to “eu-west-1” manually. The goal was testing if the map function was working right.

In practice, you don’t need to manually change your main.tf file, but you can override the value of region by using a new region as an argument:

Try using “eu-west-1” instead of the default value “eu-west-3”:

It is possible to input other variables in the same line. As an example, we can change the used profile from “terraform” to “default” using the following command:

Using Variable Files

We want to separate the configuration from the execution code, that’s why we are going to create a file containing the variables we are using (variables.tfvars):

After removing the variables from the main.tf file, this is how it becomes:

Let’s execute the plan command to see if there the EC2 machine will be created:

Terraform Apply

In order to execute create our EC2 machine, we need to execute: terraform apply .
Because we are using a file to store our variables, we need to execute:

After executing the command above, we will have a similar output to this one:

You will be asked to confirm, enter “yes”:

Terraform & Immutable Infrastructure

To simply define this concept, an immutable resource or component is replaced for every deployment. For instance, servers are never modified after the deployment. When an updated is needed, a new server should be created from a base/common image with the new updates.

In order to see this in practice, I made it explicit to forget adding the SSH key to the EC2 description file, without it you can create an EC2 machine but you can’t access it using SSH. Let’s now add a key pair to the EC2 machine:

In the main.tf file:

In variables.tfvs file:

Now you can execute the plan command than the apply command and you will notice that Terraform will not update the machine to add the new key but will destroy it and create a new one with a new configuration:

Using Terraform Modules

Terraform hots a public registry where you can find common reusable modules.

You can use this registry modules for your projects. Some of them are verified by HashiCorp, the company behind Terraform.

In order to put this in practice, we are going to do the same operation(creating an EC2 machine), but using this module .

Create a new file (for example: main2.tf) and add these lines:

Now type terraform init and the module files will be downloaded. You can use the plan then the apply command.

Connect Deeper

In this tutorial, we started manipulating Terraform with AWS but this is an introduction and it will be extended in Practical AWS online training .

If you are interested in Practical AWS training, you can make an order and start learning AWS right now.

You can also download my mini ebook 8 Great Tips to Learn AWS.

--

--