Terraform - Define dictionary using locals

by
Jeremy Canfield |
Updated: July 20 2023
| Terraform articles
A dictionary (key value pairs) can be defined in a locals block. Let's say you have the following files on your Terraform server.
├── locals.tf
├── modules.tf
├── outputs.tf
├── provider.tf
├── terraform.tfstate
├── variables.tf
├── child (directory, child module)
│ ├── data.tf
│ ├── outputs.tf
│ ├── resources.tf
For example, let's say locals.tf file has the following.
locals {
dictionary = {
foo = "Hello"
bar = "World"
}
}
And let's say your outputs.tf in the same directory as your main root module (main.tf) has the following.
output "my-dictionary" {
value = local.dictionary
}
The terraform output command should return the following.
my-dictionary = {
"foo" = "Hello"
"bar" = "World"
}
Did you find this article helpful?
If so, consider buying me a coffee over at