Prompt

How do I separate secrets and environment config in Terraform?

Technology / Cloud Infrastructure1 observationLast seen Oct 25, 2026

Latest observation

Oct 25, 2026OpenAI APIWeb search: off

A good rule of thumb is:

  • Terraform state should not contain secrets unless you’re comfortable protecting state like a secret store.
  • Environment config (region, instance sizes, feature flags, account IDs, etc.) is fine to pass as variables.
  • Secrets should usually come from a secret manager or CI/CD environment, not hardcoded in .tf files.

Recommended patterns

1) Use input variables for non-secret environment config

Put config in variables.tf and supply values per environment.

variable "region" {
  type = string
}

variable "instance_type" {
  type = string
  default = "t3.micro"
}

Then set values via:

  • terraform.tfvars
  • *.auto.tfvars
  • CLI -var
  • CI/CD pipeline variables
  • workspace-specific variable files

Example prod.tfvars:

region        = "us-east-1"
instance_type = "t3.small"

Run:

terraform apply -var-file=prod.tfvars

2) Treat secrets differently from config

Prefer pulling secrets from an external secret store:

  • AWS Secrets Manager / SSM Parameter Store
  • Azure Key Vault
  • Google Secret Manager
  • Vault
  • CI/CD secret variables

Example using AWS Secrets Manager:

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

locals {
  db_secret = jsondecode(data.aws_secretsmanager_secret_version.db.secret_string)
}

Then use local.db_secret.password where needed.

3) Mark sensitive variables

If you must pass a secret into Terraform, declare it sensitive:

variable "db_password" {
  type      = string
  sensitive = true
}

This helps hide it in plan/apply output, but does not prevent it from being stored in state if used in a resource argument.

4) Keep secrets out of .tfvars when possible

Avoid committing secret values in:

  • terraform.tfvars
  • *.tfvars
  • module defaults
  • plain text .tf files

If you must use *.tfvars, keep them out of git and load them locally only.

5) Use separate files for separation of concerns

A common structure:

terraform/
  main.tf
  variables.tf
  outputs.tf
  env/
    dev.tfvars
    prod.tfvars

And secrets come from:

  • environment variables
  • secret manager data sources
  • CI/CD secret injection

6) Be aware of state sensitivity

Even if a value is marked sensitive, Terraform state may still contain it. So:

  • encrypt remote state
  • restrict access to state backend
  • use backend-specific encryption and IAM controls
  • avoid outputting secrets

Example pattern

variable "environment" {
  type = string
}

variable "region" {
  type = string
}

variable "db_password" {
  type      = string
  sensitive = true
}

prod.tfvars:

environment = "prod"
region      = "us-east-1"

Secret supplied separately:

terraform apply \
  -var-file=prod.tfvars \
  -var='db_password=supersecret'

Better: pass it through CI secrets or a secret manager rather than the command line.

Practical guidance

  • Config: tfvars, workspace vars, module inputs
  • Secrets: secret manager, CI secret store, env vars, Vault
  • Always: protect state, avoid committing secrets, mark secret variables sensitive

If you want, I can show you:

  1. a best-practice folder layout for dev/stage/prod, or
  2. an example using AWS Secrets Manager / SSM / Vault.

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.