Skip to main content

Getting Started

·1111 words·6 mins
Terraform - This article is part of a series.
Part 2: This Article

Step 1, you have decided to take on the challenge of the Terraform Associate Certification. You are an inspiration to all the Infrastructure as Code warriors out there! What is step 2? Probably you will have to install the terraform CLI and run a “Hello-World” style example to make sure that it works? This is the topic of this lesson.

This lesson covers a necessary prerequisite to take on the rest of the course, but it does not explicitly cover a specific section of the exam curriculum. So do not expect there to be questions on how to install the terraform CLI on the exam, but there will be plenty of questions on running various commands using the CLI.

Installing terraform CLI
#

Just reading about Terraform will not be enough for you to learn Terraform. You must also use it! The more you use it the easier the certification journey will be. To use Terraform you must first install it.

I am currently using a mac. I will assume that you are as well. Pretentious of me? I just like to keep things simple! If you are using Windows or Linux you can find official instructions here.

For all mac users out there you will be glad to hear that you can install Terraform using Homebrew. So without further ado, you can run the following commands to get started:

$ brew tap hashicorp/tap
$ brew install hashicorp/tap/terraform

The first command brew tap hashicorp/tap installs the Hashicorp repository (tap) where all Hashicorp-related packages are located. The second commands brew install hashicorp/tap/terraform installs the terraform CLI from the Hashicorp repository.

You can verify that it worked by running

$ terraform -version

Terraform v1.3.6
on darwin_arm64

Updating terraform CLI
#

Every now and then when you use terraform in your terminal you will receive a message like the following:

$ terraform -version

Terraform v1.3.1
on darwin_arm64

Your version of Terraform is out of date! The latest version
is 1.3.6. You can update by downloading from https://www.terraform.io/downloads.html

Then you know it is time to update your terraform CLI version! This is easy to do, just run the following command:

$ brew upgrade hashicorp/tap/terraform

Running `brew update --auto-update`...
...
==> Upgrading hashicorp/tap/terraform
  1.3.1 -> 1.3.6
...

Run a “Hello World” example
#

Now we have installed the CLI and we are ready to use it for something. This section covers a simple Hello-World example and will give you a preview of what is to come. I will not go into details of what is going on here, it will be fairly self-explanatory, and we will revisit everything covered here in future lessons.

In this Hello-World example I will create a local text file containing the words “Hello World”.

All Terraform projects consists of Terraform configuration files. A file containing Terraform code is usually referred to as configuration. Although I will use code interchangeably in this course, just like I did in the previous sentence.

Here is our Hello-World example where we see our first Terraform configuration:

// main.tf
terraform {
  required_providers {
    local = {
      source = "hashicorp/local"
    }
  }
}

resource "local_file" "file" {
  content  = <<EOF
Hello world
  EOF
  filename = "helloworld.txt"
}

The language of Terraform is actually called Hashicorp Configuration Language, or HCL. We will see a lot of HCL in this course. HCL is used in other products from Hashicorp as well, so it is not purely for Terraform.

I run terraform init to initialize my project:

$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/local...
- Installing hashicorp/local v2.2.3...
- Installed hashicorp/local v2.2.3 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

Now I execute Terraform by running terraform apply:

$ terraform apply -auto-approve

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # local_file.file will be created
  + resource "local_file" "file" {
      + content              = <<-EOT
            Hello world
        EOT
      + directory_permission = "0777"
      + file_permission      = "0777"
      + filename             = "helloworld.txt"
      + id                   = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.
local_file.file: Creating...
local_file.file: Creation complete after 0s [id=33ab5639bfd8e7b95eb1d8d0b87781d4ffea4d5d]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

If I check in my directory I see the following:

$ ls -go

total 24
-rwxr-xr-x@ 1    12 Dec 26 19:19 helloworld.txt
-rw-r--r--  1   196 Dec 26 19:18 main.tf
-rw-r--r--@ 1   861 Dec 26 19:19 terraform.tfstate

I see my main.tf file, holding the configuration I wrote earlier, and I can see two new files have appeared after I executed Terraform! First of all I see my new helloworld.txt file that I created using Terraform. I also see a terraform.tfstate file, that is a file holding the Terraform state for my project. We will see a lot more state in future lessons.

We should also make sure the helloworld.txt file contains the correct content:

$ cat helloworld.txt

Hello world

It does! Our Hello-World project has been successful and now it is time to run terraform destroy to end the life of our rendered project:

$ terraform destroy -auto-approve

local_file.file: Refreshing state... [id=33ab5639bfd8e7b95eb1d8d0b87781d4ffea4d5d]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # local_file.file will be destroyed
  - resource "local_file" "file" {
      - content              = <<-EOT
            Hello world
        EOT -> null
      - directory_permission = "0777" -> null
      - file_permission      = "0777" -> null
      - filename             = "helloworld.txt" -> null
      - id                   = "33ab5639bfd8e7b95eb1d8d0b87781d4ffea4d5d" -> null
    }

Plan: 0 to add, 0 to change, 1 to destroy.
local_file.file: Destroying... [id=33ab5639bfd8e7b95eb1d8d0b87781d4ffea4d5d]
local_file.file: Destruction complete after 0s

Destroy complete! Resources: 1 destroyed.

Summary
#

This was a brief lesson covering how to install the terraform CLI. We also ran a Hello-World example just to make sure Terraform works. We saw our first few terraform CLI commands

  • terraform init to initialize a project
  • terraform apply with the -auto-approve flag to execute Terraform and created the resources that were defined in our configuration
  • terraform destroy with the -auto-approve flag to destroy the resources Terraform created when we ran terraform apply

We also encountered the Hashicorp Configuration Language (HCL) for the first time.

In the next lesson we will talk a bit about how Terraform works at a high-level and how it compares to other similar tools.

Mattias Fjellström
Author
Mattias Fjellström
Cloud architect · Author · HashiCorp Ambassador
Terraform - This article is part of a series.
Part 2: This Article