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).