Set the Terraform Cloud variables across Projects and Workspaces from an output file.
When you need to share Terraform Output values across workspaces,
terraform_remote_state data source is the primary way, but this feature can
create dependencies between workspaces and then make Terraform less manageable.
However, manually registering Output values from other workspaces is not only
labor-intensive but also carries the risk of operational errors. This tool
allows semi-automatic registration of variables without creating dependencies
between workspaces.
- Outputs with type of
string,numberorboolare created/updated ashcl = falseand the others ashcl = true.- Reference:
Types and Values
- Reference:
- All variables are registered...
- as
Non sensitive, so please be careful not to specify sensitive output values. - as category of
terraform(NOTenvironment variables).
- as
- Update operation does NOT take into account whether the value was really updated or not. In other words, this operation makes the value at the target workspace and the value in the output file idempotent.
- Export environment variables
- Terraform Cloud token as
TFVE_TOKEN - (Optional) Terraform Cloud organization name as
TFVE_ORGANIZATION_NAME- Required if
--show-workspacesflag is set.
- Required if
- Terraform Cloud token as
- Generate output values file with
terraform output --json- Values in this file are created or updated as variables with specified name and description.
- Specify outputs to be exported to other workspaces. See Below for details.
Export list defines variables to be exported to other workspaces.
The list is specified in the format as below:
<Output name>,<Variable name>,<Variable description>
where <Output name> is the name of the output in the output file and
<Variable name> and <Variable description> are the name and its description
of the variable at the destination.
Note that only the variables listed in this list are exported.
Example:
If you need to export the value of output such as:
output "my_output" {
description = "my output"
value = "some value"
}
output "my_output_2" {
description = "my output 2"
value = "some value 2"
}define the export list as follows.
# This line is ignored as a comment
my_output,my_var,this_is_description
# Description is optional
my_output_2,my_var_2
Then the value of my_output is created or updated as my_var with the
description this_is_description at the targeted workspace(s).
As well, the value of my_output_2 is created or updated as my_var_2 without
description.
REMARK:
- Updating is allowed by using the
--allow-updateflag. - To comment out a whole line, use
#.
Usage: tfvar-export [OPTIONS] [PATH_TO_OUTPUT_VALUES_FILE] [PATH_TO_EXPORT_LIST]
Arguments:
[PATH_TO_OUTPUT_VALUES_FILE] Path to the output values file generated with
`terraform output --json`.
Required unless `--show-workspaces` is set.
[PATH_TO_EXPORT_LIST] Path to the export list.
Required unless `--show-workspaces` is set.
Options:
-b, --base-url <BASE_URL>
Base URL of Terraform API [default: https://app.terraform.io]
-t, --target-workspaces <WORKSPACE_NAME1,WORKSPACE_NAME2,...>
Comma separated Terraform Cloud workspace names.
Required unless `--show-workspaces` is set.
-q, --disable-log
Disable `Info` and `Warn` log.
Note that `Error` log is always enabled regardless of this flag.
-w, --show-workspaces
Show available workspaces and exit.
-u, --allow-update
Allow update of existing values.
-h, --help
Print help
-V, --version
Print version
[
{
"terraform_workspace_id": "ws-xxxxxxxxxxxxxxxx",
"terraform_workspace_name": "ws-x",
"terraform_project": {
"terraform_project_id": "prj-xxxxxxxxxxxxxxxx",
"terraform_project_name": "pj-x"
}
},
{
"terraform_workspace_id": "ws-yyyyyyyyyyyyyyyy",
"terraform_workspace_name": "ws-y",
"terraform_project": {
"terraform_project_id": "prj-yyyyyyyyyyyyyyyy",
"terraform_project_name": "pj-y"
}
},
{
"terraform_workspace_id": "ws-zzzzzzzzzzzzzzzz",
"terraform_workspace_name": "ws-z",
"terraform_project": {
"terraform_project_id": "prj-zzzzzzzzzzzzzzzz",
"terraform_project_name": "pj-z"
}
}
]- Set the
TFVE_WORKSPACE_ID_TESTINGandTFVE_WORKSPACE_ID_TESTING2environment variable.- Testing dedicated workspaces should be used for safety.