mirror of https://github.com/hashicorp/packer
HCL2: allow calling env as input var default value (#10240)
* HCL2: allow to use env in default value of input variablespull/10244/head
parent
17ec88246f
commit
deba1484ff
@ -0,0 +1,32 @@
|
||||
package function
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/function"
|
||||
)
|
||||
|
||||
// EnvFunc constructs a function that returns a string representation of the
|
||||
// env var behind a value
|
||||
var EnvFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "key",
|
||||
Type: cty.String,
|
||||
AllowNull: false,
|
||||
AllowUnknown: false,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
key := args[0].AsString()
|
||||
value := os.Getenv(key)
|
||||
return cty.StringVal(value), nil
|
||||
},
|
||||
})
|
||||
|
||||
// Env returns a string representation of the env var behind key.
|
||||
func Env(key cty.Value) (cty.Value, error) {
|
||||
return EnvFunc.Call([]cty.Value{key})
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
---
|
||||
layout: docs
|
||||
page_title: env - Functions - Configuration Language
|
||||
sidebar_title: env
|
||||
description: The env function retrieves environment values for input variables.
|
||||
---
|
||||
|
||||
# `env` Function
|
||||
|
||||
```hcl
|
||||
variable "aws_region" {
|
||||
default = env("AWS_DEFAULT_REGION")
|
||||
}
|
||||
```
|
||||
|
||||
`env` allows you to get the value for an environment variable inside input
|
||||
variables _only_. This is the only function that is callable from a variable
|
||||
block and it can only be used in the default input. `env` cannot be called from
|
||||
other places.
|
||||
|
||||
In the previous example, the value of `aws_region` will be what's stored in the
|
||||
`AWS_DEFAULT_REGION` env var, unless aws_region is also set in a [manner that takes
|
||||
precedence](/docs/from-1.5/variables#variable-definition-precedence).
|
||||
|
||||
|
||||
-> **Why can't I use environment variables elsewhere?** User variables are the
|
||||
single source of configurable input. We felt that having environment variables
|
||||
used _anywhere_ in a configuration would confuse the user about the possible inputs
|
||||
to a template. By allowing environment variables only within default values for
|
||||
input variables, input variables remain as the single source of input to a
|
||||
template that a user can easily discover using `packer inspect`.
|
||||
|
||||
When the environment variable is not set at all -- not even with the empty
|
||||
string -- the value returned by `env` will be an an empty string. It will still
|
||||
be possible to set it using other means but you could use [custom validation
|
||||
rules](/docs/from-1.5/variables#custom-validation-rules) to error in that case
|
||||
to make sure it is set, for example:
|
||||
|
||||
```hcl
|
||||
variable "aws_region" {
|
||||
default = env("AWS_DEFAULT_REGION")
|
||||
|
||||
validation {
|
||||
condition = length(var.aws_region) > 0
|
||||
error_message = <<EOF
|
||||
The aws_region var is not set: make sure to at least set the AWS_DEFAULT_REGION env var.
|
||||
To fix this you could also set the aws_region variable from the arguments, for example:
|
||||
$ packer build -var=aws_region=us-something-1...
|
||||
EOF
|
||||
}
|
||||
}
|
||||
```
|
||||
Loading…
Reference in new issue