79082841

Date: 2024-10-13 09:45:09
Score: 1
Natty:
Report link

You can dynamically load variables in Terraform based on the workspace using terraform.workspace without external scripts or wrappers. Here's how:

Create Workspace-Specific Variable Files: Example files: dev.tfvars, prod.tfvars, etc.

Use the Workspace to Load Variables:

terraform apply -var-file="${terraform.workspace}.tfvars"

Alternatively, use conditional logic in main.tf:

locals { instance_type = terraform.workspace == "prod" ? "t3.large" : "t2.micro" }

This auto-loads variables when switching workspaces (terraform workspace select dev).

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Contains question mark (0.5):
  • Low reputation (0.5):
Posted by: Shahbaz Rahmat