Locals
Objective
Understand the basics of Terraform local variables.
Overview
Terraform offers a second type of variable knows as a local variable. Locals are available to the module they are defined in. Locals are often used for repeated values and expressions. Locals can not be overridden from the CLI or tfvars file. They are defined with the locals keyword followed by a map of key/value pairs.
locals {
key = "value"
}
Lab
In this lab we will define a local variable and output it to the CLI.
Add a Local Value to the Root Module
Edit main.tf
locals {
hello_world_local = "Hello World - Local"
}
Add Another Output
Edit output.tf
output "hello_world_local" {
value = local.hello_world_local
}
Plan
We can now view our new output in a plan.
Execute
terraform plan
Review
In this section we learned how to use local variables.