Do Terraform workspaces isolate provider credentials for repeatable developer environments
0 reputation · 26 May 2020, 11:56 UTC
0 reputation · 26 May 2020, 11:56 UTC
Goal is a repeatable development environment for Terraform across developer machines and CI using a single module composition with version-pinned modules, explicit input variables, and a remote state backend with locking.
Workspaces are considered to provide per-developer or per-branch states without duplicating code. The configuration and provider schema are shared across workspaces, so credential, region or account differences must be supplied via variables. Variable defaults can mask differences between local and shared environments, and Terraform does not automatically detect external drift.
Uncertainty remains about the isolation guarantees of workspaces when provider configuration depends on variables.
Does workspace selection change provider configuration evaluation? Which variable file loading strategy ensures credentials do not leak between workspaces? What behavior occurs when a variable default differs from a workspace-specific value?
34775 reputation · 26 May 2020, 17:26 UTC
No. Selecting a Terraform workspace does not isolate provider credentials. Workspaces isolate state, not provider configuration. The same provider blocks and configuration are evaluated for every workspace; only the state file path changes. Credential, region or account differences must be supplied via variables, and without explicit per-workspace variable loading the same values will be used across workspaces.
terraform workspace show changes the state target, not the configuration.access_key = var.aws_access_key, the provider will use whatever value the variable resolves to for that run. Workspace selection alone does not re-evaluate the provider.-backend-config or backend-specific variables. Workspaces do not create separate backend credentials.It is common to assume workspaces provide per-developer isolation because state is isolated. In practice the configuration graph, provider schema and provider blocks are identical across workspaces. Variable defaults mask differences: if a variable has a default and no workspace-specific value is supplied, Terraform uses the default for every workspace, which can unintentionally leak the same credentials.
Use workspace-scoped variable values and avoid defaults for secrets.
terraform apply -var-file=dev.tfvars or terraform apply -var-file=terraform.tfvars.d/${TF_WORKSPACE}.tfvars. Terraform loads terraform.tfvars then terraform.tfvars.json then any *.auto.tfvars in terraform.tfvars.d in alphabetical order.TF_VAR_* per developer or per branch and never commit them. Keep a *.example.tfvars with non-sensitive placeholders only.When a variable default differs from a workspace-specific value, Terraform uses the workspace-specific value if it is supplied via the loading strategy above. If it is not supplied, the default is used for that run. There is no automatic detection of external drift and no automatic switching of provider credentials on workspace change.
terraform workspace new dev
terraform workspace new feature-branch
# set different values for a provider-referenced variable
terraform plan -var-file=dev.tfvars
terraform workspace select feature-branch
terraform plan -var-file=feature-branch.tfvars
Check the plan output for the provider configuration and confirm state isolation with terraform state list after each switch.
Missing diagnostic detail: Are your provider credentials currently hard-coded in the provider block or referenced via variables / TF_VAR_? The recommended isolation strategy changes if credentials are hard-coded, because hard-coding makes them identical across all workspaces regardless of variable files.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.