Link Search Menu Expand Document

Terraform

Lesson 4

Learn the Cloud uses Terraform as a basis for building resources in the AWS Cloud. Terraform is an Infrastructure as Code tool used to provision and manage any cloud, infrastructure, or service. You can read more about Terraform on their website. While there are many Infrastructure as Code tools available, Terraform is currently the most popular and marketable to have under your belt.

As you move throughout this course, terraform will appear frequently. Your resources and projects will all be deployed through terraform, and you’ll do a lot of learning on the go. Don’t ever feel as if you can only do a section or lesson once. We will always include instructions on how to start from scratch so you can practice again and again until you are comfortable with the material. This is especially important in the beginning when you are being introduced to so much new information. For example, in EC2 when you launch your first instance in terraform, you’ll also be learning about project structure, terraform code blocks, and how to move between your code editor and terminal. Try not to feel too overwhelmed and go at a pace that works for you!

Table of contents

  1. Terraform
  2. What you’ll learn
  3. Install Terraform
    1. Linux – Ubuntu/Debian OS
    2. MacOS
    3. Windows
  4. Your first Terraform project
    1. Terraform code
      1. The provider { } block
      2. The resource { } block
    2. terraform init
    3. terraform apply
    4. Go find your AWS instance!
  5. Next Steps
    1. Redo the lesson (optional)
    2. Move On

What you’ll learn

In this section, you’ll download and install terraform and create your first terraform project! Don’t worry if you don’t understand exactly what you are creating in AWS yet. The important thing is learning how to use terraform in the terminal to create and deploy projects.

We will be building our instructions below from the Getting Started with Terraform Guide that Terraform offers. We suggest you watch the introduction video from the link above to familiarize yourself with terraform on a high-level. Do not feel as if you must remember everything.

If you would rather, you may view Terraform’s instructions directly by visiting the link above and following along through all of the getting started material Make sure to choose AWS as your infrastructure of choice when given an option!

Install Terraform

Perhaps the easiest way to install terraform is via the command line (your terminal). For other options, visit Terraform’s Installation Guide directly.

Linux – Ubuntu/Debian OS

Instructions

Paste each command into your terminal from your home directory, one at a time, in order:

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform

Verify the installation worked by closing your terminal and opening a new terminal window, then paste:

terraform -help

This command will list the subcommands terraform offers. You should see something like this:

terraform-help

MacOS

Instructions

We will walk you through how to install Terraform using Homebrew. If you do not have Homebrew installed, visit the previous link for instructions on how to install it, or paste the below command into your terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Then, paste each command below into your terminal from your home directory, one at a time, in order:

brew tap hashicorp/tap
brew install hashicorp/tap/terraform
brew upgrade hashicorp/tap/terraform

Verify the installation worked by closing your terminal and opening a new terminal window, then paste:

terraform -help

This command will list the subcommands terraform offers. You should see something like this:

terraform-help

Windows

For installation instructions on Windows, please see Terraform’s direct instructions here.

Your first Terraform project

Now it’s time to try out your first terraform project! Remember, you don’t have to be an AWS and terraform pro by the end of this section. You are just using this as a “jumping off point” for your journey!

In the terminal, use cd to change into your home directory, if you aren’t already there. You can also check with the command pwd, which will print the directory you are currently in.

Once in your home directory, paste the command below, which will create a directory called learn-terraform-aws-instance:

mkdir learn-terraform-aws-instance

Next, change into the directory you just made:

cd learn-terraform-aws-instance

Create a file for your example code (which will tell AWS what resource you want to deploy):

touch example.tf

Go to your code editor, open up the example.tf file you just created, and paste the below code into it, then save.

provider "aws" {
  version = "~> 3.0"
  region  = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-0947d2ba12ee1ff75"
  instance_type = "t2.micro"
}

**********example-terraform************

Terraform code

Blocks are the lines of code you see that seem separate from each other. In this example, we have two blocks:

  • The provider { } block
  • The resource { } block

Blocks are opened and closed with curly brackets { }. Notice in each block for every open bracket: { there is eventually a closed bracket: }.

The provider { } block

From Terraform’s website:

The provider { } block configures the named provider, in our case aws, which is responsible for creating and managing resources. A provider is a plugin that Terraform uses to translate the API interactions with the service. A provider is responsible for understanding API interactions and exposing resources. Because Terraform can interact with any API, you can represent almost any infrastructure type as a resource in Terraform.

The profile attribute in your provider block refers Terraform to the AWS credentials stored in your AWS Config File, which you created when you configured the AWS CLI. HashiCorp recommends that you never hard-code credentials into *.tf configuration files. We are explicitly defining the default AWS config profile here to illustrate how Terraform should access sensitive credentials.

Note: If you leave out your AWS credentials, Terraform will automatically search for saved API credentials (for example, in ~/.aws/credentials) or IAM instance profile credentials. This is cleaner when .tf files are checked into source control or if there is more than one admin user.

The resource { } block

From Terraform’s website:

The resource { } block defines a piece of infrastructure. A resource might be a physical component such as an EC2 instance, or it can be a logical resource such as a Heroku application.

The resource block has two strings before the block: the resource type and the resource name. In the example, the resource type is aws_instance and the name is example. The prefix of the type maps to the provider. In our case “aws_instance” automatically tells Terraform that it is managed by the “aws” provider.

The arguments for the resource are within the resource block. The arguments could be things like machine sizes, disk image names, or VPC IDs. Our providers reference documents the required and optional arguments for each resource provider. For your EC2 instance, you specified an AMI for Ubuntu, and requested a t2.micro instance so you qualify under the free tier.

terraform init

Every time you create a new terraform project, or add a module to an existing terraform project, you’ll need to run:

terraform init

You’ll see a message saying the initialization was successful, like below.

terraform-init

terraform apply

When you are ready to apply your terraform (or, deploy your resources to AWS), you’ll run:

terraform apply

You’ll see terraform’s plan for what to build. In this case, terraform is only planning to build a single EC2 instance (a server hosted in AWS).

Respond with yes.

terraform-apply

Go find your AWS instance!

Wait for terraform to finish creating your EC2 instance (you’ll get a green Apply complete! message), then login to the AWS console here and see if you can find your EC2 instance!

  1. Click Services in the top right hand corner of the console after you’ve logged in.
  2. Search for EC2 and click the result from the dropdown.
  3. If you’ve followed along here, your instance should be in the N. Virginia (or us-east-1) region. In the upper right-hand corner, between your username/account name and Support you should see a region drop down. If it doesn’t already say N. Virginia, you need to select it from the dropdown.
  4. Then, underneath the Resources section of EC2, click on the Instances (running) link.
  5. You should see an instance here!

first-instance

Next Steps

You have now completed your first terraform project! Don’t worry if you don’t have everything memorized, but know that you can always redo this section as many times as you need to get the hang of it.

Redo the lesson (optional)

If you’d like to redo this lesson, in the terminal you need to change directories to the one above learn-terraform-aws-instance using the command:

cd ..

Then, you need to delete the directory (do you remember the command?):

rm -rf learn-terraform-aws-instance

You now have a fresh start and can begin this Lesson again!

Move On

Now that you have finished the getting start project with terraform and have an idea of how it works, you can move out of the Getting Started section! Head over to EC2.