Learning Terraform

I’ve been spending some time lately learning about HashiCorp’s Terraform product and thought I would share my journey so far… 

What is Terraform

Terraform is an open source Infrastructure-as-code (IaC) orchestration tool for building, changing, managing and versioning infrastructure.  Terraform is vendor agnostic and integrates with several service providers and can be used on-premises as well.  Terraform uses an easy to read declarative syntax known as HashiCorp Configuration Language (HCL) to configure and provision infrastructure. 

To build and deploy infrastructure, Terraform uses configuration files with a .tf extension.  Terraform configuration files are declarative, i.e. the code in the Terraform file defines the desired end state.  Terraform uses providers and resources within the configuration file to specify the desired end state.

Provider

Infrastructure providers (Azure, AWS, GCP, etc.) are designated as providers in Terraform.  Providers are responsible for managing resources and API integrations with the infrastructure provider.  Providers are declared with the ‘provider’ keyword in the provider block.

Provider block example for Azure
Provider block example for AWS

Terraform supports version constraints which allows users to explicitly set provider versions. This constraint is set in the provider block.

This prevents Terraform from installing a new version of the provider which may have breaking changes.  Information regarding changes to Terraform providers can be found in the Terraform provider GitHub repository.  Browse to the specific provider being used and click on Releases to view the changes for each version.

Resource

Terraform resources describe infrastructure exposed by the infrastructure provider including compute resources, virtual networks and DNS records.  Resources are declared with the ‘resource’ keyword in the in the resource block.

Resource block example

The Terraform syntax consists of the following elements:

Blocks: 

Blocks can be thought of as a container that represents the configuration being applied.  They consist of the block type and block label.

The following example is a block used to create an Azure Resource Group.

The following example is a block used to create an AWS EC2 Instance.

Arguments:

Arguments appear within blocks and assign a value to a name.

Expressions:

Expressions are used to reference or compute values within a Terraform configuration file.  Expressions can be used for assigning simple literal values, referencing data in other resources, performing mathematical computations and evaluating conditional statements.  You can learn more about Terraform expressions in the Terraform documentation.

Terraform commands

Terraform uses a command-line interface (CLI) to execute commands.  All Terraform commands begin with terraform.  The terraform command is then followed by a subcommand such as init or plan.

Basic Terraform commands:

$ terraform init

This is the first command to run when using Terraform.  The terraform init command initializes the current working directory that will contain the Terraform configuration files and downloads any specific binaries for the provider specified. 

$ terraform plan

This is the second command to run when using Terraform.  This command will read the Terraform configuration file and create an execution plan.  The Execution plan includes all the changes the Terraform will make to your provider environment.

$ terraform apply

This is the third command to run when using Terraform.  This command will apply the changes that were described when the terraform plan was run.

$ terraform destroy

This command will delete all the resources created by the Terraform configuration file

Other Commands

There are several other commands available in Terraform.  To see the full list of commands, type $ terraform -h at the command line or browse to the Terraform documentation.

Next Steps

  1. Download and Install the appropriate version of Terraform.
  2. Follow the installation instructions on the HashiCorp website for either Windows, Mac or Linux
  3. Get started provisioning infrastructure with Azure, AWS or Google Cloud Platform

Summary

Terraform is an infrastructure orchestration tool that supports several infrastructure providers and has an easy to learn syntax that can be used to describe infrastructure across multiple platforms.  Terraform can also integrate with configuration management tools like Ansible, Chef or Puppet.  In future blog posts, I’m looking forward to continuing to dive deeper into Terraform.

Thank you for reading!