Development Workflow
This page describes a workflow that keeps the feedback loop short while you build a Serverless Framework service on AWS Lambda: each developer works in a personal stage, edits code against real AWS events with Dev Mode, and lets CI/CD deploy the shared stages. It ends with a cheat sheet of the commands each step uses.
The loop on a personal stage named after you looks like this:
serverless deploy --stage alice
serverless dev --stage alice
serverless deploy --stage alice
serverless remove --stage alice
Deploy the stage once, run Dev Mode while you change handler code, deploy again after you change serverless.yml, and remove the stage when you no longer need it.
Give each developer a stage
A stage is a separate deployment of the same service: its own CloudFormation stack, functions and resources, named after the stage (my-service-alice). The stage comes from --stage, else provider.stage, else dev.
- Use a personal stage per developer (
--stage alice), so one person's deploys and Dev Mode sessions never change another's functions. - Keep shared stages such as
stagingandprodfor CI/CD. - Put stages that hold production data in a separate AWS account from development stages (AWS Credentials).
- Set stage-specific values under
stages.<stage>.paramsand read them with${param:…}(Parameters), instead of branching on the stage name in code.
Develop against real events with Dev Mode
Dev Mode (serverless dev) runs the Node.js (including TypeScript) and Python functions of a deployed stage on your machine. Real invocations of the stage, such as API requests, queue messages and direct invokes, run the code in your working copy, with the deployed function's environment variables and IAM role. Save a file and the next invocation runs it, with no redeploy. When you stop the session with Ctrl+C, q or serverless dev stop, the functions return to their deployed configuration. A session that crashed, was killed or was stopped with a second Ctrl+C leaves functions attached, running their deployed code, until serverless dev restore, the next serverless dev or a deploy restores them (Stop a session and recover functions).
serverless dev --stage alice
- Dev Mode refuses the
prodandproductionstages; use it on a personal or development stage. - A stage that was never deployed is deployed first.
- Functions in other runtimes, container image functions and Lambda@Edge functions keep running their deployed code.
- Scripts and AI coding agents run a session in the background with
serverless dev --detachand stop it withserverless dev stop(serverless dev).
Deploy changes
Choose the deploy command by what changed:
- After a change to
serverless.yml(functions, events, resources, IAM, settings), runserverless deploy --stage alice. It deploys the whole service through CloudFormation, and skips the deployment when nothing changed. - To push one function's code and configuration without CloudFormation, run
serverless deploy function -f hello --stage alice. It doesn't deploy events or resources, leaves the function out of sync with the stack, and a laterserverless deploydoesn't undo a configuration change it made. Use it on personal stages only (serverless deploy function).
To see what a deploy would change first, run serverless diff --stage alice (serverless diff).
Test and debug a deployed function
Invoke a function in AWS and print its log output:
serverless invoke -f hello --stage alice --log
Follow a function's logs in a separate terminal while you send requests:
serverless logs -f hello --stage alice --tail
Write unit tests for handler logic that run locally without AWS, and keep tests that need AWS resources for a deployed stage (Testing).
Deploy shared stages from CI/CD
Let a pipeline deploy staging and prod, so every change to those stages comes from version control:
serverless deploy --stage prod --region us-east-1
In the pipeline:
- Authenticate the CLI with a secret in
SERVERLESS_ACCESS_KEY(an Access Key) orSERVERLESS_LICENSE_KEY(License Keys). Without a terminal, the CLI doesn't prompt you to sign in: when the CLI finds no credential (no key variable, noserverless loginsession, nolicenseKey, no saved key and no License Key in the/serverless-framework/license-keySSM parameter), the command stops with an error that names the keys to set. - Give the pipeline AWS credentials for the stage's account, for example through your CI provider's OpenID Connect integration or environment variables (AWS Credentials).
- Pin the Framework version with
frameworkVersioninserverless.yml, so the pipeline and developers deploy with the same CLI.
Split larger projects into services
Keep each service small enough to deploy quickly and to reason about:
- Split an application into several services, each with its own
serverless.yml, around a data model or a workflow. - Keep long-lived resources such as databases and queues in their own services, deployed less often than the code that uses them.
- Deploy related services together, and pass outputs between them, with Serverless Compose.
Clean up personal stages
Remove a stage you no longer use, so it stops running and costing money:
serverless remove --stage alice
Warning: serverless remove deletes the stage's CloudFormation stack, including its functions and every resource defined in the service, such as tables and buckets. Check the stage name before you run it.
CloudFormation keeps a resource with a DeletionPolicy of Retain, and by default takes a final snapshot of an AWS::RDS::DBCluster and of an AWS::RDS::DBInstance without DBClusterIdentifier before deleting it (DeletionPolicy attribute). Retained resources and snapshots stay in the account and keep incurring charges until you delete them, so check for them after the stack is removed.
CloudFormation deletes an S3 bucket only when it is empty, and serverless remove empties only the service's files in the deployment bucket. When a bucket defined in resources still holds objects, CloudFormation can't delete it, so the stack deletion stops and the stack is left in the DELETE_FAILED state; a bucket with DeletionPolicy: Retain is kept instead and doesn't stop the deletion. Check the stack's events in the CloudFormation console to find the resource that couldn't be deleted. If it's a bucket, empty only that bucket, including every object version and delete marker in a versioned bucket (Emptying a general purpose bucket), then run serverless remove again.
Command cheat sheet
| Task | Command |
|---|---|
| Create a service from a template | serverless |
| Deploy the whole service | serverless deploy --stage STAGE --region REGION |
| Deploy with detailed progress | serverless deploy --stage STAGE --verbose |
| Deploy one function's code and settings | serverless deploy function -f FUNCTION --stage STAGE |
| Run the stage's functions locally | serverless dev --stage STAGE |
| Invoke a deployed function and print logs | serverless invoke -f FUNCTION --stage STAGE --log |
| Follow a function's logs | serverless logs -f FUNCTION --stage STAGE --tail |
| Show the stage's endpoints and outputs | serverless info --stage STAGE |
| Remove the stage | serverless remove --stage STAGE |
Replace STAGE, REGION and FUNCTION with your stage, AWS Region and the function's key under functions in serverless.yml. -s, -r and -f are the short forms of --stage, --region and --function; -v prints the CLI version, not verbose output.
Related pages
- Dev Mode: run a deployed stage's functions on your machine.
- Deploying to AWS: how
serverless deployworks. - Parameters: stage-specific values.
- Serverless Compose: deploy several services together.