Skip to content

Lesson 9 of 9 · 28m

Team Workflows: CI, Policy, and Safe Apply

Build pull-request driven Terraform operations with isolated credentials, reusable plan artifacts, policy checks, controlled applies, and auditability.

A solo Terraform workflow is a shell prompt. A team Terraform workflow is a control system. Once several people can change infrastructure, the question is no longer 'can Terraform apply this?' It is 'who reviewed it, under what identity, with what evidence, and how do we stop obvious harm before production?' Tools vary; the operating model does not.

Format and validate are the floor, not the ceiling

Every repository should enforce the basics automatically:

terraform fmt -check -recursive
terraform init -backend=false
terraform validate

These catch syntax and internal consistency. They do not prove cloud permissions, resource behavior, quota availability, or business safety. Treat them as compiler warnings, not deployment approval.

Plan on pull request, apply after merge

A practical pipeline is:

pull request opened
  -> fmt, validate, security scan
  -> terraform plan for affected roots
  -> post redacted human-readable summary
  -> run policy checks against plan JSON
merge to main
  -> re-plan or reuse immutable artifact
  -> apply with protected deployment identity
  -> archive state version, plan artifact, logs, and commit SHA

The branch plan is advisory. The merge apply is authoritative. If the world may change between them, re-plan or explicitly compare before apply.

Use short-lived deployment identity

Static cloud keys in CI are a gift to attackers. Prefer OIDC federation where the cloud supports it:

permissions:
  id-token: write
  contents: read

steps:
  - uses: aws-actions/configure-aws-credentials@v4
    with:
      role-to-assume: arn:aws:iam::123456789012:role/terraform-prod-deploy
      aws-region: us-east-1

Scope deployment roles narrowly by account, path, and environment. A role that can apply every stack can also destroy every stack.

Policy as code catches classes of mistakes

Plan JSON can be evaluated with Open Policy Agent or similar systems. For example, a policy can reject public ingress on production security groups:

package terraform.production

deny[msg] {
  rc := input.resource_changes[_]
  rc.type == "aws_security_group_rule"
  rc.change.after.type == "ingress"
  rc.change.after.cidr_blocks[_] == "0.0.0.0/0"
  msg := sprintf("public ingress is not allowed: %s", [rc.address])
}

Policy should encode organizational invariants, not personal taste. Good examples: no public databases, required encryption, approved regions, mandatory tags, no wildcard IAM in production. Bad examples: naming preferences that force endless exceptions without reducing risk.

Small roots are safer than heroic orchestration

Monorepos can still deploy small independent roots. Detect changed directories, map dependencies deliberately, and avoid inferring deployment order from file paths alone. A network change may require service re-plans, but blindly applying everything after every merge turns every mistake into a fleet-wide event.

Human approval belongs where blast radius is real

Production applies should require explicit approval when they delete data, replace network identity, alter IAM, or touch shared foundations. Approval is not a rubber stamp attached to a 2,000-line plan. Generate concise summaries that highlight destroys, replacements, privilege changes, public exposure, and stateful resources.

Handle failure as part of the workflow

When apply fails halfway, the pipeline must not blindly rerun on the next push. Mark the root unhealthy, require operator triage, preserve logs and state versions, and follow the recovery discipline from earlier lessons. Automation should amplify careful behavior, not automate panic.

Field notes

  • terraform validate does not contact the cloud and cannot prove arguments are valid for the provider API.
  • Never allow pull requests from untrusted forks to run with production credentials or state access.
  • Pin Terraform and provider versions; uncontrolled upgrades make the same configuration produce different behavior.
  • Keep apply credentials out of contributor shells. Humans review intent; deployment identity performs mutation.
  • If your summary hides destroy actions because they are noisy, your automation is optimized for speed over survival.

Was this lesson helpful?

Sign in to rate