github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/configuration-0-11/locals.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "Local Values - 0.11 Configuration Language" 4 sidebar_current: "docs-conf-old-locals" 5 description: |- 6 Local values assign a name to an expression that can then be used multiple times 7 within a module. 8 --- 9 10 # Local Values 11 12 -> **Note:** This page is about Terraform 0.11 and earlier. For Terraform 0.12 13 and later, see 14 [Configuration Language: Configuring Local Values](../configuration/locals.html). 15 16 Local values assign a name to an expression, that can then be used multiple 17 times within a module. 18 19 Comparing modules to functions in a traditional programming language, 20 if [variables](./variables.html) are analogous to function arguments and 21 [outputs](./outputs.html) are analogous to function return values then 22 _local values_ are comparable to a function's local variables. 23 24 This page assumes you're already familiar with 25 [the configuration syntax](./syntax.html). 26 27 ## Examples 28 29 Local values are defined in `locals` blocks: 30 31 ```hcl 32 # Ids for multiple sets of EC2 instances, merged together 33 locals { 34 instance_ids = "${concat(aws_instance.blue.*.id, aws_instance.green.*.id)}" 35 } 36 37 # A computed default name prefix 38 locals { 39 default_name_prefix = "${var.project_name}-web" 40 name_prefix = "${var.name_prefix != "" ? var.name_prefix : local.default_name_prefix}" 41 } 42 43 # Local values can be interpolated elsewhere using the "local." prefix. 44 resource "aws_s3_bucket" "files" { 45 bucket = "${local.name_prefix}-files" 46 # ... 47 } 48 ``` 49 50 Named local maps can be merged with local maps to implement common or default 51 values: 52 53 ```hcl 54 # Define the common tags for all resources 55 locals { 56 common_tags = { 57 Component = "awesome-app" 58 Environment = "production" 59 } 60 } 61 62 # Create a resource that blends the common tags with instance-specific tags. 63 resource "aws_instance" "server" { 64 ami = "ami-123456" 65 instance_type = "t2.micro" 66 67 tags = "${merge( 68 local.common_tags, 69 map( 70 "Name", "awesome-app-server", 71 "Role", "server" 72 ) 73 )}" 74 } 75 ``` 76 77 ## Description 78 79 The `locals` block defines one or more local variables within a module. 80 Each `locals` block can have as many locals as needed, and there can be any 81 number of `locals` blocks within a module. 82 83 The names given for the items in the `locals` block must be unique throughout 84 a module. The given value can be any expression that is valid within 85 the current module. 86 87 The expression of a local value can refer to other locals, but as usual 88 reference cycles are not allowed. That is, a local cannot refer to itself 89 or to a variable that refers (directly or indirectly) back to it. 90 91 It's recommended to group together logically-related local values into 92 a single block, particularly if they depend on each other. This will help 93 the reader understand the relationships between variables. Conversely, 94 prefer to define _unrelated_ local values in _separate_ blocks, and consider 95 annotating each block with a comment describing any context common to all 96 of the enclosed locals.