Q: How do you manage Terraform provider credentials without hardcoding them?
To manage Terraform provider credentials without hardcoding them, keep secrets out of .tf files and state-adjacent code and let the provider obtain short-lived credentials from its environment: instance or workload identity, OIDC.
Want to master this scenario in a live sandbox? KodeKloud's HashiCorp Certified Terraform Associate (003) Interactive Labs covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: To <strong>manage Terraform provider credentials</strong> without hardcoding them, keep secrets out of <code>.tf</code> files and state-adjacent code and let the provider obtain short-lived credentials from its environment: instance or workload identity, OIDC federation from CI, an SSO profile or a secrets manager such as Vault. Interviewers ask this because leaked access keys in Git are a common incident cause, and because Terraform state itself can contain secrets. The best answer prefers identity-based, short-lived credentials over long-lived keys, scopes roles tightly per environment and protects the state backend.
Mental model: how a provider finds credentials
Providers read credentials from a defined chain, so your job is to supply one without writing secrets into code.
- Provider block: keep it free of
access_keyandsecret_key. Only non-secret settings such as region andassume_rolebelong there. - Environment chain: the AWS provider checks environment variables, shared config and credentials files, SSO, container credentials and instance metadata, in order.
- Best order of preference: workload identity (EC2 role, IRSA, Pod Identity) or OIDC federation, then SSO profiles for humans, then Vault-issued dynamic keys. Static keys are last resort.
- Other clouds: the same idea applies: Azure managed identity or workload identity federation, Google workload identity federation or impersonation.
- Kubernetes and Helm providers: fetch cluster credentials via an exec plugin (for example
aws eks get-token) instead of embedding tokens. - Backend credentials: the state backend needs its own access path; configure it through the same environment chain rather than keys in the backend block.
Hands-on: provider credentials without hardcoding them
Use assume_role and a named profile locally, and OIDC in CI so no secret is stored at all.
provider "aws" {
region = var.region
assume_role {
role_arn = "arn:aws:iam::111122223333:role/terraform-prod"
session_name = "terraform"
}
}
variable "db_password" {
type = string
sensitive = true
}
# Local workflow
# aws sso login --profile prod-admin
# export AWS_PROFILE=prod-admin
# aws sts get-caller-identity
# export TF_VAR_db_password="$(vault kv get -field=password secret/prod/db)"
# terraform plan
- Local developers: run
aws sso login --profile prod-adminthen exportAWS_PROFILE; credentials expire automatically. - Assume role: an
assume_roleblock in the provider lets one identity switch into a per-environment role with limited rights. - Confirm identity:
aws sts get-caller-identitybefore plan prevents applying to the wrong account. - Non-AWS secrets: pass sensitive variables through
TF_VAR_nameenvironment variables, not committed.tfvarsfiles.
Automation: OIDC federation from CI, no stored keys
With OpenID Connect, the pipeline proves its identity to the cloud and receives temporary credentials for one job.
# .github/workflows/terraform.yml (excerpt)
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::111122223333:role/terraform-plan
aws-region: us-east-1
- run: terraform init -input=false && terraform plan -input=false
- Trust policy: create an IAM OIDC provider for the CI system and a role whose trust policy restricts the repository, branch or environment claims.
- GitHub Actions: grant
permissions: id-token: writeand useaws-actions/configure-aws-credentialswithrole-to-assume. - Least privilege: use a read-only plan role for pull requests and a separate apply role limited to the protected branch or environment.
- No secrets to rotate: there are no stored access keys in repository settings, removing the largest leak surface.
- Fork protection: never grant apply credentials to pull requests from forks; run those with read-only or no cloud access.
Failure modes: secrets still leak through state and logs
Removing hardcoded keys is only half the job, because Terraform records resource attributes in state.
- State contains secrets: passwords, generated keys and some data source values are stored in plain text in state. Use an encrypted backend with tight access and versioning.
- sensitive does not encrypt: marking a variable or output
sensitive = truehides it from CLI output only; it still exists in state. - Logs and plans: avoid printing plan files or
TF_LOG=DEBUGoutput in CI artifacts, which can expose tokens. - Secrets managers: prefer letting the application fetch secrets at runtime from Secrets Manager, Vault or External Secrets rather than injecting values through Terraform. Newer Terraform releases add ephemeral values to avoid state storage; check your version.
- Rotation: if a long-lived credential is unavoidable, store it in a secrets manager, rotate on a schedule and alert on its use from unexpected networks.
Governance and audit of provider access
Credentials are only as safe as the policy around them.
- Separate accounts: use one cloud account per environment with its own Terraform role, so a compromised dev pipeline cannot reach prod.
- Audit trails: use
session_namein assume_role and review CloudTrail events to see which pipeline run performed which change. - Pre-commit scanning: run gitleaks or detect-secrets in a pre-commit hook and in CI to block commits that contain keys.
- Approval gates: require manual approval for apply on protected environments so credentials with write power are only used after review.
- I keep provider blocks free of secrets and let the provider pull short-lived credentials from workload identity, SSO profiles, assume_role or OIDC federation.
- I verify with aws sts get-caller-identity before plans, and secret scanners in CI to ensure nothing is committed.
- I automate with GitHub OIDC roles per environment, an encrypted versioned state backend and Vault or Secrets Manager for any remaining secret values.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Committing terraform.tfvars with keys or passwords Git history keeps the secret forever; use environment variables or a secrets manager and rotate anything already leaked.
- Assuming sensitive = true protects state It only redacts CLI output; the values remain in the state file, so the backend must be encrypted and access-controlled.
- One broad admin role for every environment and for PR plans A malicious pull request or a mistaken apply can then reach production; use separate least-privilege roles per environment and per action.
- Long-lived static access keys in CI secrets They must be rotated, can leak through logs, and are valid from anywhere; OIDC issues credentials that expire after the job.