Terraform Cookbook

5 (1 reviews total)
By Mikael Krief
  • Instant online access to over 8,000+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Setting Up the Terraform Environment

About this book

HashiCorp Configuration Language (HCL) has changed how we define and provision a data center infrastructure with the launch of Terraform—one of the most popular and powerful products for building Infrastructure as Code. This practical guide will show you how to leverage HashiCorp's Terraform tool to manage a complex infrastructure with ease.

Starting with recipes for setting up the environment, this book will gradually guide you in configuring, provisioning, collaborating, and building a multi-environment architecture. Unlike other books, you’ll also be able to explore recipes with real-world examples to provision your Azure infrastructure with Terraform. Once you’ve covered topics such as Azure Template, Azure CLI, Terraform configuration, and Terragrunt, you’ll delve into manual and automated testing with Terraform configurations. The next set of chapters will show you how to manage a balanced and efficient infrastructure and create reusable infrastructure with Terraform modules. Finally, you’ll explore the latest DevOps trends such as continuous integration and continuous delivery (CI/CD) and zero-downtime deployments.

By the end of this book, you’ll have developed the skills you need to get the most value out of Terraform and manage your infrastructure effectively.

Publication date:
October 2020
Publisher
Packt
Pages
366
ISBN
9781800207554

 
Setting Up the Terraform Environment

Before you start writing the Terraform configuration file, it is necessary to install and configure a local development environment. This development environment will allow Terraform's configuration file to be written and validated as it is developed.

In the recipes in this chapter, we will learn how to download and install Terraform manually on a Windows machine, as well as how to install it using a script on Windows and Linux. We will also learn how to use Terraform in a Docker container before learning how to migrate the Terraform configuration written in version 0.11 to version 0.13.

In this chapter, we'll cover the following recipes:

  • Downloading and installing Terraform manually

  • Installing Terraform using a script on Linux

  • Installing Terraform using a script on Windows

  • Executing Terraform in a Docker container

  • Writing Terraform configuration in VS Code

  • Migrating your Terraform configuration to Terraform 0.13

Let's get started!

 

Technical requirements

This chapter does not require that you have any specific technical knowledge. We will mainly use graphical user interfaces and simple Linux and Windows scripts. However, knowledge of Docker is also recommended so that you can complete the Executing Terraform in a Docker container recipe. 

Finally, for the IDE, we will use Visual Studio Code, which is available for free at https://code.visualstudio.com/.

The source code for this chapter is available at https://github.com/PacktPublishing/Terraform-Cookbook/tree/master/CHAP01.

Check out the following video to see the code in action: https://bit.ly/3h9noXz

 

Downloading and installing Terraform manually

In this recipe, we will learn how to download and install Terraform on a local machine under a Windows operating system.

 

Getting ready

To complete this recipe, the only prerequisite is that you're on a Windows operating system.

 

How to do it…

Perform the following steps:

  1. Open Windows File Explorer. Choose a location and create a folder called Terraform. We will use this to store the Terraform binary; for example, C:/Terraform.

  2. Launch a web browser and go to https://www.terraform.io/downloads.html.

  3. Scroll down the page until you reach the package for Windows:

  1. Click on the 64-bit link, which targets the Terraform ZIP package for the Windows 64-bit operating system. The package will be downloaded locally.

  2. Unzip the content of the downloaded ZIP file into the Terraform folder that we created in step 1:

The last thing we need to do to install Terraform is configure the Path environment variable by adding the path of the Terraform binary folder.

To do this, follow these steps:

  1. In File Explorer, right-click on the This PC menu and choose Properties:

  1. Click on the Advanced system settings link and click the Environment variables button of the newly opened window:

  1. When provided with a list of environments, select User variables or Systems variables (choose this option to apply the environment variable to all users of the workstation), and select the Path variable. Then, click on the Edit button:

  1. From the list of paths, add the folder we created; that is, C:\Terraform\:

Finally, we validate all the open windows by clicking on the OK button, which is present at the bottom of every open window.

 

How it works…

Downloading and installing Terraform is simple, and adding the path of the Terraform binary to the PATH environment variable makes it possible to execute the Terraform command line from any terminal location.

After completing all these steps, we can check that Terraform is working properly by opening a command-line terminal or PowerShell and executing the following command:

terraform --help

The result of executing the preceding command is shown in the following screenshot:

By doing this, the list of Terraform commands will be displayed in the terminal.

 

Installing Terraform using a script on Linux

In this recipe, we will learn how to install Terraform on a Linux machine using a script.

 

Getting ready

To complete this recipe, the only prerequisites are that you are running a Linux operating system and that you have an unzip utility installed. The gpg, curl, and shasum tools must be installed; they are often installed by default on all Linux distributions.

 

How to do it…

Perform the following steps:

  1. Open a command-line terminal and execute the following script:

TERRAFORM_VERSION="0.12.29"

curl -Os https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
&& curl -Os https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_SHA256SUMS \
&& curl https://keybase.io/hashicorp/pgp_keys.asc | gpg --import \
&& curl -Os https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_SHA256SUMS.sig \
&& gpg --verify terraform_${TERRAFORM_VERSION}_SHA256SUMS.sig terraform_${TERRAFORM_VERSION}_SHA256SUMS \
&& shasum -a 256 -c terraform_${TERRAFORM_VERSION}_SHA256SUMS 2>&1 | grep "${TERRAFORM_VERSION}_linux_amd64.zip:\sOK" \
&& unzip -o terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d /usr/local/bin
The source code for this script is also available in this book's GitHub repository: https://github.com/PacktPublishing/Terraform-Cookbook/blob/master/CHAP01/install_terraform_linux.sh
  1. After executing this script, we can check the installation of Terraform by executing the following command:

terraform --version

The preceding command displays the installed version of Terraform, which means we can check that Terraform is correctly installed and that the desired version is installed.

 

How it works…

In this script, in the first line, the TERRAFORM_VERSION variable is filled in with the Terraform version that we want to install. This variable is only mentioned here since we don't want to keep repeating the version of Terraform we're using throughout the script.

In this recipe, the 0.12.29 version of Terraform is used, but we are free to modify this.

With the curl tool, the script downloads the ZIP file containing the Terraform binary. Then, the script checks the security integrity of the packaged. This is called shasum.

In the last line, the script unzips the downloaded package inside the local directory, /usr/local/bin, which is already mentioned by default in the PATH environment variable.

You can check that the version of Terraform you have installed corresponds to the one mentioned in the script by executing the following command:

terraform --version

This command displays the installed version of Terraform, as shown in the following screenshot:

As we can see, here, the version of Terraform we have is 0.12.29.

 

There's more…

In this Terraform installation script, we have specified the version number of Terraform to be installed.

If you want to install the latest version without having to know the version number, it is also possible to dynamically retrieve the version number of the latest version using the following API: https://checkpoint-api.hashicorp.com/v1/check/terraform. This retrieves information about the current version of Terraform.

The following screenshot shows our current version:

Note that the current_version property, which contains the latest Terraform version, is a value.

With this API, we can perfectly modify the first line of the installation script with the following code:

TERRAFORM_VERSION=$(curl -s https://checkpoint-api.hashicorp.com/v1/check/terraform | jq -r .current_version)

....

The complete script for installing Terraform with this block is available at https://github.com/PacktPublishing/Terraform-Cookbook/blob/master/CHAP01/install_terraform_linux_v2.sh.

This block of code uses the curl command to retrieve the API return and parses its contents to retrieve the value of the current_version property with the jq tool (available at https://stedolan.github.io/jq/).

In addition, the terraform --version command shows whether the latest version is installed. If you have installed an old version, this command displays a message indicating the latest version:

Here, we can see that we have installed version 0.12.28 and that the latest version is 0.12.29 (at the time of writing this recipe).

Finally, HashiCorp announced that the Terraform binary will soon be available in the Linux package manager. For more information, take a look at the following article: https://www.hashicorp.com/blog/announcing-the-hashicorp-linux-repository

 

See also

 

Installing Terraform using a script on Windows

In this recipe, we will learn how to install Terraform on a Windows machine using a script that uses the Chocolatey software package manager.

 

Getting ready

To complete this recipe, you'll need to be using a Windows operating system and have Chocolatey (https://chocolatey.org/) installed, which is a Windows software package manager.

If you don't have Chocolatey installed, you can easily install it by following these steps:

  1. Open a PowerShell terminal in administrator mode, as shown in the following screenshot:

  1. Then, execute this following script in the terminal:

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
The complete installation documentation for Chocolatey is available at https://chocolatey.org/install.
 

How to do it…

Perform the following steps:

  1. Open a PowerShell command-line terminal in administrator mode.

  2. Execute the following command:

choco install -y  terraform

The following screenshot shows the execution of this command:

The -y option is optional. It allows us to accept the license agreement automatically.

 

How it works…

 

There's more…

When upgrading Terraform, it is possible to upgrade it directly with Chocolatey by executing the choco upgrade -y terraform command.

By default, the choco install command installs the latest version of the mentioned package. It is also possible to specify a specific version by adding the --version option to the command, which in our case would give us, for example, the following:

choco install -y terraform --version "0.12.28"

In this example, we have specified that we want to install version 0.12.28 of Terraform and not the latest version.

Be aware that the Terraform package from Chocolatey can have a time lag regarding the latest official version of Terraform, while in the Linux script, as shown in the Installing Terraform on Linux recipe, you can specify the latest version that has just been released.
 

See also

 

Executing Terraform in a Docker container

In the previous recipes of this chapter, we discussed how to install Terraform locally, either manually or via a script, depending on the local operating system.

In this recipe, we will learn how to run Terraform in a Docker container, which will allow us to enjoy the following benefits:

  • There is no need to install Terraform locally.

  • We can have a Terraform runtime environment independent of the local operating system.

  • We can test our Terraform configuration with different versions of Terraform.

Let's get started!

 

Getting ready

To complete this recipe, you'll need to know about Docker and its commands, as well as how to write Dockerfiles. Please read the documentation for more information: https://docs.docker.com/get-started/overview/

On our local computer, we installed Docker using a tool called Docker Desktop for Windows.

For Docker installation guides for other operating systems, please read the Docker installation documentation at https://docs.docker.com/get-docker/.

We also have a Terraform configuration file already written, which will not be detailed here. This will be executed in our Docker container.

You will also need the respective Terraform commands, init, plan, and apply, which will not be explained in the context of this recipe.

 

How to do it…

Perform the following steps:

  1. At the root of the folder that contains the Terraform configuration, we need to create a Dockerfile that contains the following code:

FROM golang:latest
ENV TERRAFORM_VERSION=0.13.0
RUN apt-get update && apt-get install unzip \
&& curl -Os https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
&& curl -Os https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_SHA256SUMS \
&& curl https://keybase.io/hashicorp/pgp_keys.asc | gpg --import \ && curl -Os https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_SHA256SUMS.sig \
&& gpg --verify terraform_${TERRAFORM_VERSION}_SHA256SUMS.sig terraform_${TERRAFORM_VERSION}_SHA256SUMS \
&& shasum -a 256 c terraform_${TERRAFORM_VERSION}_SHA256SUMS 2>&1 | grep "${TERRAFORM_VERSION}_linux_amd64.zip:\sOK" \
&& unzip -o terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d /usr/bin
RUN mkdir /tfcode
COPY . /tfcode
WORKDIR /tfcode
  1. Next, we need to create a new Docker image by executing the docker build command in a terminal:

docker build -t terraform-code:v1.0 .
  1. Then, we need to instantiate a new container of this image. To do this, we will execute the docker run command:

docker run -it -d --name tfapp terraform-code:v1.0 /bin/bash
  1. Now, we can execute the Terraform commands in our container by using the following commands:

docker exec tfapp terraform init
docker exec tfapp terraform plan
docker exec tfapp terraform apply --auto-approve

The following screenshot shows a part of the output of executing these commands (terraform plan):

 

How it works…

In step 1, we write the composition of the Docker image in the Dockerfile. We do this as follows:

  1. We use a Golang base image.

  2. We initialize the TERRAFORM_VERSION variable with the version of Terraform to be installed.

  3. We write the same Terraform installation script we wrote in the Installing Terraform on Linux recipe. 

  4. We copy the Terraform configuration from our local file into a new folder located in the image.

  5. We specify that our workspace will be our new folder.

Then, in steps 2 and 3, we create a Docker terraform-code image with a v1.0 tag. This tag is used to version our Terraform configuration. Then, we create a tfapp instance of this image, which runs with the bash tool.

Finally, in step 4, in the tfapp instance, we execute the Terraform commands in our container workspace.

 

There's more…

In this recipe, we studied how to write, build, and use a Docker image that contains the Terraform binary. With this, it is possible to complete this image with other tools such as Terragrunt, which are also used to develop the Terraform configuration file.

If you want to use just Terraform, you can use the official image provided by HashiCorp. This is public and available on Docker Hub at https://hub.docker.com/r/hashicorp/terraform/.

 

See also

 

Writing Terraform configuration in VS Code

Writing a Terraform configuration file does not require a special code editor. However, popular code editors have adapted and now offer plugins that simplify writing such a file.

In this recipe, we will focus on Visual Studio Code, which has the following benefits:

  • It's cross-platform, which means it can be installed on Windows, Linux, and macOS.

  • It's free of charge.

  • It has a multitude of extensions that cover all the needs of developers on a daily basis.

In this recipe, we will learn how to configure Visual Studio Code so that we can write the Terraform configuration. We'll also see how much faster it is to write code in this way.

 

Getting ready

For this recipe, you need to have Visual Studio Code installed on your local machine. You can install it by going to https://code.visualstudio.com/.

 

How to do it…

To use Visual Studio Code with Terraform, we need to install the respective extension and configure it.

To install this extension, perform the following steps:

  1. Open Visual Studio Code and click on the extension tab. This can be found on the sidebar, on the left-hand side of the editor, as shown in the following screenshot:

  1. Then, we search for the extension by using the Terraform keyword.

  1. Install the first extension of the list, called Terraform. This is published by HashiCorp. Do this by clicking on the Install button of the extension:

  1. Reload Visual Studio Code to apply the extension.

 

How it works…

In the first part of this recipe, we installed the Terraform extension for Visual Studio Code.

When searching for Terraform extensions, several will appear, but the one we chose is one of the most powerful in my opinion.

Once installed, this extension offers a lot of functionalities for editing Terraform's configuration, such as autocompletion, validating the configuration, syntax for tflint, code formatting, links to the official documentation, and a module explorer.

This component allows Visual Studio Code to function better with Terraform 0.12  at least when it comes to the extension's functionalities.

 

There's more…

Once the extension has been installed and configured, we can write the Terraform configuration in our main.tf file. Here, we have some very useful features we can use to develop the Terraform configuration, some of which are as follows:

  • Syntax highlighting:

  • Autocompletion for resources and properties:

  • Real-time code validation:

  • The ability to display the number of references, along with a link to view them:

 

See also

 

Migrating your Terraform configuration to Terraform 0.13

Officially released in May 2019, version 0.12 of Terraform has brought many new features to the language, but also changes, and during this summer 2020, the new Terraform version 0.13 has been released, also providing new features and some changes.

We must take these changes into account before we upgrade the code.

In this recipe, we will discuss how to verify that our Terraform configuration is compatible with version 0.12. After that, we will learn how to migrate our Terraform configuration from version 0.11 to version 0.12 and then to version 0.13.

 

Getting ready

Before migrating your code  from version 0.11 to the latest version (currently 0.13), you will need to have code that works with the latest version of Terraform 0.11, which is 0.11.14. You can download this version from https://releases.hashicorp.com/terraform/0.11.14/.

It is important to know that if your Terraform configuration is in version 0.11, it is not possible to migrate it directly to 0.13. You will first have to upgrade to 0.12 and then migrate to 0.13.

In addition, before any migration, it is strongly advised to read the upgrade documentation provided by HashiCorp (here for 0.12, https://www.terraform.io/upgrade-guides/0-12.html, and here for 0.13, https://github.com/hashicorp/terraform/blob/master/website/upgrade-guides/0-13.html.markdown). This is because in the upgrade process, as we will study in this recipe, many elements are migrated automatically, but others will have to be done manually.

Finally, it is also recommended by HashiCorp, before performing the migration process, to commit its code in a source code manager (for example, Git) in order to be able to visualize the code changes brought by the migration.

The code source (version 0.11) used for this recipe is available here: https://github.com/PacktPublishing/Terraform-Cookbook/tree/master/CHAP01/tf011

 

How to do it…

The upgrade of the Terraform configuration from version 0.11 to version 0.13 takes place in two steps, First the code has to be migrated to 0.12, and then be migrated to 0.13.

To migrate our  Terraform configuration from version 0.11 to version 0.12, perform the following steps:

Before migrating this configuration to version 0.12, we must verify that it is compatible with this version. To do this, follow these steps:

  1. Using Terraform 0.11.14, in a terminal, execute the following command:

terraform 0.12checklist

The following screenshot shows the output of executing the preceding command:

As we can see, our Terraform configuration is compatible with Terraform version 0.12. Now, we can migrate it.

  1. Next, we need to install the latest version of Terraform 0.12 manually, as described in the Downloading and installing Terraform manually recipe. We can also do this via a script, according to our operating system, as shown in the Installing Terraform on Linux and Installing Terraform using a script on Windows recipes.

  2. In the folder that contains our code, we execute the init command:

terraform init
  1. Then, we execute the following command:

terraform 0.12upgrade
  1. Finally, we confirm the migration by answering yes when prompted to enter a value, as shown in the following screenshot:

Then, to migrate our Terraform configuration from version 0.12 to version 0.13, perform the following steps:

  1. Download and install the latest version of Terraform 0.13.

  2. Exactly as before for 0.12, run the terraform 0.13upgrade command to upgrade the configuration to 0.13:

 

How it works…

In step 1, we verified that our Terraform configuration is compatible with the language evolution (HCL 2) included in Terraform 0.12.

Then, we installed Terraform 0.12 locally and started the migration process by executing the terraform init command, which is necessary to download the different providers that will be called in our code.

We migrated the Terraform configuration to the 0.12 version using the terraform 0.12upgrade command, which upgrades the Terraform configuration directly.

Finally, to upgrade the Terraform configuration to the 0.13 version, we installed the Terraform 0.13 binary and executed the command terraform 0.13upgrade.

 

There's more…

Please note that the migration procedure only changes the current Terraform configuration. If our Terraform configuration calls for modules, it is necessary to migrate the code of the modules beforehand.

 

See also

For more information on the migration procedure for Terraform to version 0.12, please refer to the technical documentation at https://www.terraform.io/upgrade-guides/0-12.html.

To find out more about the evolution of Terraform and the changes that were made in this new major version, take a look at the following documentation and related articles:

Finally, the following is the official repository for Terraform: https://github.com/hashicorp/terraform-guides/tree/master/infrastructure-as-code/terraform-0.12-examples. It contains multiple code examples for Terraform 0.12, all of which we will look at throughout this book.

Regarding the Terraform 0.13, the upgrade guide is available here – https://github.com/hashicorp/terraform/blob/master/website/upgrade-guides/0-13.html.markdown, and the change log is available here – https://github.com/hashicorp/terraform/blob/master/CHANGELOG.md.

About the Author

  • Mikael Krief

    Mikael Krief lives in France and works as a DevOps engineer. He came to believe that Infrastructure as Code is a fundamental practice in DevOps culture. He is therefore interested in HashiCorp products and specializes in the use of Terraform in several company contexts. He loves to share his passion through various communities, such as the HashiCorp User Groups. In 2019, he wrote the book Learning DevOps (Packt Publishing), and he also contributes to many public projects, writes blogs and other books, and speaks at conferences. For all his contributions and passion, he was nominated and selected as a HashiCorp Ambassador, and he has been awarded the Microsoft Most Valuable Professional (MVP) award for 5 years.

    Browse publications by this author

Latest Reviews

(1 reviews total)
Very good and easy to use

Recommended For You

Book Title
Access this book, plus 8,000 other titles for FREE
Access now