How to use locals with terraform ?

Namrata D
2 min readJul 21, 2023

--

A local value assigns a name to an expressions so you can use the name multiple times within a module. It is helpful to avoid repeating the same values or expressions multiple times in a configuration, but if overused they can also make a configuration hard to read . Locals values are not set by the user input or values in terraform files, instead, they are set ‘locally’ to the configuration .

Benefits:

Lets understand the benefits -

  1. Terraform locals can be re-used multiple times in the terraform configuration file.
  2. When you use locals within the terraform locals you need to update its value once and it should reflect all over the place where it is referred.

Example:

locals {
key1 = value1
key2 = value2
}

Using it to create a simple resources in GCP

  1. The locals block is used to define local variables. In this case, we define a local variable named bucket name, which is constructed using the project and environment variables. This allows us to create unique bucket names based on the environment.

2. The second block creates the GCS bucket. We use the local.bucket_name variable as the name for the bucket, which ensures that the bucket name will be unique for each environment.


locals {
# Define the bucket name based on some variables
bucket_name = "my-bucket-${var.environment}-${var.project_name}"
}

resource "google_storage_bucket" "my_bucket" {
name = local.bucket_name
location = var.bucket_location

versioning {
enabled = true
}
}

Let’s look a bit complex problem Problem.

To use locals to create multiple instances in different projects and regions, you can define a list of instance configurations and use for_each with locals to loop through and create instances accordingly.

locals {
instances = {
"instance-1" = {
project_id = "project-1-id"
region = "us-west1"
zone = "us-west1-a"
machine_type = "n1-standard-1"
},
"instance-2" = {
project_id = "project-2-id"
region = "europe-west1"
zone = "europe-west1-b"
machine_type = "n1-standard-2"
},
"instance-3" = {
project_id = "project-3-id"
region = "asia-east1"
zone = "asia-east1-a"
machine_type = "n1-standard-2"
}
// Add more instance configurations as needed
}
}

resource "google_compute_instance" "instances" {
for_each = local.instances

name = each.key
machine_type = each.value.machine_type
zone = each.value.zone
project = each.value.project_id
}

This cleaner approach provides a more straightforward way to define and manage multiple instances in different projects and regions. You can easily add or modify instance configurations within the local.instances map to create more instances or update existing ones.

Overall, locals provide an intelligent approach to manage intermediate values in your Terraform configuration, making it more maintainable, readable, and efficient. They are a key feature that helps make Terraform configurations more elegant and manageable, especially as the infrastructure complexity grows.

--

--

Namrata D
Namrata D

Written by Namrata D

AWS Solution Architect Associate, CKA,CKAD,CKS, Terraform & HashiCorp Vault Certified

No responses yet