docs/scf/examples/react.md
This example demonstrates how to build and deploy a React application using the Serverless Container Framework (SCF). The application is bundled using esbuild and optimized for production deployments. The Serverless Container Framework enables this application to be deployed to either AWS Lambda or AWS ECS Fargate without rearchitecting, though it is configured for AWS Fargate ECS to accommodate larger bundle sizes.
Before getting started, make sure you have:
npm i -g serverless
For more information on setting up AWS credentials, see the SCF Getting Started guide.
The SCF configuration is defined in the serverless.containers.yml file at the project root:
name: react
deployment:
type: [email protected]
containers:
service:
src: ./service
routing:
pathPattern: /*
pathHealthCheck: /health
environment:
HELLO: world
compute:
# awsLambda is not recommended for this React app.
# The bundled app may exceed AWS Lambda's request/response size limits.
type: awsFargateEcs
For more details on SCF configuration options, see the SCF Configuration documentation.
A typical project structure for this React example:
example-react/
├── serverless.containers.yml # SCF configuration file
└── service/
├── package.json # Project configuration and dependencies
├── public/ # Static assets (HTML, CSS, images, etc.)
├── server.js # Server entrypoint for serving the React app
└── src/
├── index.jsx # React application entrypoint
└── (other components) # React components and logic
Serverless Container Framework provides a local development mode that emulates AWS routing and compute environments, including AWS Application Load Balancer emulation:
serverless dev
This will automatically start the development environment with hot reloading and AWS-like routing.
For more information on local development with SCF, see the SCF Development documentation.
Deploy your React application to AWS by running:
serverless deploy
SCF takes care of building the container image (using the provided Dockerfile) and provisioning the necessary resources.
For more details on deployment options and processes, see the SCF Deployment documentation.
Serverless Container Framework supports the Serverless Framework Variables system to reference infrastructure details, secrets, and more from various sources:
containers:
service:
environment:
# Simple static value
SERVICE_NAME: react-application
# Environment variable reference
NODE_ENV: ${env:NODE_ENV}
# AWS Systems Manager Parameter Store reference
API_ENDPOINT: ${aws:ssm:/path/to/api/endpoint}
# AWS Secrets Manager reference
API_KEY: ${aws:secretsmanager:ReactApiSecret.key}
# HashiCorp Vault reference
AUTH_SECRET: ${vault:secret/data/auth/credentials.secret}
# HashiCorp Terraform state reference
REDIS_ENDPOINT: ${terraform:outputs:redis_endpoint}
# S3 bucket value reference
CONFIG_JSON: ${aws:s3:config-bucket/react-config.json}
# CloudFormation stack output reference
VPC_ID: ${aws:cf:networking-stack.VpcIdOutput}
For more details on using variables, see the Serverless Framework Variables documentation.
To remove the deployed AWS resources, run:
serverless remove --force --all