⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Terraform & IaC Interview Questions Scenario 30 of 120 in Terraform & IaC
Staff SRE / Principal Architect [L3] Terraform Terraform Modules & State Staff SRE Scenario [L3]
🎯 Target Role / Context: Terraform Scenario #25 (L3) · Use VPC ID from another module

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.

#Terraform #L3 #IaC #Cloud Infrastructure #EC2
🎙️ Candidate Opening & Architectural Context
"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."
Advertisement
⚡ Recommended Practice Lab

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.

1️⃣

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_key and secret_key. Only non-secret settings such as region and assume_role belong 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.
2️⃣

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-admin then export AWS_PROFILE; credentials expire automatically.
  • Assume role: an assume_role block in the provider lets one identity switch into a per-environment role with limited rights.
  • Confirm identity: aws sts get-caller-identity before plan prevents applying to the wrong account.
  • Non-AWS secrets: pass sensitive variables through TF_VAR_name environment variables, not committed .tfvars files.
Advertisement
3️⃣

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: write and use aws-actions/configure-aws-credentials with role-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.
4️⃣

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 = true hides it from CLI output only; it still exists in state.
  • Logs and plans: avoid printing plan files or TF_LOG=DEBUG output 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.
Pro Tip: Scan the repository history with a tool such as gitleaks and rotate any credential that was ever committed, since deleting the line does not remove it from history.
5️⃣

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_name in 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.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Prefer identity federation (OIDC, workload identity, SSO) over any stored key, and separate plan and apply roles; also remember that sensitive = true does not stop secrets from landing in state."
⚡ 60-Second Elevator Pitch Talking Points
  • 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.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
Advertisement
Want more Terraform & IaC scenarios?
Explore our complete collection of scenario-based Terraform & IaC interview runbooks.
Browse All Terraform & IaC Questions →