github.com/pgray/terraform@v0.5.4-0.20170822184730-b6a464c5214d/website/docs/configuration/locals.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "Configuring Local Values" 4 sidebar_current: "docs-config-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 Value Configuration 11 12 Local values assign a name to an expression, that can then be used multiple 13 times within a module. 14 15 Comparing modules to functions in a traditional programming language, 16 if [variables](./variables.html) are analogous to function arguments and 17 [outputs](./outputs.html) are analogous to function return values then 18 _local values_ are comparable to a function's local variables. 19 20 This page assumes you're already familiar with 21 [the configuration syntax](/docs/configuration/syntax.html). 22 23 ## Example 24 25 Local values are defined in `locals` blocks: 26 27 ```hcl 28 # Ids for multiple sets of EC2 instances, merged together 29 locals { 30 instance_ids = "${concat(aws_instance.blue.*.id, aws_instance.green.*.id)}" 31 } 32 33 # A computed default name prefix 34 locals { 35 default_name_prefix = "${var.project_name}-web" 36 name_prefix = "${var.name_prefix != "" ? var.name_prefix : local.default_name_prefix}" 37 } 38 ``` 39 40 ## Description 41 42 The `locals` block defines one or more local variables within a module. 43 Each `locals` block can have as many locals as needed, and there can be any 44 number of `locals` blocks within a module. 45 46 The names given for the items in the `locals` block must be unique throughout 47 a module. The given value can be any expression that is valid within 48 the current module. 49 50 The expression of a local value can refer to other locals, but as usual 51 reference cycles are not allowed. That is, a local cannot refer to itself 52 or to a variable that refers (directly or indirectly) back to it. 53 54 It's recommended to group together logically-related local values into 55 a single block, particulary if they depend on each other. This will help 56 the reader understand the relationships between variables. Conversely, 57 prefer to define _unrelated_ local values in _separate_ blocks, and consider 58 annotating each block with a comment describing any context common to all 59 of the enclosed locals.