If a workspace has a hard dependency on another, I recommend making this dependency explicit by using HCP Terraform variable sets, which enable sharing values between workspaces. When you update a variable set, the changes automatically apply to all workspaces that use it.
Aim to model your infrastructure as code and your HCP Terraform environment similarly to how you model your cloud. For instance, just as a workload might depend on a VPC in a different account or project, you should create a dedicated workspace for the VPC. Define a variable set with values like VPC_ID
and share it with the dependent workload workspace. This approach ensures dependencies are visible and well-defined, maintaining clarity and structure across your environment.
Creating a variable set:
resource "tfe_variable_set" "network" {
name = "network"
description = "Contains network/vpc related variables"
organization = "XXXXXXXXXXXXXXX"
}
Attaching a variable to the variable set:
resource "tfe_variable" "vpc_id" {
key = "vpc_id"
value = aws_vpc.example.id
category = "terraform"
variable_set_id = tfe_variable_set.network.id
}
Finally, share the variable set with another HCP Terraform workspace. This ensures that the targeted workspace receives and uses the variables:
resource "tfe_workspace_variable_set" "workload" {
variable_set_id = tfe_variable_set.network.id
workspace_id = tfe_workspace.workload_xyz.id
}