How do you implement Terraform in a CI/CD pipeline safely?
Running Terraform in CI/CD requires careful guardrails:
- PR triggers plan: On every pull request, run
terraform planand post the output as a PR comment (using tools like Atlantis orterraform-pr-commenter). - Merge triggers apply: Only apply after PR is merged to main. Require manual approval for production.
- State locking: Ensure DynamoDB locking is configured to prevent concurrent applies.
- OIDC credentials: Use OIDC to get short-lived tokens from AWS instead of storing long-lived access keys.
- Plan artifacts: Save the plan file and apply that exact file — never re-plan at apply time.