Terraform output variables

Terraform supports output values, which are useful for providing relevant information about your infrastructure configuration both to other Terraform resources as well as administrators. Octopus supports these output values natively through output variables in your deployment process.

Output variable handling

Terraform’s output variables are captured as Octopus variables after a template is applied. Each output variable is captured in two different formats: the JSON representation of the variable, and the value only of the variable.

The JSON representation of the output variable is the result of calling terraform output -json variablename. For example, the JSON representation of a string output variable (which would appear in the logs as a message similar to Saving variable "Octopus.Action[Apply Template].Output.TerraformJsonOutputs[test]" with the JSON value of "test") would look similar to this:

{
    "sensitive":  false,
    "type":  "string",
    "value":  "hi there"
}

While the value only output (which would appear in the logs as a message similar to Saving variable "Octopus.Action[Apply Template].Output.TerraformValueOutputs[test]" with the value only of "test") would look similar to this:

"hi there"

Accessing Terraform output variables

Using the previous example output variable called test you can access the output using PowerShell as follows:

$value = $OctopusParameters["Octopus.Action[Apply Template].Output.TerraformValueOutputs[test]"]

// OR

$value = $OctopusParameters["Octopus.Action[Apply Template].Output.TerraformJsonOutputs[test]"] | ConvertFrom-Json  | select -ExpandProperty value

The syntax for accessing JSON variables as covered by our documentation here applies to both TerraformJsonOutputs as well as TerraformValueOutputs. However the latter is less useful as it can also be a primitive value. In this case Octostache won’t know that it should deserialize the value and will provide you with a JSON encoded result. It is therefore recommended to prefer TerraformJsonOutputs where possible. The following syntax can be used to access the value using the binding syntax:

#{Octopus.Action[Apply Template].Output.TerraformJsonOutputs[test].value}

Help us continuously improve

Please let us know if you have any feedback about this page.

Send feedback

Page updated on Sunday, January 1, 2023