Serverless Framework: serverless.yml Config & Validation Guide
The Serverless Framework turns AWS Lambda deployment into a single YAML file and one CLI command. This reference covers every section of serverless.yml, common patterns, plugin setup, and how to choose between Serverless Framework, SAM, and CDK.
The serverless.yml Structure
A complete serverless.yml has six top-level sections:
# serverless.yml — complete reference
service: my-api-service
frameworkVersion: '3'
provider:
name: aws
runtime: nodejs20.x
region: us-east-1
stage: ${opt:stage, 'dev'}
memorySize: 512
timeout: 30
environment:
NODE_ENV: ${self:provider.stage}
DB_URL: ${ssm:/myapp/${self:provider.stage}/db-url}
iam:
role:
statements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:PutItem
Resource: !GetAtt UsersTable.Arn
plugins:
- serverless-webpack
- serverless-offline
- serverless-prune-plugin
custom:
webpack:
webpackConfig: webpack.config.js
includeModules: false
prune:
automatic: true
number: 3
functions:
api:
handler: src/api.handler
events:
- httpApi:
path: /users
method: GET
- httpApi:
path: /users/{id}
method: GET
processor:
handler: src/processor.handler
events:
- sqs:
arn: !GetAtt ProcessingQueue.Arn
batchSize: 10
resources:
Resources:
UsersTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: users-${self:provider.stage}
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH Custom Variables and Stage Configuration
Use the custom section to define stage-specific settings that functions and resources reference:
custom:
settings:
dev:
domainName: dev-api.example.com
logLevel: DEBUG
reservedConcurrency: 5
production:
domainName: api.example.com
logLevel: WARN
reservedConcurrency: 100
# Reference: ${self:custom.settings.${self:provider.stage}.logLevel}
functions:
api:
handler: src/handler.ts
reservedConcurrency: ${self:custom.settings.${self:provider.stage}.reservedConcurrency}
environment:
LOG_LEVEL: ${self:custom.settings.${self:provider.stage}.logLevel} Deploying with the Serverless Framework CLI
# Install
npm install -g serverless
# Deploy to dev (default stage)
serverless deploy
# Deploy to production
serverless deploy --stage production --region us-east-1
# Deploy a single function only (faster)
serverless deploy function --function api --stage production
# Remove all resources
serverless remove --stage production
# Invoke a function directly
serverless invoke --function api --stage dev --data '{"httpMethod":"GET"}'
# View logs
serverless logs --function api --stage production --tail Serverless Framework vs SAM vs CDK Comparison
| Feature | Serverless Framework | AWS SAM | AWS CDK |
|---|---|---|---|
| Config format | YAML / JS | YAML / JSON | TypeScript / Python / Java |
| Multi-cloud | Yes (AWS/GCP/Azure) | AWS only | AWS only |
| Local testing | serverless-offline | sam local (built-in) | Via SAM CLI integration |
| Cost | Free (v3) / Paid (v4 prod) | Free | Free |
| Learning curve | Low | Low-Medium | Medium-High |
| Complex infra | Limited | Good | Excellent |
| Best for | Quick deploys, multi-cloud | AWS Lambda + API GW | Complex AWS architecture |
Related Products
on GumroadPremium Workflow Pack
Production-grade workflows, architecture guides, and deployment scripts for teams that ship. 10 advanced CI/CD workflows (blue-green, canary, E2E, security), 5 architecture deep-dives, code review checklist, PR process guide, and 5 battle-tested Bash deployment scripts.
Buy on Gumroad →GitHub Actions CI/CD Templates Pack
12 production-ready GitHub Actions workflow templates covering Node.js, Python, Docker, multi-environment deployments, security scanning, and auto-changelog. Copy, paste, ship.
Buy on Gumroad →Developer Productivity Bundle
Stop rebuilding the same setup every project. 51 VSCode snippets, 40 Git aliases, 5 Docker Compose stacks, 5 GitHub Actions workflows, 5 README templates, and a dotfiles kit. Set up a professional dev environment in under an hour.
Buy on Gumroad →You Might Also Need
AWS CDK Infrastructure as Code Guide
Complete AWS CDK guide for TypeScript/Python IaC. Learn constructs (L1/L2/L3), stacks, CDK Pipelines, diff/deploy workflow, and comparison with Terraform and CloudFormation.
Open free tool →
AWS Lambda Cost Calculator
Estimate monthly AWS Lambda costs. Input invocations, duration, memory, and data transfer to get compute cost, request cost, and total with EC2 comparison.
Open free tool →
AWS IAM Policy Generator
Visual AWS IAM policy JSON builder with action and resource selectors. Generate least-privilege IAM policies for S3, EC2, Lambda, DynamoDB, and more.
Open free tool →
More Free Tools
Related Articles
AWS Lambda Cold Start Optimization: 10 Proven Techniques 2026
Eliminate or minimize AWS Lambda cold starts with 10 proven techniques: provisioned concurrency, SnapStart, package minimization, ARM Graviton2, lazy loading, and more with benchmark data.
Serverless vs Containers 2026: Lambda vs Docker Decision Guide
When should you use AWS Lambda vs Docker containers? This 2026 guide covers cost models, cold starts, use cases, hybrid architectures, and a decision flowchart to help you choose.
Cloud Deployment Tools in 2026: From Containers to Serverless
A practical guide to cloud deployment tools in 2026. Compare deployment strategies, container platforms, serverless frameworks, and GitOps workflows that enterprise teams use to ship software reliably.
Serverless vs Containers 2026: When to Use Each
Decision framework for choosing between serverless functions and containers in 2026. Cost analysis, cold start realities, architecture patterns, and the right tool for each workload type.
Serverless Cold Starts: How to Reduce Lambda Latency by 80%
Learn what causes AWS Lambda cold starts and how to reduce latency by 80%. Covers runtime comparison, Provisioned Concurrency, SnapStart, package optimization, and best practices.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- ⚡ AI Code Review, Doc Generator & SQL Builder
- ⚡ All premium templates & early access
- ⚡ Member discounts on Gumroad products
$9/mo after trial · Cancel anytime
Want the full toolkit?
Get DevPlaybook Pro
Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.
MIT licensed · Instant download · No subscription
See what's included → Browse all deals →Frequently Asked Questions
What is the Serverless Framework?▼
The Serverless Framework is an open-source CLI and deployment tool that simplifies deploying functions-as-a-service across AWS Lambda, Azure Functions, Google Cloud Functions, and other providers. You define your entire serverless application in a serverless.yml file — functions, events, resources, and permissions — and the framework handles packaging, deployment, and IAM role creation.
What is the difference between Serverless Framework v3 and v4?▼
Serverless Framework v4 introduced a paid license for production use (free for dev/test and open-source projects). Architecturally, v4 improves performance monitoring, CI/CD integrations, and dashboard features. v3 remains widely used and fully functional. For new projects in 2026, evaluate whether v4's managed features justify the cost vs using AWS SAM or CDK directly.
How do I use environment variables and secrets in serverless.yml?▼
Use the ${env:VAR_NAME} syntax for environment variables and ${ssm:/path/to/param} for AWS SSM Parameter Store values. For secrets, prefer SSM SecureString or AWS Secrets Manager references over hardcoded values. The framework resolves these at deploy time, and you can use ${param:VAR} for Serverless Dashboard parameters.
What is the difference between stages in the Serverless Framework?▼
Stages are deployment environments (dev, staging, production). You specify the stage with --stage production during deploy. Use stage-specific configuration with provider.stage: ${opt:stage, 'dev'} and condition resources or settings per stage using the ${self:custom.settings.${self:provider.stage}} pattern.
How do Serverless Framework plugins work?▼
Plugins extend the framework's functionality. Install them via npm and list them in your serverless.yml plugins section. Popular plugins include serverless-webpack (bundling), serverless-offline (local emulation), serverless-prune-plugin (cleanup old versions), and serverless-dotenv-plugin (load .env files). Plugins can add new commands and hook into existing lifecycle events.
Should I use Serverless Framework, AWS SAM, or AWS CDK?▼
Serverless Framework excels for multi-cloud deployments and teams familiar with its workflow. AWS SAM is ideal for AWS-only projects using CloudFormation — it is free, AWS-native, and integrates with SAM CLI for local testing. AWS CDK is best for complex infrastructure with full programming language support (TypeScript, Python). For Lambda-only projects on AWS, SAM or CDK are generally preferred in 2026.