Skip to content

feat: AWS infrastructure for static site hosting #4

feat: AWS infrastructure for static site hosting

feat: AWS infrastructure for static site hosting #4

Workflow file for this run

name: Terraform
on:
push:
branches:
- master
paths:
- 'infra/**'
pull_request:
paths:
- 'infra/**'
permissions:
id-token: write
contents: read
pull-requests: write
concurrency:
group: terraform-${{ github.ref }}
cancel-in-progress: false
env:
TF_VAR_aws_profile: ""
jobs:
plan:
name: Terraform Plan
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@ececac1a45f3b08a01d2dd070d28d111c5fe6722 # v4.1.0
with:
role-to-assume: ${{ secrets.AWS_TERRAFORM_ROLE_ARN }}
aws-region: us-east-1
- name: Setup Terraform
uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2
with:
terraform_wrapper: false
- name: Terraform Init
working-directory: infra
run: terraform init
- name: Terraform Validate
working-directory: infra
run: terraform validate
- name: Terraform Plan
id: plan
working-directory: infra
run: |
set -o pipefail
terraform plan -no-color -out=tfplan 2>&1 | tee plan_output.txt
echo "plan_exitcode=$?" >> "$GITHUB_OUTPUT"
- name: Post Plan to PR
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const fs = require('fs');
const plan = fs.readFileSync('infra/plan_output.txt', 'utf8');
const truncated = plan.length > 60000
? plan.substring(0, 60000) + '\n\n... (truncated)'
: plan;
const body = [
'### Terraform Plan',
'```',
truncated,
'```',
`*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`
].join('\n');
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});
- name: Setup Infracost
uses: infracost/actions/setup@e9d6e6cd65e168e76b0de50ff9957d2fe8bb1832 # v3.0.1
with:
api-key: ${{ secrets.INFRACOST_API_KEY }}
- name: Run Infracost Breakdown
working-directory: infra
run: infracost breakdown --path=. --format=json --out-file=/tmp/infracost.json
- name: Post Infracost Comment
run: |
infracost comment github \
--path=/tmp/infracost.json \
--repo=${{ github.repository }} \
--pull-request=${{ github.event.pull_request.number }} \
--github-token=${{ github.token }} \
--behavior=update
apply:
name: Terraform Apply
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master' && github.event_name == 'push'
environment: production-infra
steps:
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@ececac1a45f3b08a01d2dd070d28d111c5fe6722 # v4.1.0
with:
role-to-assume: ${{ secrets.AWS_TERRAFORM_ROLE_ARN }}
aws-region: us-east-1
- name: Setup Terraform
uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2
with:
terraform_wrapper: false
- name: Terraform Init
working-directory: infra
run: terraform init
- name: Terraform Apply
working-directory: infra
run: terraform apply -auto-approve