In HCP Terraform, how do dynamic provider credentials replace stored AWS keys, and why might a run's plan authenticate while its apply fails?
answer
- a signed identity for each phase
- trust set up once, nothing stored
- read the sub claim's last segment
- plan and apply can use different roles
basics
~20 sHCP Terraform sends a signed OIDC token for each plan and apply; AWS verifies it and returns temporary credentials. The token's sub claim names the run phase, so a trust policy matching only plan rejects the apply.
solid answer
~40 sYou set up a trust relationship once, then add workspace environment variables: `TFC_AWS_PROVIDER_AUTH=true` and a role ARN. For each plan and each apply, HCP Terraform generates a workload identity token, sends it to AWS, gets temporary credentials and puts them in the run environment, discarding them when that phase ends. No long-lived key is stored. A plan can succeed while the apply fails because the phases are authenticated separately. The token's `sub` claim ends in `run_phase:plan` or `run_phase:apply`, so a trust policy with `StringEquals` on the plan phase rejects the apply token. The other usual causes are a wrong `TFC_AWS_APPLY_ROLE_ARN`, or setting only `TFC_AWS_PLAN_ROLE_ARN` with no `TFC_AWS_RUN_ROLE_ARN` for the apply to fall back on.
code
hcl · 20 linesvariable "tfc_aws_dynamic_credentials" {
description = "Object containing AWS dynamic credentials configuration"
type = object({
default = object({
shared_config_file = string
})
aliases = map(object({
shared_config_file = string
}))
})
}
provider "aws" {
shared_config_files = [var.tfc_aws_dynamic_credentials.default.shared_config_file]
}
provider "aws" {
alias = "ALIAS1"
shared_config_files = [var.tfc_aws_dynamic_credentials.aliases["ALIAS1"].shared_config_file]
}go deeper
Remember the idea: no stored key, and a new temporary credential for each plan and each apply, granted because the cloud platform trusts a signed token.
Name the variables, TFC_AWS_PROVIDER_AUTH and the run, plan and apply role ARNs, and explain the fallback to the run role.
Read the sub claim when a phase fails, separate rejected trust from denied permissions, and catch a rename or project move that breaks an exact match.
Use phase-specific roles as a standing rule: a narrow plan role limits what pull request code can reach, at the cost of maintaining two trust policies per workspace.
## The problem being solved Static cloud keys stored in a workspace remain a security risk even if you rotate them regularly: someone has to manage them, and they outlive any single run. **Dynamic provider credentials** remove the stored key. Each plan and each apply gets new temporary credentials instead. This mechanism is HCP Terraform's own. It belongs to the **HCP Terraform workspace** and its runs; a pipeline in a CI system obtains federated credentials through that system instead. ## How the flow works You configure a trust relationship between the cloud platform and HCP Terraform once. Then, for each plan and apply: 1. HCP Terraform generates a **workload identity token**, an OIDC-compliant JSON Web Token signed by its private key. 2. When the phase begins, it sends the token to the cloud platform. 3. The platform verifies the token with HCP Terraform's public signing key. 4. If verification succeeds, the platform returns fresh temporary credentials, which HCP Terraform sets up in the run environment for the provider to use. 5. When the phase completes, the run environment is torn down and the credentials are discarded. The token expires at the end of the plan or apply timeout, and for AWS the provider's authentication is valid for the length of that phase. ## The AWS configuration On AWS you create an OIDC identity provider and a role whose trust policy matches the token. In the workspace, or in a variable set shared across workspaces, you add environment variables: | Variable | Purpose | |---|---| | `TFC_AWS_PROVIDER_AUTH` | Must be `true`, or HCP Terraform does not attempt to authenticate | | `TFC_AWS_RUN_ROLE_ARN` | Role to assume; optional only if both phase-specific roles are set | | `TFC_AWS_PLAN_ROLE_ARN` | Role for the plan phase; falls back to the run role | | `TFC_AWS_APPLY_ROLE_ARN` | Role for the apply phase; falls back to the run role | | `TFC_AWS_WORKLOAD_IDENTITY_AUDIENCE` | Sets the `aud` claim; defaults to `aws.workload.identity` | None of these values is a credential: they are a flag, role ARNs and an audience, and the provider block carries no key. The token's `sub` claim is the fully qualified workspace path followed by the phase: ```text organization:my-org:project:Default Project:workspace:my-workspace:run_phase:apply ``` ## Why the plan works and the apply fails Because the phases authenticate separately, a run can get through one and fail at the other. Check these in order: - **Trust policy pinned to one phase.** With `StringEquals` on `run_phase:plan`, the apply token does not match. A role shared by both phases needs `StringLike` with `run_phase:*`. - **Wrong or missing apply role.** Check `TFC_AWS_APPLY_ROLE_ARN`. If only `TFC_AWS_PLAN_ROLE_ARN` is set and `TFC_AWS_RUN_ROLE_ARN` is absent, the apply has nothing to fall back on. - **Separate trust on the apply role.** Two roles mean two trust policies, each of which must match its own phase. - **Authorisation, not authentication.** If the role is assumed and a resource call is then denied, the token worked. That is the role's permissions, which is a different fix. The separation is also a feature. A read-oriented plan role limits what a pull request's speculative plan can do, because that plan runs the branch's code with plan-phase credentials. ## Failures that hit both phases - **A rename or a move.** The `sub` claim contains project and workspace names, so an exact match stops working after either changes. - **Leftover provider arguments.** Other authentication arguments on the provider may interfere. Supply `region` or `AWS_REGION` and leave the rest out. - **Old agents.** Self-hosted HCP Terraform agents need v1.7.0 or newer, and v1.12.0 or newer for multiple configurations. - **No run environment.** In Local execution mode HCP Terraform does not evaluate workspace variables, and `terraform import` runs locally, so neither gets these credentials. ## Scoping trust A **claim** is a named field inside the token, which AWS can read once the signature is verified. HashiCorp's minimum is to check the audience and the organization name, which stops other HCP Terraform organizations using the role. Matching on project, workspace and phase narrows it further, so a production role trusts only the production workspace's apply. Custom claims such as `terraform_run_id` and `terraform_run_phase` also help when you trace which run assumed a role and why a request was rejected.
- What should an AWS trust policy check on an HCP Terraform workload identity token at the very least?At minimum the audience and the organization name, or tokens from other HCP Terraform organizations could use the role. Project, workspace and run phase narrow it further. All of this is read from claims once AWS has verified the token's signature with HCP Terraform's public key.
- Why does terraform import fail against AWS in an HCP Terraform workspace that uses dynamic credentials?HCP Terraform does not support remote execution for the `terraform import` command. It runs locally with the workspace acting only as a state backend, so workspace environment variables are not available and no run environment receives credentials. Supply credentials locally, or prefer `import` blocks, which are planned in a normal run.
- How can one HCP Terraform workspace deploy into two AWS accounts with dynamic credentials?Add tagged variables, such as `TFC_AWS_PROVIDER_AUTH_[TAG]` and `TFC_AWS_RUN_ROLE_ARN_[TAG]`. Declare the `tfc_aws_dynamic_credentials` variable, then map each provider block's `shared_config_files` to its entry, including the default provider. Each configuration gets a distinct token. Self-hosted agents need v1.12.0 or newer.
- HCP Terraform issues one credential set at run start, shared by plan and apply.
- TFC_AWS_RUN_ROLE_ARN alone turns on dynamic credentials without TFC_AWS_PROVIDER_AUTH.
- Regularly rotated static keys carry no risk that dynamic credentials remove.
- A trust policy only needs to confirm HCP Terraform signed the token.
- A Local execution mode workspace hands the same temporary credentials to my laptop.
- A denied resource call during apply proves the workload identity token was rejected.