Prompt

How do I fix secrets handling in infrastructure code?

Technology / Cloud Infrastructure1 observationLast seen Oct 25, 2026

Latest observation

Oct 25, 2026OpenAI APIWeb search: off

Secrets handling in infrastructure code usually goes wrong in a few predictable ways: secrets get committed to Git, passed through plain-text variables, logged during deploys, or copied into multiple places manually. The fix is to centralize secret storage, inject secrets at runtime, and make sure your IaC only references secret locations—not the secret values themselves.

Good rules of thumb

  1. Never hardcode secrets in IaC

    • Don’t put passwords, API keys, private keys, tokens, or connection strings directly in Terraform/CloudFormation/Pulumi/CDK templates.
  2. Store secrets in a dedicated secret manager

    • Use something like:
      • AWS Secrets Manager or SSM Parameter Store
      • Azure Key Vault
      • GCP Secret Manager
      • HashiCorp Vault
    • Keep secrets encrypted at rest and access-controlled.
  3. Pass references, not values

    • Your infrastructure code should point to:
      • secret names
      • secret ARNs/IDs
      • secret version references
    • The application or runtime environment retrieves the secret when needed.
  4. Use runtime injection

    • Inject secrets into containers, VMs, serverless functions, or CI/CD jobs at deploy/runtime via:
      • environment variables from secret stores
      • mounted secret files
      • cloud-native secret integrations
    • Avoid baking secrets into images or immutable artifacts.
  5. Limit who/what can access secrets

    • Use least privilege IAM/RBAC.
    • Separate read access for deploy systems vs application runtime.
    • Restrict developers from broad secret access unless required.
  6. Prevent secret leakage in logs and state

    • Mark outputs as sensitive.
    • Don’t print env vars or command lines containing secrets.
    • Be careful with Terraform state: it can contain secret values.
    • Encrypt state and lock down state storage access.
  7. Rotate secrets regularly

    • Use short-lived credentials where possible.
    • Automate rotation and update consumers safely.
  8. Scan for secret exposure

    • Use pre-commit hooks, secret scanning in CI, and repository scanning tools.
    • Add policy checks to fail builds if secrets are found.

If you’re using Terraform

Terraform is a common place where secrets leak.

Avoid

variable "db_password" {
  default = "supersecret"
}

Better

variable "db_password" {
  sensitive = true
}

But note: sensitive = true only hides it in CLI output; it does not prevent it from being stored in state.

Better pattern

  • Read secret values from a secret manager data source
  • Or pass them at apply time from a secure pipeline secret store
  • Or have the app fetch them at runtime instead of Terraform managing the value directly

Example:

data "aws_secretsmanager_secret_version" "db" {
  secret_id = "prod/db"
}

locals {
  db_password = jsondecode(data.aws_secretsmanager_secret_version.db.secret_string)["password"]
}

Be careful: this still puts the secret into Terraform state if Terraform reads it.

If possible, use Terraform to:

  • create the secret container/resource
  • grant permissions
  • configure consumers to fetch it directly
  • avoid ever reading the secret value into Terraform when you can

Best practice architecture

A solid pattern looks like this:

  1. IaC creates the secret resource
    • e.g. aws_secretsmanager_secret
  2. CI/CD writes the secret value
    • from a protected pipeline variable or external secret source
  3. App runtime reads the secret
    • using IAM role / workload identity
  4. Terraform never stores the secret plaintext in state
    • only the secret reference/metadata

Common mistakes to avoid

  • Committing .env files to Git
  • Putting secrets in user data scripts
  • Using default provider debug logs in CI
  • Storing secret values in Terraform outputs
  • Passing secrets through many layers of modules
  • Reusing the same long-lived credential everywhere
  • Keeping secrets in plain text in cloud-init, Helm values, or Kubernetes manifests

If this is Kubernetes

Use:

  • External Secrets Operator
  • Sealed Secrets
  • CSI secret store drivers
  • Native cloud secret integrations

Avoid:

  • plaintext Secret manifests in Git unless encrypted or sealed
  • embedding secrets into container images
  • mounting secrets via ConfigMaps

A practical checklist

  • Remove hardcoded secrets from code and templates
  • Move secrets to a secret manager
  • Replace plaintext vars with secret references
  • Ensure IaC state is encrypted and access-controlled
  • Mark sensitive outputs as sensitive
  • Disable/avoid debug logging that prints secrets
  • Add secret scanning in CI and Git hooks
  • Rotate any leaked credentials
  • Use short-lived auth where possible

If you want, I can give you a tool-specific fix for Terraform, Pulumi, CloudFormation, Kubernetes, or GitHub Actions.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.