Can I set terraform output to env variables?

Viewed 7094

So terraform generates a bunch of the output I'm interested in. How can pipe those values to environment variables or something such that my script will be able to pick them up after someone runs terraform apply?

3 Answers

Terraform can't directly modify the environment of the shell that called it --generally, a program can only modify its own environment or set a new environment when starting a child program itself -- so any solution here will involve taking additional action in the calling shell once terraform apply returns.

One way you could do this is to pipe the output from terraform output -json into a program that can parse the JSON, extract the relevant values, and then print them out as one or more shell statements to set environment variables. You could then run the result of that program as a shell script, for example using the source command in bash, to incorporate those environment variable values into your current shell.

You could write this transforming program in any programming language you are comfortable with. For simple cases you might like to write a simple shell script using jq, relying on its @sh escaping mode to guarantee valid shell quoting of the values:

terraform output -json | jq -r '@sh "export EXAMPLE1=\(.example1.value)\nexport EXAMPLE2=\(.example2.value)"'

I tried this with the following test Terraform configuration:

output "example1" {
  value = "hello"
}

output "example2" {
  value = <<EOT
Hello world
This is a multi-line string with 'quotes' in "it".
EOT
}

After applying that configuration, the command above produced the following output:

export EXAMPLE1='hello'
export EXAMPLE2='Hello world
This is a multi-line string with '\''quotes'\'' in "it".
'

I redirected the output to a file env.sh and then loaded it into my shell to confirm that the variables were usable:

$ terraform output -json | jq -r '@sh "export EXAMPLE1=\(.example1.value)\nexport EXAMPLE2=\(.example2.value)"' >env.sh
$ source env.sh 
$ echo $EXAMPLE1
hello
$ echo "$EXAMPLE2"
Hello world
This is a multi-line string with 'quotes' in "it".

(Note that for EXAMPLE2 I put the variable interpolation in quotes, because otherwise the shell will understand each of the space-separated words as a separate argument, rather than considering the whole value as a single string with all of the whitespace characters intact.)

as stated before, you can't take the value from terraform apply, you can use terraform output.

I would also suggest to use -raw, from help

  -raw             For value types that can be automatically
                   converted to a string, will print the raw
                   string directly, rather than a human-oriented
                   representation of the value.

Example how I use:

KMS_KEY_ID=$(terraform output -raw kms_secrets_id)

I hope it helps somebody

You can use this command to export the output of the terraform to environment variables.

export $(terraform output | sed 's/\s*=\s*/=/g' | xargs)

The middle part of the command, sed 's/\s*=\s*/=/g' is responsible for removing the space between the equal (=) sign. It will convert from KEY = "VALUE" to KEY="VALUE". The last part, xargs will convert KEY="VALUE" to KEY=VALUE which is the standard way of exporting values into environment variables.

Related