Skip to main content

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.

✓ Complete serverless.yml reference ✓ Plugins & custom variables ✓ SLS vs SAM vs CDK comparison ✓ Multi-stage deployments

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 formatYAML / JSYAML / JSONTypeScript / Python / Java
Multi-cloudYes (AWS/GCP/Azure)AWS onlyAWS only
Local testingserverless-offlinesam local (built-in)Via SAM CLI integration
CostFree (v3) / Paid (v4 prod)FreeFree
Learning curveLowLow-MediumMedium-High
Complex infraLimitedGoodExcellent
Best forQuick deploys, multi-cloudAWS Lambda + API GWComplex AWS architecture
Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$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.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

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.