Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Deploy to Development
on:
push:
branches:
- main
- dev

permissions:
id-token: write
Expand Down
27 changes: 18 additions & 9 deletions .github/workflows/deploy-staging.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Deploy Dev
name: Deploy to Staging

on:
push:
Expand All @@ -9,6 +9,10 @@ permissions:
id-token: write
contents: read

concurrency:
group: staging-deployment
cancel-in-progress: true

jobs:
deploy:
runs-on: ubuntu-latest
Expand All @@ -17,30 +21,35 @@ jobs:

defaults:
run:
working-directory: vite-app
working-directory: .

steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
cache-dependency-path: package-lock.json

- run: npm install

- run: npm run test -- --run
- name: Install dependencies and build
run: npm ci

- run: npm run build
- name: Build application
run: npm run build

- uses: aws-actions/configure-aws-credentials@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: us-east-1

- run: |
- name: Sync files to S3
run: |
aws s3 sync dist/ s3://${{ secrets.S3_BUCKET }} --delete

- run: |
- name: Invalidate CloudFront distribution
run: |
aws cloudfront create-invalidation \
--distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} \
--paths "/*"
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ This system provisions and connects:

## ⚡ Features

This project was built with:

- Fully automated CI/CD pipeline (GitHub Actions)
- Secure AWS authentication using OIDC (no long-lived AWS keys)
- Environment-based deployments (dev / staging / prod)
Expand All @@ -94,7 +96,7 @@ This system provisions and connects:
Before using this project, ensure you have:

- AWS Account
- Terraform ≥ 1.10+
- Terraform ≥ 1.10+ (Required for S3 file lock feature introduced in v.1.10. enabling file lock in S3 allows us to lock our state file without the need for DynamoDB + S3 lock feature which is being deprecated by AWS)
- Node.js ≥ 20+
- GitHub repository
- AWS CLI configured (for local testing)
Expand All @@ -111,20 +113,16 @@ git clone https://github.com/ecoderP/s3-static-app-terraform.git

### Before you continue, Please note:

- I have preset customisable terraform variables in .tfvarsexample.
- There are preset customisable terraform variables in .tfvarsexample.
- Terraform state backend configurations are in .tfbackendexample files.

I named these this way to bypass .gitignore, because git will ignore all .tfvars and .tfbackend files. You will need to rename .tfvarsexample and .tfbackendexample to .tfvars and .tfbackend extensions respectively.
These are so named to bypass .gitignore. Gitgnore will ignore all .tfvars and .tfbackend files for security. You will need to rename .tfvarsexample and .tfbackendexample to .tfvars and .tfbackend extensions respectively.

For example, for bootstrap/ directory:
For example, for terraform/bootstrap/ directory, update configuration settings, then:

```
cd terraform/bootstrap
```

After updating your AWS region, project name and your unique bucket name:

```
cp terraform.tfvarsbackendexample terraform.tfvars
```

Expand All @@ -140,7 +138,8 @@ terraform init
**_Important:_** Copy the bucket name from terminal output. This is the shared backend state bucket name for all environments. Use this output as bucket name in .tfbackend for all environments.

4. Configure environment
Each environment (dev/staging/prod) has its own configuration. Locate .tfbackend and .tfvars configuration files, personalise and rename for each environment.

Each environment (dev/staging/prod) has its own configuration. Locate .tfbackend and .tfvars configuration files, personalise and rename for each environment.

```
cd terraform/environments/dev
Expand All @@ -165,7 +164,9 @@ terraform apply -auto-approve
This project uses GitHub Actions → AWS OIDC federation, meaning:

✔ No AWS access keys stored in GitHub

✔ Temporary credentials issued per workflow run

✔ Least-privilege IAM roles scoped per environment

### IAM Role Trust Relationship
Expand All @@ -186,18 +187,19 @@ This project includes GitHub Actions workflows for:

### 🧪 Dev Deployment

- Trigger: push to develop
- Trigger: push to dev
- Deploys to dev S3 bucket + CloudFront

### 🧱 Staging Deployment

- Trigger: push to staging
- Deploys to staging S3 bucket + CloudFront
- Used for pre-production validation

### 🚀 Production Deployment

- Trigger: push to main
- Deploys stable build to production environment
- Deploys stable build to production environment (S3 + CloudFront)

### CI/CD Flow

Expand All @@ -210,21 +212,21 @@ This project includes GitHub Actions workflows for:

### Important GitHub Actions secrets

To get your ci/cd pipeline working, add the following environment secrets to GitHub Actions:
To get your CI/CD pipeline working, add the following environment secrets to GitHub Actions:

- S3_BUCKET
- CLOUDFRONT_DISTRIBUTION_ID
- AWS_ROLE_ARN

To get the values for your project, from each environment directory (dev, staging, prod), run:
To get the values for your secrets, from each environment directory (dev, staging, prod), run:

```
terraform output
```

---

## ♻️ How to Re-use This Project (Important)
## ♻️ Re-using This Project (Some Viable Options)

This repo is designed as a starter backend infrastructure for any React + Vite frontend project.

Expand Down
25 changes: 25 additions & 0 deletions terraform/environments/staging/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions terraform/environments/staging/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ output "distribution_domain_name" {

output "bucket_name" {
value = module.s3_static_site.bucket_name
}

output "github_actions_role_arn" {
value = module.github_oidc.github_actions_role_arn
}

output "cloudfront_distribution_id" {
value = module.cloudfront.distribution_id
}
Loading