docs/scf/examples/express.md
This example demonstrates how to build and deploy a simple Express-based web application using the Serverless Container Framework (SCF). The application sets up basic routes—including a health check, static file delivery, and a fallback 404 page—with minimal configuration. The Serverless Container Framework enables this application to be deployed to either AWS Lambda or AWS ECS Fargate without rearchitecting.
/health route for monitoring application health.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.
At the project root, the serverless.containers.yml file defines the SCF configuration:
name: express
deployment:
type: [email protected]
containers:
service:
src: ./service
routing:
pathPattern: /*
pathHealthCheck: /health
environment:
HELLO: world
compute:
type: awsLambda # or awsFargateEcs
This configuration sets:
./service directory./*) is used with a dedicated health check endpoint (/health).HELLO) is provided.awsLambda by default (switchable to awsFargateEcs as needed).For more details on SCF configuration options, see the SCF Configuration documentation.
A typical project structure for this Express example:
example-express/
├── serverless.containers.yml # SCF configuration file
└── service/
├── package.json # Node.js project configuration and dependencies
└── src/
├── index.js # Main Express application entrypoint
└── public/ # Static assets (HTML, CSS, images, etc.)
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 everything and set up hot reloading.
For more information on local development with SCF, see the SCF Development documentation.
Deploy your Express application to AWS by running:
serverless deploy
During deployment, SCF builds the container image (using the provided multi-stage Dockerfile) and provisions the necessary AWS resources (ALB, VPC, Lambda function or ECS Fargate service).
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: express-service
# Environment variable reference
NODE_ENV: ${env:NODE_ENV}
# AWS Systems Manager Parameter Store reference
DATABASE_URL: ${aws:ssm:/path/to/database/url}
# AWS Secrets Manager reference
DATABASE_PASSWORD: ${aws:secretsmanager:ExpressDbSecret.password}
# HashiCorp Vault reference
API_SECRET: ${vault:secret/data/api/credentials.secret}
# HashiCorp Terraform state reference
REDIS_ENDPOINT: ${terraform:outputs:redis_endpoint}
# S3 bucket value reference
CONFIG_JSON: ${aws:s3:config-bucket/express-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 deployed AWS resources when they are no longer needed:
serverless remove --force --all