Prerequisites
Before installing the Terraform Plan GitHub Action, ensure you have:
- An existing Terraform project in a GitHub repository
- Terraform installed (version 1.0 or higher recommended)
- AWS credentials configured (via OIDC or access keys)
- GitHub Actions enabled for your repository
Required permissions
The workflow running this action needs specific permissions to function correctly. Add these to your workflow file:
permissions:
contents: read # Required to read repository contents
pull-requests: write # Required to post comments on PRs
If you're using AWS OIDC authentication (recommended), you'll also need:
permissions:
contents: read
id-token: write # Required for AWS OIDC authentication
pull-requests: write
1) Create the workflow file
Create a new workflow file at .github/workflows/terraform-plan.yml in your repository:
name: Terraform Plan on Pull Request
on:
pull_request:
branches:
- main
jobs:
post-terraform-plan-comment:
name: Post Terraform Plan Comment to PR
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
pull-requests: write
env:
AWS_REGION: us-east-1
steps:
- name: Checkout Repository
uses: actions/checkout@v5
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionsRole
aws-region: ${{ env.AWS_REGION }}
- name: Terraform Init
run: terraform init
- name: Terraform Plan
run: terraform plan -out=tfplan.binary
- name: Post Terraform Plan Comment
uses: towardsthecloud/terraform-plan-pr-commenter@v1
with:
planfile: tfplan.binary
aws-region: ${{ env.AWS_REGION }}
2) Replace placeholder values
Update the following values in your workflow file:
- IAM Role ARN: Replace
arn:aws:iam::123456789012:role/GitHubActionsRolewith your actual GitHub Actions IAM role ARN. If you don't have one set up yet, check out our guide on configuring OpenID Connect for GitHub in AWS CDK. - AWS Region: Change
us-east-1to your target AWS region if different. - Branch: Adjust the
branchesfilter if your default branch isn'tmain. - Terraform version: If you need a specific Terraform version, add the
terraform_versioninput to thesetup-terraformstep.
3) Test the action
Create a test Pull Request that modifies your Terraform infrastructure:
- Create a new branch:
git checkout -b test-terraform-plan - Make a change to one of your Terraform resources
- Commit and push:
git push origin test-terraform-plan - Open a Pull Request against your main branch
The action will run automatically and post a comment with the Terraform plan output. The comment will show:
- Resources to be added (if any)
- Resources to be changed (if any)
- Resources to be destroyed (if any)
- A summary of all planned changes
4) Verify the output
Check your Pull Request for a new comment from the GitHub Actions bot. The comment should contain:
- A header identifying the plan
- The AWS region (if configured)
- Formatted sections for each type of change
- A summary of all infrastructure changes
If there are no infrastructure changes, the action will skip posting a comment to keep your PR conversations clean (when using the empty output conditionally).
Try it out
Want to experiment without setting up your own workflow? Check out the AWS Terraform Starter Kit. It's a production-ready Terraform template with this workflow already configured. Fork it, open a Pull Request with infrastructure changes, and see the action in action.
Next steps
- Learn about customizing inputs and outputs to fit your workflow
- Explore usage examples for different scenarios including reusable workflows
Best practices
Workflow organization
- Separate concerns: Keep plan generation and deployment in separate workflows
- Use reusable workflows: Create organization-level reusable workflows for consistency
- Use binary plan files: Always generate a binary plan file with
-out=tfplan.binaryfor accurate change detection - Enable parallel execution: Use job matrices or parallel jobs when possible
Security considerations
- Use OIDC over access keys: Always prefer AWS OIDC for authentication
- Minimize token permissions: Only grant required permissions to GitHub tokens
- Protect sensitive data: Never expose AWS account IDs or role ARNs in public repos
- Secure state files: Ensure Terraform state files are stored securely (e.g., S3 with encryption)
- Audit trail: Enable CloudTrail logging for all Terraform operations
Performance optimization
- Cache dependencies: Use
actions/cachefor Terraform providers and modules - Parallel environments: Run multi-environment plans in parallel
- Conditional execution: Skip unnecessary steps with
ifconditions - Artifact reuse: Share plan artifacts between jobs instead of regenerating
Maintenance strategies
- Pin action versions: Use specific versions (
@v1) instead of@main. Check the GitHub Marketplace for the latest stable release. - Monitor workflow runs: Set up alerts for failed workflows
- Regular updates: Keep the action and dependencies updated
- Document customizations: Add comments explaining custom configurations
Troubleshooting
Comment not posting
Symptoms: Workflow succeeds but no comment appears on the PR.
Solutions:
- Verify
pull-requests: writepermission is set - Check that the workflow is triggered on
pull_requestevents - Ensure the
planfilepath is correct - Verify the GitHub token has appropriate permissions
Plan output looks malformed
Symptoms: Comment shows raw text instead of formatted sections.
Solutions:
- Ensure you're using a binary plan file generated with
-out=tfplan.binary - Check that the plan file actually contains Terraform plan output
- Verify the file isn't empty
- Make sure
terraform initran successfully before the plan
Multiple comments on the same PR
Symptoms: Multiple comments appear instead of updating the existing one.
Solutions:
- Use consistent
headervalues across workflow runs - Don't change the header between commits on the same PR
- Check that the action version is consistent
AWS authentication failures
Symptoms: Cannot assume IAM role or access AWS resources.
Solutions:
- Verify the IAM role ARN is correct
- Check the trust relationship allows your GitHub repository
- Ensure
id-token: writepermission is set - Validate the OIDC identity provider configuration
Terraform init failures
Symptoms: Terraform init fails before generating the plan.
Solutions:
- Verify backend configuration is correct
- Check AWS credentials have permissions to access state bucket
- Ensure all required Terraform variables are set
- Validate provider configurations
Performance issues
Symptoms: Workflow takes too long to complete.
Solutions:
- Enable Terraform provider caching
- Use job matrices for parallel execution
- Cache Terraform plugins and modules
- Consider reducing the number of resources or splitting into smaller stacks
Need more help? Visit the GitHub Marketplace page or check the source repository for issues and discussions.