DEV Community

Cover image for What the H*ck is Terraform: An Introduction
Brandon Damue
Brandon Damue

Posted on

What the H*ck is Terraform: An Introduction

I have been absent from sharing my learnings for quite some time. My first article of 2025 is also my first after a four month writing hiatus. Isn’t it a nice coincidence that my first article of the new year is on a new technology (new in the sense that I haven’t done much with it before) that I am currently diving deep into? If you are new to Terraform as well, don’t worry because I got you. The aim of this article is to give you a soft-landing introduction to Terraform. Let’s glide into it!

Terraform is an open-source, cloud-agnostic (meaning you can use Terraform to provision and manage infrastructure on different cloud platforms) infrastructure as code tool that enables you to safely and predictably provision and manage infrastructure in the cloud.

If you’ve ever had to do a task such as creating a couple of Amazon EC2 instances with security groups and other configurations by having to click around in the AWS management console, you can attest to how tedious that might have been. Terraform makes carrying out such tasks a breeze. Before we go any further, here are some of the benefits of using Terraform to keep you interested and inspire you to consider using it the next time you are building out infrastructure in the cloud. These benefits include:

  • Terraform templates can be source controlled that is you can use GitHub, BitBucket and related technologies to keep track of the different versions of your Terraform templates.

  • It offers multi-cloud support (cloud-agnostic) with over 100 providers including Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP)

  • Has separate plan and apply stages so you can verify changes to your infrastructure before they happen (more on this later).

  • Offers a public registry of modules that make provisioning common groups of resources easy.

  • It is stateful and keeps track of all infrastructure it provisions.

Now it’s time to dive deeper. In this section of the article, we will explore Terraform’s CLI tool, Terraform Configuration, the HashiCorp Configuration Language (HCL) and Terraform providers.

Terraform CLI Tool

Terraform is written in GoLang and packaged as a single binary which makes installing it a breeze. You can download the binaries for the open-source version of Terraform by clicking here. Make sure you are downloading the binary that matches the OS of the computer you want to install Terraform on.

Once Terraform is installed, it comes with a list of commands and subcommands you can use to provision and make changes to your infrastructure. The CLI has three main commands:

  • terraform init

  • terraform plan

  • terraform apply

Time to look at each one in detail.

terraform init: This command initializes a working directory containing Terraform configuration files. This is the first command that you should run after writing a new Terraform configuration or cloning an existing one from version control. Running this command multiple times does not negatively affect your project as it skips the re-initializing process unless in scenarios where changes have been made to your configuration.

terraform plan: The command lets you preview the changes Terraform is going to make to your infrastructure when you run terraform apply. It essentially shows you a detailed execution plan of the resources that will created, modified or destroyed. This helps you verify that the proposed changes tie with what you expect to happen when you apply the changes. There is no need to run this command when you are sure that what you have written in your configuration file will run exactly as expected.

terraform apply: As the name suggests, this is the command you run when you want to apply the changes made to your configuration files. It is the command that actually creates or modifies your infrastructure. When you run the command without the optional -auto-approve flag, it requires that you manually confirm that you want to have the proposed changes applied by typing yes in the interactive terminal.

💡 Terraform’s official documentation recommends that you only use -auto-approve when you are sure that no one is going to make changes to your infrastructure outside of your Terraform workflow. Doing this minimizes the risks of configuration drift and unpredictable changes.

In addition to these three primary commands, there are also subcommands that the Terraform CLI offers. You can read about them in the official documentation.

Providers

Terraform uses a plugin architecture to manage all of the resource providers that it supports so no providers are included when you first install Terraform. You declare which providers you need to use in a configuration file and they are installed from the Terraform registry when you run the terraform init command. This registry is the main directory of publicly available Terraform providers, and hosts providers for most major infrastructure platforms such as AWS, Microsoft Azure, GCP, Alibaba Cloud, and others.

Each provider has its own documentation, describing its resource types and their arguments. This documentation is included in the registry.

Terraform Configuration and HashiCorp Configuration Language

A Terraform configuration is a complete document in the Terraform language that tells Terraform how to manage a given collection of infrastructure. The Terraform files you work with to manage your infrastructure use the .tf file extension. The Terraform language (aka HCL), is designed to be both human-readable and machine-friendly.

HCL allows you to treat your infrastructure as code and serves as your system's "living documentation". The main goal of the Terraform language according to the official documentation is declaring resources, which represent infrastructure objects. All other language features exist only to make the definition of resources more flexible and convenient.

The language is declarative, describing an intended goal rather than the steps to reach that goal. Here is the syntax of the Terraform language as seen in the official documentation:

resource "aws_vpc" "main" {
  cidr_block = var.base_cidr_block
}

<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
  # Block body
  <IDENTIFIER> = <EXPRESSION> # Argument
}
Enter fullscreen mode Exit fullscreen mode

As seen in the syntax, the language consists only of a few basic elements: Blocks, Arguments, and Expressions.

Blocks are containers for other content and usually represent the configuration of some kind of object, like a resource. Blocks have a block type, can have zero or more labels, and have a body that contains any number of arguments and nested blocks. Most of Terraform's features are controlled by top-level blocks in a configuration file.
Arguments assign a value to a name. They appear within blocks.
Expressions represent a value, either literally or by referencing and combining other values. They appear as values for arguments, or within other expressions.

Conclusion

If you are a business that is looking to do more with Terraform, HashiCorp also provides enterprise versions of Terraform that provide additional features for collaboration, infrastructure policy, and governance.

If it is the case that I succeeded in scratching your curiosity's itch about Terraform with this article, I implore you to follow me, connect with me on LinkedIn, and keep an eye out as I will be putting out more articles on Terraform and related technologies. Until then, you can check out my other articles here, and don’t stop going after your goals and ideals.

References

Top comments (0)