Terraform Backends

Overview

Previously, we were managing Terraform states locally on our platform. In this lesson, we’ll learn how to share this state with other modules, users, and contexts using Terraform backends.

Importance

In real-world usage, an understanding of backends is essential. We’ll learn how they work in principle by showing the same state file stored remotely from the code of two separate modules.

Terraform backends

Up to this point, we’ve automatically created a Terraform state file in the directory in which we run the terraform command. We’ve also looked at examining and interrogating.

Now we’ll begin to look at placing this state file elsewhere.

Why would we do this? Placing our state file next to our code is fine for casual projects, but if the resources and their state are precious to us, we’ll want to make sure it’s in a safe place, ideally backed up and with a change log. The most commonly seen pattern in real Terraform projects is to place the Terraform state in an S3 bucket (if on AWS) or another remote storage method. This state file, along with their resources’ data, can then be reliably referred to by multiple modules.

Here, we’ll demonstrate the principle by storing the same state file that tracks the state of a single file over two separate Terraform modules. This should give you a clearer idea of what remote state files can do before extending that to cloud resources.

Up to this point in the course, if we wanted to manage a local resource—such as a simple text file—we stored the Terraform state locally, which is the default.

Get hands-on with 1200+ tech skills courses.