Introduction to Terraform
Tags: terraform, devops, infrastructure
Published: 2020-11-20 13:59:32

---
title: Introduction to Terraform
published: true
description: "In this post you will find an overview of how Terraform works and why it might be a good addition to your toolset."
tags: 
  - terraform
  - devops
  - infrastructure
canonical_url: https://ruanmartinelli.com/posts/introduction-to-terraform
---

First, let’s talk about **Infrastructure as Code** (IaC).

Like the name suggests, Infrastructure as Code is the process of provisioning infrastructure using code. It is a way to automate the creation of the topology needed to run your application.

Think of _Infrastructure_ as a broad term. It can be anything from virtual machines to Kubernetes clusters to storage buckets. _Code_ in this context refers to the language used for writing the automation. The most common ones are JSON, YAML and HashiCorp Language Format (HCL).

## IaC: the good parts

Automating your infrastructure has great advantages. Some of my favorite are:

**Consistency**. Consistency is the name of the game on IaC. Ad-hoc changes to infrastructure can cause a mismatch between development, staging and production environments.

**Dynamic provisioning**. Infrastructure is transient. When it’s automated, it becomes easier to provision and deprovision resources according to load.

**Reproducible environments**. Spinning up a sandbox for tests with a certain configuration usually takes a few minutes and has very little human intervention.

**Documented infrastructure**. The IaC code works as an always up-to-date documentation of your infrastructure. Tools like Git will also give you an audit trail and bring collaboration aspects to it.

## Meet Terraform

[Terraform](https://www.terraform.io) is an open source command line tool by [HashiCorp](https://www.hashicorp.com).

It gives you the power to manage - build, update and destroy - infrastructure using IaC principles.

Terraform uses the HCL language. With HCL, you declare a blueprint of how you want your infrastructure to look like. Terraform will read that blueprint and provision all the resources you declared.

This is how an HCL file looks like:

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

resource "aws_s3_bucket" "bucket" {
  bucket = "my-private-bucket"
  acl    = "private"

  versioning {
    enabled = true
  }
}
```

<figcaption>1. Example HCL file.</figcaption>

In the example above we are informing Terraform that Amazon Web Services (AWS) is our provider of choice. We are also declaring a resource: a private S3 bucket.

## Providers

One very important concept from Terraform are _providers_.

Providers are the glue between the Terraform CLI and other systems. They work like plugins and live separately from Terraform.

With providers, Terraform is able to connect to cloud vendors (e.g. Google Cloud Platform, AWS), PaaS solutions (e.g. Heroku, Kubernetes) and SaaS applications (e.g. Fastly, GitHub).

## State

Terraform keeps track of your infrastructure though a **state file**.

The state file is the source of truth for your architectural elements. It contains all the resources created by Terraform, plus any metadata needed to make Terraform work.

When you update your configuration - your HCL files - you are declaring a new **desired state**.

Terraform’s job is to turn that desired state into real-world. It is going to match your current state - state file - and your desired state - your configuration - and come up with a plan of execution.

The plan contains information about what needs to be created, updated or deleted on the infrastructure. Dependencies between resources are solved using a [dependency graph](https://en.wikipedia.org/wiki/Dependency_graph). With everything in place, Terraform will then call all the APIs necessary to build and manage all the resources.

## Example

Let’s go back to our first example.

Imagine we change the bucket ACL from `private` to `public-read`. Here’s what Terraform would do:

1. Compares the current state with the desired state;
2. Sees that the ACL of the bucket has changed;
3. Creates a plan - 1 resource needs to be updated;
4. Calls the AWS S3 endpoints to update the bucket ACL.

Will it simply update the ACL to `public-read`? Will it delete the bucket and create a new one?

That depends on which type of resource is modified. In general, Terraform is smart enough to only destroy and recreate resources when absolutely needed.

## Is Terraform a good choice for my company?

..or startup, or side project?

Terraform does bring an additional complexity to your workflow. HCL is a new language to learn. The command line tool is still fairly new compared to other IaC solutions and it still shows a few rough edges.

That being said, once you master Terraform, it becomes an indispensable tool. Managing infrastructure with code can actually feel easier and safer than poking around on buttons in a cloud provider dashboard.

The variety of providers on the [Terraform Registry](https://registry.terraform.io) also shows how versatile it is. Once you learn it, you can apply the same knowledge on any cloud provider, service, or [pizza delivery chain](https://github.com/ndmckinley/terraform-provider-dominos).

That’s it for an introduction, thanks for reading!

---

This post was originally published on [HYPERFOO](https://hyperfoo.io), go there for more!

_<span>Cover background by <a href="https://unsplash.com/@pawel_czerwinski?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Paweł Czerwiński</a> on Unsplash_