Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.
* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 11:49:21 +01:00
|
|
|
---
|
2020-03-18 18:46:47 -04:00
|
|
|
page_title: Local Values - HCL Configuration Language
|
|
|
|
description: >-
|
|
|
|
Local values assign a name to an expression that can then be used multiple
|
|
|
|
times within a folder.
|
Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.
* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 11:49:21 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
# Local Values
|
|
|
|
|
2020-05-26 15:29:47 +02:00
|
|
|
`@include 'from-1.5/beta-hcl2-note.mdx'`
|
2020-04-28 11:37:50 +02:00
|
|
|
|
2021-02-22 02:33:20 -08:00
|
|
|
There are two kinds of variables in HCL Packer templates: Input variables,
|
|
|
|
sometimes simply called "variables", and Local variables, also known as
|
|
|
|
"locals". Input variables may have defaults, but those defaults can
|
|
|
|
be overridden from the command line or special variable files. Local variables
|
|
|
|
can be thought of as constants, and are not able to be overridden at runtime.
|
|
|
|
|
|
|
|
This page is about local variables. To learn about input variables, see the
|
|
|
|
[input variables](/docs/templates/hcl_templates/variables) page.
|
|
|
|
|
Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.
* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 11:49:21 +01:00
|
|
|
Local values assign a name to an expression, that can then be used multiple
|
|
|
|
times within a folder.
|
|
|
|
|
2021-01-15 09:18:39 -08:00
|
|
|
If [variables](/docs/templates/hcl_templates/variables) are analogous to function arguments then
|
Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.
* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 11:49:21 +01:00
|
|
|
_local values_ are comparable to a function's local variables.
|
|
|
|
|
|
|
|
Input variable and local variable usage are introduced in the [_Variables
|
|
|
|
Guide_](/guides/hcl/variables).
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2021-01-26 01:21:44 -08:00
|
|
|
Local values are defined in `local` or `locals` blocks:
|
Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.
* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 11:49:21 +01:00
|
|
|
|
|
|
|
```hcl
|
2021-01-26 01:21:44 -08:00
|
|
|
# Using the local block allows you to mark locals as sensitive, which will
|
|
|
|
# filter their values from logs.
|
|
|
|
local "mylocal" {
|
|
|
|
expression = "${var.secret_api_key}"
|
|
|
|
sensitive = true
|
|
|
|
}
|
|
|
|
|
|
|
|
# Using the locals block is more compact and efficient for declaring many locals
|
Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.
* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 11:49:21 +01:00
|
|
|
# Ids for multiple sets of EC2 instances, merged together
|
|
|
|
locals {
|
|
|
|
instance_ids = "${concat(aws_instance.blue.*.id, aws_instance.green.*.id)}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# A computed default name prefix
|
|
|
|
locals {
|
|
|
|
default_name_prefix = "${var.project_name}-web"
|
|
|
|
name_prefix = "${var.name_prefix != "" ? var.name_prefix : local.default_name_prefix}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Local values can be interpolated elsewhere using the "local." prefix.
|
|
|
|
source "virtualbox-iso" "example" {
|
|
|
|
output = "${local.name_prefix}-files"
|
|
|
|
# ...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Named local maps can be merged with local maps to implement common or default
|
|
|
|
values:
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
# Define the common tags for all resources
|
|
|
|
locals {
|
|
|
|
common_tags = {
|
|
|
|
Component = "awesome-app"
|
|
|
|
Environment = "production"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create a resource that blends the common tags with instance-specific tags.
|
|
|
|
source "amazon-ebs" "server" {
|
|
|
|
source_ami = "ami-123456"
|
|
|
|
instance_type = "t2.micro"
|
|
|
|
|
|
|
|
tags = "${merge(
|
|
|
|
local.common_tags,
|
|
|
|
map(
|
|
|
|
"Name", "awesome-app-server",
|
|
|
|
"Role", "server"
|
|
|
|
)
|
|
|
|
)}"
|
|
|
|
# ...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
2021-01-26 01:21:44 -08:00
|
|
|
The `local` block defines exactly one local variable within a folder. The block
|
|
|
|
label is the name of the local, and the "expression" is the expression that
|
|
|
|
should be evaluated to create the local. Using this block, you can optionally
|
|
|
|
supply a "sensitive" boolean to mark the variable as sensitive and filter it
|
|
|
|
from logs.
|
|
|
|
|
Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.
* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 11:49:21 +01:00
|
|
|
The `locals` block defines one or more local variables within a folder.
|
|
|
|
|
|
|
|
The names given for the items in the `locals` block must be unique throughout a
|
|
|
|
folder. The given value can be any expression that is valid within the current
|
|
|
|
folder.
|
|
|
|
|
|
|
|
The expression of a local value can refer to other locals, but reference cycles
|
|
|
|
are not allowed. That is, a local cannot refer to itself or to a variable that
|
|
|
|
refers (directly or indirectly) back to it.
|
|
|
|
|
|
|
|
It's recommended to group together logically-related local values into a single
|
|
|
|
block, particularly if they depend on each other. This will help the reader
|
|
|
|
understand the relationships between variables. Conversely, prefer to define
|
|
|
|
_unrelated_ local values in _separate_ blocks, and consider annotating each
|
|
|
|
block with a comment describing any context common to all of the enclosed
|
|
|
|
locals.
|