How to Use Local Values in Terraform: Simple Guide
In Terraform,
locals let you define named values that you can reuse within your configuration. You declare them inside a locals block and then reference them by name to keep your code clean and avoid repeating expressions.Syntax
The locals block defines one or more local values as key-value pairs. Each key is a name you choose, and the value can be any valid Terraform expression. You reference these values elsewhere using local.name.
- locals block: Groups local values.
- key: The name of the local value.
- value: The expression or value assigned.
- reference: Use
local.keyto access the value.
terraform
locals {
example_name = "Hello, Terraform!"
example_number = 42
example_list = ["a", "b", "c"]
}
output "greeting" {
value = local.example_name
}
output "number" {
value = local.example_number
}
output "letters" {
value = local.example_list
}Example
This example shows how to define local values for a project name and environment, then use them to build resource names. It demonstrates how locals help avoid repeating the same strings multiple times.
terraform
locals {
project = "myapp"
environment = "dev"
resource_prefix = "${local.project}-${local.environment}"
}
resource "aws_s3_bucket" "example" {
bucket = "${local.resource_prefix}-bucket"
acl = "private"
}
output "bucket_name" {
value = aws_s3_bucket.example.bucket
}Output
bucket_name = "myapp-dev-bucket"
Common Pitfalls
Common mistakes when using local values include:
- Trying to use
localvalues outside the same module where they are defined. - Using interpolation syntax
${}unnecessarily insidelocalswhen not needed. - Defining locals with complex expressions that are hard to read or debug.
Always keep local values simple and use them only within the module they are declared.
terraform
locals {
# Wrong: unnecessary interpolation
wrong_name = "myapp"
# Right: simple string assignment
right_name = "myapp"
}
output "wrong" {
value = local.wrong_name
}
output "right" {
value = local.right_name
}Output
wrong = "myapp"
right = "myapp"
Quick Reference
| Concept | Description | Example |
|---|---|---|
| Define locals | Use a locals block with key-value pairs | locals { name = "value" } |
| Reference locals | Use local.key to get the value | local.name |
| Scope | Locals are only available inside the module they are declared | Cannot access locals across modules |
| Use case | Avoid repeating expressions and improve readability | Build resource names from parts |
| Avoid | Unnecessary interpolation and complex logic | Use simple expressions |
Key Takeaways
Use
locals to define reusable values inside a Terraform module.Reference local values with
local.name to keep code clean and avoid repetition.Locals are limited to the module where they are declared and cannot be shared across modules.
Avoid unnecessary interpolation syntax inside locals; assign values directly when possible.
Keep local values simple and readable to make your Terraform code easier to maintain.