I understand that we can access resource attributes, data sources attributes, variables, local variables and string constants in outputs. However in some cases, I want to directly use environment variables to create some output. For instance, if I have to provide a summary of execution like
$start_time = // Set the start time before running terraform apply.
output “process_summary” {
value = “The terraform execution started at $start_time.”
}
I can’t do it. The workaround is I create a variable like TF_VAR_start_time and define that in my variables section. I was wondering if the outputs are allowed to be more flexible to use environment variables rather than forcing everything to be a variable
I’m not aware of any way of getting access to variables from the shell environment in the configuration language, except via the TF_VAR_* approach you already mentioned.
I’m curious about what your goal is here. Do you want to include these environment variable values in the state file for some reason?
Thank you for responding. Yes for each resource creations, we would want to add extra metadata as outputs, which can be used for rendering in an application we develop. So these outputs are more of helper content for a generic application that I want
@psaddi Terraform does not allow to use an undeclared variable. So the variable you want to use anywhere in the code (not only in the output value) must be declared before the run.
variable "foo" {}
output test {
value = var.foo
}
But then you can use environment variable from your shell to assign the needed value
$ export TF_VAR_foo="$(date) from shell"
$ terraform apply -auto-approve
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
test = Mon 14 Sep 2020 01:17:31 EEST from shell