Skip to content

bcgov/quickstart-aws-sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Merge PR PR Validate CodeQL Pause AWS Resources Resume AWS Resources

πŸš€ AWS SQL Quickstart

⚑ Aurora Serverless v2 + ECS Fargate + CloudFront

🎯 Ready-to-deploy containerized app stack for AWS! Built by BC Government developers, for developers.

🌟 What's Inside?

This template gives you a complete, production-ready application stack with:

  • πŸ—„οΈ Aurora Serverless v2 PostgreSQL database with PostGIS extension
  • 🐳 ECS Fargate with mixed FARGATE/FARGATE_SPOT capacity providers for cost optimization
  • πŸ”„ Flyway Migrations automated through ECS tasks for database schema management
  • πŸšͺ API Gateway with VPC link integration for secure backend access
  • 🌐 CloudFront for frontend content delivery with WAF protection
  • πŸ—οΈ NestJS TypeScript backend API with Prisma ORM
  • βš›οΈ React with Vite for the frontend application
  • πŸ—οΈ Terragrunt/Terraform for infrastructure-as-code deployment
  • πŸ”„ GitHub Actions for CI/CD pipeline automation
  • πŸ” AWS Secrets Manager integration for secure credential management

πŸ“‹ Prerequisites

Before you start, make sure you have:

  • βœ… BCGOV AWS account with appropriate permissions
  • βœ… AWS CLI installed and configured
  • βœ… Docker/Podman installed (for local development)
  • βœ… Node.js 22+ and npm installed
  • βœ… Terraform CLI and Terragrunt

πŸ“ Project Structure

/quickstart-aws-sql
β”œβ”€β”€ πŸ“„ CODE_OF_CONDUCT.md        # Project code of conduct
β”œβ”€β”€ πŸ“‹ COMPLIANCE.yaml           # Compliance and regulatory information
β”œβ”€β”€ 🀝 CONTRIBUTING.md           # Contribution guidelines
β”œβ”€β”€ 🐳 docker-compose.yml        # Local development environment definition
β”œβ”€β”€ πŸ”§ eslint.config.mjs         # ESLint configuration
β”œβ”€β”€ πŸ“– GHA.md                    # GitHub Actions workflows documentation
β”œβ”€β”€ πŸ“œ LICENSE                   # Project license
β”œβ”€β”€ πŸ“¦ package.json              # Monorepo configuration and dependencies
β”œβ”€β”€ πŸ“– README.md                 # Project documentation
β”œβ”€β”€ πŸ”„ renovate.json             # Renovate bot configuration
β”œβ”€β”€ πŸ”’ SECURITY.md               # Security policy
β”œβ”€β”€ πŸ”§ tsconfig.json             # TypeScript configuration
β”œβ”€β”€ πŸ—οΈ backend/                  # NestJS backend API code
β”‚   β”œβ”€β”€ 🐳 Dockerfile            # Container definition for backend service
β”‚   β”œβ”€β”€ πŸ”§ nest-cli.json         # NestJS CLI configuration
β”‚   β”œβ”€β”€ πŸ“¦ package.json          # Backend dependencies
β”‚   β”œβ”€β”€ πŸ“ prisma/               # Prisma ORM schema and migrations
β”‚   β”‚   └── schema.prisma        # Database schema definition
β”‚   β”œβ”€β”€ πŸ’» src/                  # Source code (controllers, services, modules)
β”‚   └── πŸ§ͺ test/                 # Backend test utilities
β”œβ”€β”€ βš›οΈ frontend/                 # Vite + React SPA
β”‚   β”œβ”€β”€ 🌐 Caddyfile             # Caddy server config for frontend
β”‚   β”œβ”€β”€ 🐳 Dockerfile            # Container definition for frontend service
β”‚   β”œβ”€β”€ πŸ“„ index.html            # Main HTML entry point
β”‚   β”œβ”€β”€ πŸ“¦ package.json          # Frontend dependencies
β”‚   β”œβ”€β”€ 🎭 e2e/                  # End-to-end tests using Playwright
β”‚   β”œβ”€β”€ πŸ“ public/               # Static assets
β”‚   └── πŸ’» src/                  # React source code
β”œβ”€β”€ πŸ—οΈ infra/                    # Terraform code for AWS infrastructure
β”‚   β”œβ”€β”€ πŸ“ main.tf               # Infrastructure root module
β”‚   └── πŸ“ modules/              # Infrastructure modules
β”‚       β”œβ”€β”€ πŸšͺ api/              # API infrastructure (ECS, ALB, etc.)
β”‚       β”œβ”€β”€ πŸ—„οΈ database/         # Database infrastructure (Aurora, etc.)
β”‚       └── 🌐 frontend/         # Frontend infrastructure (CloudFront, etc.)
β”œβ”€β”€ πŸ”„ migrations/               # Flyway migrations for database
β”‚   β”œβ”€β”€ 🐳 Dockerfile            # Container for running migrations
β”‚   └── πŸ“ sql/                  # SQL migration scripts
β”œβ”€β”€ πŸ—οΈ terragrunt/               # Terragrunt configuration for environments
β”‚   β”œβ”€β”€ πŸ”§ terragrunt.hcl        # Root Terragrunt config
β”‚   β”œβ”€β”€ πŸ§ͺ dev/                  # Dev environment config
β”‚   β”œβ”€β”€ πŸš€ prod/                 # Prod environment config
β”‚   └── πŸ”¬ test/                 # Test environment config
└── πŸ§ͺ tests/                    # Test suites beyond component-level
    β”œβ”€β”€ πŸ”— integration/          # Integration tests across services
    └── ⚑ load/                 # Load testing scripts

πŸ—οΈ Key Components Explained

πŸ”§ Infrastructure (terragrunt/ & infra/)

  • πŸ—οΈ Terragrunt: Orchestrates infrastructure deployment across environments
  • πŸ“ Environment folders (dev, test, prod): Environment-specific configurations
  • πŸ›οΈ Terraform modules: Reusable infrastructure components
    • πŸšͺ API: ECS Fargate backend (ALB, API Gateway, autoscaling, IAM, Secrets Manager)
    • 🌐 Frontend: CloudFront distribution and WAF
    • πŸ—„οΈ Database: Aurora Serverless v2 PostgreSQL with networking

πŸ’» Applications

  • πŸ—οΈ Backend (backend/): NestJS TypeScript API with Prisma ORM
  • βš›οΈ Frontend (frontend/): React SPA built with Vite
  • πŸ”„ Migrations (migrations/): Flyway database schema management

πŸ§ͺ Testing & Quality

  • πŸ§ͺ Unit Tests: Built into each application
  • 🎭 E2E Tests: Playwright for UI validation
  • ⚑ Load Tests: Performance testing with k6
  • πŸ”— Integration Tests: Cross-service validation

πŸš€ Quick Start

Option 1: 🐳 Docker Compose (Easiest!)

  1. Clone and navigate to the project:

    git clone <repo-url>
    cd quickstart-aws-sql
  2. Start everything with one command:

    docker-compose up --build
  3. Access your apps:

  4. Stop when done:

    docker-compose down

Option 2: πŸ’» Local Development (Advanced)

Prerequisites:

  • β˜• JDK 17+
  • πŸ“¦ Node.js 22+
  • πŸ—„οΈ PostgreSQL 17.4 with PostGIS
  • πŸ”„ Flyway CLI

Steps:

  1. Start PostgreSQL (as a service)

  2. Run database migrations:

    java -jar flyway.jar \
      -url=jdbc:postgresql://$postgres_host:5432/$postgres_db \
      -user=$POSTGRES_USER \
      -password=$POSTGRES_PASSWORD \
      -baselineOnMigrate=true \
      -schemas=$FLYWAY_DEFAULT_SCHEMA \
      migrate
  3. Start the backend:

    cd backend
    npm run start:dev  # or npm run start:debug
  4. Start the frontend:

    cd frontend
    npm run dev

πŸš€ Deploying to AWS

🎯 Deployment Options

Option 1: πŸ”„ GitHub Actions CI/CD (Recommended!)

The easiest way to deploy! Our pre-configured workflows handle everything:

  • βœ… Building and testing changes on pull requests
  • πŸš€ Auto-deployment to AWS environments on merge
  • πŸ’° Resource management (pause/resume for cost savings)
  • πŸ§ͺ Comprehensive testing (unit, integration, load tests)
  • πŸ”’ Security scanning with Trivy

Quick Setup:

  1. 🍴 Fork or clone this repository
  2. πŸ” Configure GitHub secrets (see below)
  3. πŸ“€ Push changes to trigger workflows

Required GitHub Secrets:

AWS_ROLE_TO_ASSUME     # IAM role ARN with deployment permissions
SONAR_TOKEN_BACKEND    # SonarCloud analysis for backend
SONAR_TOKEN_FRONTEND   # SonarCloud analysis for frontend
AWS_LICENSE_PLATE      # License plate from OCIO (without -dev/-test)

Option 2: πŸ› οΈ Manual Terraform Deployment

For direct control over your infrastructure:

  1. Configure AWS credentials locally
  2. Navigate and deploy:
    cd terraform/api/dev
    terragrunt init
    terragrunt plan
    terragrunt apply

πŸ“– Need help? Check out our detailed AWS deployment guide.

πŸ”„ CI/CD Workflows

Our GitHub Actions provide a complete DevOps pipeline with smart automation!

πŸ“‹ Pull Request Flow

Pull Request Workflow

When you open a PR:

  1. πŸ—οΈ Code builds with concurrency control (no conflicts!)
  2. πŸ“Š Infrastructure planning with Terraform/Terragrunt
  3. πŸ§ͺ Comprehensive testing in isolated environments
  4. πŸ”’ Security scans with Trivy vulnerability detection
  5. πŸ“ˆ Code quality analysis with SonarCloud
  6. 🎭 Optional review environment (manual trigger)

πŸš€ Merge & Deploy Flow

Merge

When code merges to main:

  1. ⚑ Auto-resume AWS resources across environments
  2. πŸš€ Deploy to dev environment
  3. 🏷️ Tag containers with 'dev'
  4. πŸ§ͺ Run E2E tests against dev
  5. βœ… Deploy to test (on success)
  6. 🏷️ Tag containers with 'test'
  7. πŸ’€ Auto-pause resources to save costs

πŸ”§ Workflow Categories

πŸš€ Main Workflows

  • πŸ“‹ PR Workflows: pr-open.yml, pr-validate.yml, pr-close.yml
  • πŸš€ Deployment: merge.yml, release.yml

πŸ”„ Composite Workflows

  • πŸ—οΈ Building: .builds.yml
  • πŸ§ͺ Testing: .tests.yml, .e2e.yml, .load-test.yml
  • πŸš€ Deployment: .deploy_stack.yml, .destroy_stack.yml

πŸ’° Cost Optimization

  • ⏸️ Pause Resources: pause-resources.yml (scheduled/manual/auto)
  • ▢️ Resume Resources: resume-resources.yml (before deployments)
  • 🧹 Cleanup: prune-env.yml

πŸ“– Want more details? Check out our complete GitHub Actions guide!

πŸ›οΈ Architecture Overview

Architecture

πŸ—οΈ Infrastructure Highlights

🐳 ECS Fargate Configuration

  • πŸ’° Cost-Optimized: 20% FARGATE (reliable) + 80% FARGATE_SPOT (cheap!)
  • πŸ“ˆ Smart Auto-Scaling:
    • Scales UP aggressively (+2 instances when busy)
    • Scales DOWN conservatively (-1 instance when idle)
  • πŸ”„ Migration Tasks: Flyway runs before app deployment
  • πŸ” Secure Secrets: Database credentials from AWS Secrets Manager

πŸšͺ API Gateway

  • 🌐 HTTP API Gateway with VPC Link integration
  • πŸ”„ Routes all traffic to internal Application Load Balancer
  • πŸ›‘οΈ Supports ANY method with proxy path integration

πŸ—„οΈ Database Integration

  • πŸ”Œ Auto-connects to Aurora PostgreSQL
  • πŸ” Master credentials from Secrets Manager
  • πŸ”„ Schema migrations via Flyway ECS tasks
  • πŸ“– Read/write splitting with separate endpoints

🎨 Customizing Your Project

Ready to make this template your own? Here's your roadmap! πŸ—ΊοΈ

1. πŸš€ Repository Setup

  • 🍴 Clone this repository
  • πŸ“ Update project names in package.json files
  • πŸ” Set up required GitHub secrets

2. πŸ—οΈ Infrastructure Customization

  • πŸ”§ Terraform/Infrastructure: Modify terraform and infrastructure directories
  • βš™οΈ Environment Variables: Update environment-specific variables
  • πŸ“Š ECS Task Definitions (infrastructure/api/ecs.tf):
    • πŸ’Ύ Customize container resources (CPU/memory)
    • πŸ“ˆ Modify auto-scaling thresholds (infrastructure/api/autoscaling.tf)
    • πŸ’° Update capacity provider strategy (cost vs. reliability)
  • πŸ—„οΈ Database: Configure connection parameters and schema
  • πŸšͺ API Gateway: Customize settings in infrastructure/api/api-gateway.tf

3. πŸ’» Application Customization

  • πŸ—οΈ Backend: Customize NestJS in the backend/ directory
  • βš›οΈ Frontend: Adapt React app in the frontend/ directory
  • πŸ”„ Database: Update schema and migrations in migrations/sql/

4. πŸ”„ CI/CD Pipeline Adjustments

  • πŸ”§ Workflows: Modify GitHub workflows in .github/workflows/
  • πŸš€ Deployment: Update configuration for your AWS account
  • πŸ’° Resource Management: Configure pause/resume schedules
    • ⏰ Adjust cron schedules for your working hours
    • πŸ›‘οΈ Set up environment-specific resource management
    • πŸ”’ Customize protection rules for production

5. πŸ§ͺ Testing Setup

  • πŸ§ͺ Backend Tests: Adapt Vitest tests in backend/src/
  • βš›οΈ Frontend Tests: Update tests in frontend/src/__tests__/
  • 🎭 E2E Tests: Modify Playwright tests in frontend/e2e/
  • ⚑ Load Tests: Customize k6 tests in tests/load/
  • πŸ“ˆ SonarCloud: Update project keys for code quality analysis
  • πŸ”„ GitHub Workflows: Adjust test runners for your environments

🀝 Contributing

We ❀️ contributions! Want to help make this template even better?

πŸ‘‰ Check out our CONTRIBUTING.md for:

  • πŸ“‹ Contribution guidelines
  • πŸ”„ Development workflow
  • πŸ§ͺ Testing requirements
  • πŸ“ Code standards