Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set terraform output to env variables?

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?

like image 821
Serge Vu Avatar asked Dec 15 '20 23:12

Serge Vu


People also ask

How do you store Terraform output as a variable?

Create one last file inside the ~/terraform-output-demo directory. Name the file as output.tf, and copy/paste the code below. The Terraform output configuration file below provides AWS resources such as AWS EC2 instance arn, and public IP address after executing the terraform apply command.

Can Terraform use environment variables?

Terraform refers to a number of environment variables to customize various aspects of its behavior. None of these environment variables are required when using Terraform, but they can be used to change some of Terraform's default behaviors in unusual situations, or to increase output verbosity for debugging.

How do you pass output value in Terraform?

Terraform Output Command To get the raw value without quotes, use the -raw flag. To get the JSON-formatted output, we can use the -json flag. This is quite useful when we want to pass the outputs to other tools for automation since JSON is way easier to handle programmatically.


1 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.)

like image 53
Martin Atkins Avatar answered Oct 22 '22 06:10

Martin Atkins