Deploying to AWS
The Serverless Framework deploys a service to AWS in three ways:
serverless deploydeploys the whole service (functions, events and resources) as one AWS CloudFormation stack. Use it for any change toserverless.yml, and in CI/CD.serverless deploy functionupdates one function's code and configuration directly, without CloudFormation. Use it for fast iterations during development.serverless deploy --packagedeploys artifacts built earlier withserverless package.
Run serverless deploy in the directory that holds serverless.yml:
serverless deploy
The deploy goes to the dev stage in us-east-1 unless provider.stage and provider.region in serverless.yml, or --stage and --region, say otherwise:
serverless deploy --stage prod --region eu-central-1
It uses the AWS credentials described in which credentials a deploy uses. Add --verbose to print the CloudFormation progress, and --config (or -c) to use a configuration file other than serverless.yml.
Deploy the service
serverless deploy turns serverless.yml into a CloudFormation template and creates or updates the service's stack, named <service>-<stage> unless provider.stackName is set. When an update fails, CloudFormation rolls the stack back to its previous state, unless provider.disableRollback is set.
How a deploy works
- The Framework resolves the deployment bucket, which stores the zip files and the compiled template. By default it is a shared deployment bucket per AWS Region, created on the first deploy to that Region. A service whose stack already contains a stack-managed deployment bucket (for example, one first deployed with an earlier major version) keeps using it, except inside Serverless Compose, where services always use the shared bucket.
provider.deploymentBucketsets a bucket of your own. - With the legacy stack-managed bucket (
provider.enableLegacyDeploymentBucket: true) and no stack yet, the Framework creates a stack that holds only the bucket. Otherwise, no such initial stack is created. - The Framework packages the functions into zip files (see Packaging), unless
--packageorpackage.pathnames a directory of artifacts thatserverless packagebuilt earlier (see Deploying a package). - For container images built from a local
path, the Framework creates the service's ECR repository if needed, runsdocker loginagainst it, and builds and pushes the images. - Unless
--forceis passed, the Framework compares the hashes of the compiled CloudFormation template, the service state file and the zip files with those of the newest deployment in the bucket. When all of them match, and no object of that deployment was uploaded after the functions were last updated, the deploy stops here without changing anything. - The Framework uploads the zip files and the compiled template to the deployment bucket, under
<deploymentPrefix>/<service>/<stage>/. - CloudFormation creates the stack (first deploy) or updates it with the new template.
By default, each deploy that changes a function's code or configuration publishes a new version of it. provider.versionFunctions: false turns that off, and a function's own versionFunction overrides the provider setting. Functions with durableConfig, provisionedConcurrency or snapStart are always versioned, and versionFunction: false on a function with durableConfig stops the deploy with an error. See Versioning deployed functions.
A deploy needs AWS permissions for CloudFormation, for the resources in the stack, and for SSM and S3 to resolve the deployment bucket and upload the artifacts; inside Serverless Compose, also for the state bucket. See AWS credentials requirement for packaging.
Deployment method
By default, the Framework updates the stack with direct updates. To deploy through CloudFormation change sets instead, set provider.deploymentMethod to changesets (allowed values: direct, changesets; default direct):
provider:
name: aws
deploymentMethod: changesets
Deployment bucket settings
These provider settings change where and how the artifacts are stored:
deploymentBucket: a bucket of your own, byname, with settings such asserverSideEncryption. See the Deployment Bucket guide for every key.deploymentPrefix: the S3 key prefix of the artifacts. Defaultserverless.deploymentBucket.skipPolicySetup: don't create the default bucket policy. Applies only to the stack-managed deployment bucket.deploymentBucket.versioning: true: turn on versioning for the stack-managed deployment bucket. The shared deployment bucket is always versioned.enableLegacyDeploymentBucket: true: use a deployment bucket created and managed by the service's own stack. The--enable-legacy-deployment-bucketflag and theENABLE_LEGACY_DEPLOYMENT_BUCKETenvironment variable do the same.
--aws-s3-accelerate uploads through S3 Transfer Acceleration, which the Framework turns on only for the stack-managed deployment bucket. With provider.deploymentBucket, the deploy stops with S3_TRANSFER_ACCELERATION_ON_EXISTING_BUCKET; with the shared deployment bucket, the bucket's acceleration setting is not changed.
AWS SDK for JavaScript v3 for Framework calls
The Framework's own calls to AWS during packaging and deployment (S3 uploads, CloudFormation updates) use the AWS SDK for JavaScript v2 code path by default. To use the v3 code path, set SLS_AWS_SDK=3 in the environment, on your machine or in the CI pipeline:
SLS_AWS_SDK=3 serverless deploy
Both paths read credentials through the standard AWS credential provider chain. The setting doesn't affect your function code, which uses whatever SDK you bundle or the runtime provides; AWS has ended support for the v2 SDK, so use @aws-sdk/* (v3) clients in handlers.
Deploy a single function
serverless deploy function updates one function without touching the CloudFormation stack, which makes it much faster than a full deploy:
serverless deploy function --function myFunction
- The Framework packages the function into a zip file.
- It compares the hash of the zip (for an image function, the image digest) with the code hash of the deployed function, and skips the code update when they match, unless
--forceis passed. - Otherwise, it uploads the zip directly to the function with the Lambda
UpdateFunctionCodeAPI, bypassing the deployment bucket and CloudFormation. - If the function's configuration (such as memory size, timeout or environment variables) differs from
serverless.yml, it updates that too.--update-configupdates only the configuration, without deploying code.
The function must already exist: otherwise the command stops with FUNCTION_NOT_YET_DEPLOYED, so deploy the service once with serverless deploy first. Changes made this way are not in the stack's template, so use the command during development and keep serverless deploy for CI/CD and production. See the deploy function command for its options.
Deploying a package
serverless deploy --package deploys a directory created earlier with serverless package, without packaging again. Build the artifacts once (for example in a CI build step) and deploy them in a later step:
serverless package --package path-to-package
serverless deploy --package path-to-package
The deploy reads the zip files and templates from that directory and uses them to create or update the stack. package.path in serverless.yml does the same as --package. See the deploy command for every option.
Recover a stack in UPDATE_ROLLBACK_FAILED
When a deploy fails and CloudFormation then can't roll back one of the stack's resources, for example a resource changed or deleted outside the stack, the stack stays in UPDATE_ROLLBACK_FAILED. Every later serverless deploy stops with:
Stack:arn:aws:cloudformation:us-east-1:123456789012:stack/my-service-dev/... is in UPDATE_ROLLBACK_FAILED state and can not be updated.
The stack must return to UPDATE_ROLLBACK_COMPLETE before it accepts another deploy. Run the commands below with the credentials and Region of the deploy, adding --region (and --profile) when your AWS CLI defaults differ. Fix what blocks the rollback if you can, then continue the rollback:
aws cloudformation continue-update-rollback --stack-name my-service-dev
If you can't fix it, force the rollback to complete. CloudFormation skips every resource whose rollback failed, including resources in nested stacks, and marks it Resource skipped during UpdateRollback:
aws cloudformation continue-update-rollback --stack-name my-service-dev --force-rollback
Warning: A skipped resource no longer matches the stack's template. Before the next deploy, check each skipped resource and bring it in line with the stack's current template, the one the rollback restored (aws cloudformation get-template --stack-name my-service-dev, or the nested stack's template for a resource in a nested stack), not with the changed serverless.yml; run drift detection on it to see the differences. Drift detection covers only resource types that support it and only the properties the template sets, so compare other resources with the template yourself. Then run serverless deploy. See Continue rolling back an update.
Related pages
- Packaging: what goes into the artifacts.
- Deployment Bucket: the shared bucket and your own bucket.
- AWS credentials: where a deploy's credentials come from.
serverless deploy: command options.