Search⌘ K
AI Features

Locals in Detail

Explore how to define and utilize Terraform locals as reusable expressions and fixed values. Understand their syntax and usage within Terraform projects, including referencing other locals and resource outputs. This lesson helps you organize and simplify your infrastructure code by creating meaningful, composite values.

Terraform local

A local is Terraform’s representation of a normal programming language variable. Confusingly, Terraform also has a concept called a variable which is really an input (variables are covered in the variables chapter). A local can refer to a fixed value such as a string, or it can be used to refer to an expression such as two other locals concatenated together.

Project example

Let’s dive into an example:

C++
provider "aws" {
region = "us-east-2"
}
locals {
first_part = "hello"
second_part = "${local.first_part}-there"
bucket_name = "${local.second_part}-how-are-you-today"
}
resource "aws_s3_bucket" "bucket" {
bucket = local.bucket_name
}

Project code in detail

In the code above:

  • We define a local block by using the keyword locals and then an opening { ...