mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-03 12:20:28 -06:00
Merge pull request #1621 from hashicorp/f-envs
Set variables from env vars
This commit is contained in:
commit
af4396aa0d
@ -5988,6 +5988,46 @@ func TestContext2Apply_vars(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext2Apply_varsEnv(t *testing.T) {
|
||||
m := testModule(t, "apply-vars-env")
|
||||
p := testProvider("aws")
|
||||
p.ApplyFn = testApplyFn
|
||||
p.DiffFn = testDiffFn
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Module: m,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
})
|
||||
|
||||
// Set the env var
|
||||
old := tempEnv(t, "TF_VAR_ami", "baz")
|
||||
defer os.Setenv("TF_VAR_ami", old)
|
||||
|
||||
w, e := ctx.Validate()
|
||||
if len(w) > 0 {
|
||||
t.Fatalf("bad: %#v", w)
|
||||
}
|
||||
if len(e) > 0 {
|
||||
t.Fatalf("bad: %s", e)
|
||||
}
|
||||
|
||||
if _, err := ctx.Plan(); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
state, err := ctx.Apply()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(state.String())
|
||||
expected := strings.TrimSpace(testTerraformApplyVarsEnvStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad: \n%s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext2Apply_createBefore_depends(t *testing.T) {
|
||||
m := testModule(t, "apply-depends-create-before")
|
||||
h := new(HookRecordApplyOrder)
|
||||
|
@ -11,6 +11,12 @@ import (
|
||||
"github.com/hashicorp/terraform/config/module"
|
||||
)
|
||||
|
||||
const (
|
||||
// VarEnvPrefix is the prefix of variables that are read from
|
||||
// the environment to set variables here.
|
||||
VarEnvPrefix = "TF_VAR_"
|
||||
)
|
||||
|
||||
// Interpolater is the structure responsible for determining the values
|
||||
// for interpolations such as `aws_instance.foo.bar`.
|
||||
type Interpolater struct {
|
||||
@ -43,6 +49,11 @@ func (i *Interpolater) Values(
|
||||
}
|
||||
for _, v := range mod.Config().Variables {
|
||||
for k, val := range v.DefaultsMap() {
|
||||
envKey := VarEnvPrefix + strings.TrimPrefix(k, "var.")
|
||||
if v := os.Getenv(envKey); v != "" {
|
||||
val = v
|
||||
}
|
||||
|
||||
result[k] = ast.Variable{
|
||||
Value: val,
|
||||
Type: ast.TypeString,
|
||||
|
@ -47,6 +47,14 @@ func tempDir(t *testing.T) string {
|
||||
return dir
|
||||
}
|
||||
|
||||
// tempEnv lets you temporarily set an environment variable. It returns
|
||||
// the old value that should be set via a defer.
|
||||
func tempEnv(t *testing.T, k string, v string) string {
|
||||
old := os.Getenv(k)
|
||||
os.Setenv(k, v)
|
||||
return old
|
||||
}
|
||||
|
||||
func testConfig(t *testing.T, name string) *config.Config {
|
||||
c, err := config.Load(filepath.Join(fixtureDir, name, "main.tf"))
|
||||
if err != nil {
|
||||
@ -621,6 +629,13 @@ aws_instance.foo:
|
||||
type = aws_instance
|
||||
`
|
||||
|
||||
const testTerraformApplyVarsEnvStr = `
|
||||
aws_instance.bar:
|
||||
ID = foo
|
||||
foo = baz
|
||||
type = aws_instance
|
||||
`
|
||||
|
||||
const testTerraformPlanStr = `
|
||||
DIFF:
|
||||
|
||||
|
7
terraform/test-fixtures/apply-vars-env/main.tf
Normal file
7
terraform/test-fixtures/apply-vars-env/main.tf
Normal file
@ -0,0 +1,7 @@
|
||||
variable "ami" {
|
||||
default = "foo"
|
||||
}
|
||||
|
||||
resource "aws_instance" "bar" {
|
||||
foo = "${var.ami}"
|
||||
}
|
@ -90,6 +90,25 @@ The usage of maps, strings, etc. is documented fully in the
|
||||
[interpolation syntax](/docs/configuration/interpolation.html)
|
||||
page.
|
||||
|
||||
## Environment Variables
|
||||
|
||||
Environment variables can be used to set the value of a variable.
|
||||
The key of the environment variable must be `TF_VAR_name` and the value
|
||||
is the value of the variable.
|
||||
|
||||
For example, given the configuration below:
|
||||
|
||||
```
|
||||
variable "image" {}
|
||||
```
|
||||
|
||||
The variable can be set via an environment variable:
|
||||
|
||||
```
|
||||
$ TF_VAR_image=foo terraform apply
|
||||
...
|
||||
```
|
||||
|
||||
## Syntax
|
||||
|
||||
The full syntax is:
|
||||
|
@ -53,14 +53,16 @@ the AWS provider with the given variables.
|
||||
|
||||
## Assigning Variables
|
||||
|
||||
There are three ways to assign variables.
|
||||
There are multiple ways to assign variables. Below is also the order
|
||||
in which variable values are chosen. If they're found in an option first
|
||||
below, then the options below are ignored.
|
||||
|
||||
First, if you execute `terraform plan` or apply without doing
|
||||
**UI Input:** If you execute `terraform plan` or apply without doing
|
||||
anything, Terraform will ask you to input the variables interactively.
|
||||
These variables are not saved, but provides a nice user experience for
|
||||
getting started with Terraform.
|
||||
|
||||
For another option, you can set it directly on the command-line with the
|
||||
**Command-line flags:** You can set it directly on the command-line with the
|
||||
`-var` flag. Any command in Terraform that inspects the configuration
|
||||
accepts this flag, such as `apply`, `plan`, and `refresh`:
|
||||
|
||||
@ -74,7 +76,7 @@ $ terraform plan \
|
||||
Once again, setting variables this way will not save them, and they'll
|
||||
have to be input repeatedly as commands are executed.
|
||||
|
||||
The third way, and the way to persist variable values, is to create
|
||||
**From a file:** To persist variable values, create
|
||||
a file and assign variables within this file. Create a file named
|
||||
"terraform.tfvars" with the following contents:
|
||||
|
||||
@ -89,6 +91,10 @@ named something else, you can use the `-var-file` flag directly to
|
||||
specify a file. These files are the same syntax as Terraform configuration
|
||||
files. And like Terraform configuration files, these files can also be JSON.
|
||||
|
||||
**From environment variables:** Terraform will read environment variables
|
||||
in the form of `TF_VAR_name` to find the value for a variable. For example,
|
||||
the `TF_VAR_access_key` variable can be set to set the `access_key` variable.
|
||||
|
||||
We recommend using the "terraform.tfvars" file, and ignoring it from
|
||||
version control.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user