A team asks for a workflow that deploys Terraform with a plan on PR and apply on merge.
AdvancedScenarioGitHub Actions
Answer
Situation read → Safe IaC pipeline.
What I'd do →
- On
pull_request:terraform fmt -check,validate,plan -out=tfplanand post the plan as a PR comment; use OIDC with a read-only role. - On
pushto main: environmentproductionwith required reviewers, apply the saved plan from the same commit with a write role. - Use remote state with locking (S3 + DynamoDB or S3 native lockfile) and a
concurrencygroup per state.
Why → Separate roles for plan and apply keep PRs from changing infrastructure.
Interview tip → Plan on PR, apply on merge, with different IAM roles.
More on GitHub Actions
What are workflows, jobs, steps and actions in GitHub Actions?BeginnerWhat events can trigger a workflow?BeginnerWhat is the difference between `pull_request` and `pull_request_target`?AdvancedWhat is a runner, and how do GitHub-hosted and self-hosted runners differ?BeginnerHow do you pass data between steps and between jobs?IntermediateHow does a build matrix work?Intermediate