Skip to content

Lesson 8 of 9 · 24m

Identity, Secrets, and Policy Guardrails

Least-privilege automation, secret handling, and policy-as-code guardrails that stop dangerous changes before they reach apply.

Terraform is a privileged control plane. Whatever identity runs it can potentially create users, alter networks, read data stores, or disable logging. If that identity is a shared administrator key in CI, you have built a very efficient compromise machine. Scaling Terraform means scaling identity boundaries first.

Separate plan and apply permissions where practical

Planning often needs broad read access; applying needs write access to a bounded set of services. They do not have to be the same role. Even when provider APIs make perfect separation difficult, you can scope production write roles to protected branches and controlled environments.

{
  "Effect": "Allow",
  "Action": [
    "ec2:Describe*",
    "iam:Get*",
    "s3:Get*"
  ],
  "Resource": "*"
}

That read-only policy is not a complete production role — it is an illustration of the shape. Write permissions should be tighter and service-specific. Wildcards are sometimes necessary, but every wildcard deserves a reason.

Secrets do not belong in Terraform just because variables exist

Terraform can provision secret containers or rotate integration configuration, but raw secrets should usually live in a dedicated secret manager: AWS Secrets Manager, Azure Key Vault, Google Secret Manager, Vault, or your platform equivalent. Use data sources or runtime injection patterns carefully, and remember that any secret Terraform reads may end up in state unless the provider marks and handles it correctly.

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

That can be appropriate in narrow cases, but it is not automatically safe. Review provider documentation for sensitivity and state behavior before adopting the pattern wholesale.

Policy as code catches what review misses

Humans are bad at repeatedly noticing missing encryption, overly broad CIDR blocks, public buckets, or unlogged privileged roles. Open Policy Agent with Conftest, HashiCorp Sentinel, Checkov, or cloud-specific guardrails can evaluate plans or configuration:

deny[msg] {
  resource := input.resource_changes[_]
  resource.type == "aws_security_group"
  ingress := resource.change.after.ingress[_]
  ingress.cidr_blocks[_] == "0.0.0.0/0"
  ingress.to_port == 22
  msg := "SSH must not be open to the world"
}

Policy should explain the business reason and the safe path. Deny-by-riddle produces bypass culture. Allow documented exceptions with expiry dates and ownership.

Least privilege is iterative, not a fantasy baseline

Start with high-risk boundaries: production write access, IAM administration, network administration, state access. Record denied actions during controlled rollout and tighten in stages. A perfect zero-standing-privilege design that blocks incident response will be circumvented; a staged system people can actually use is safer.

Field notes

  • State read access can expose secrets even if secret values never appear in repository files.
  • CI roles should be assumable only from expected repositories, branches, and audience claims.
  • Scan Terraform repositories for committed credentials, but assume scanning is cleanup rather than prevention.
  • Policy exceptions without expiration become permanent architecture by accident.

Was this lesson helpful?

Sign in to rate