Using the serverless framework it is possible to specify a CloudFormation stack using the stackName variable (as described here).
When working with different stages using the CLI parameters and serverless.yml code similar to this answer all other stages and corresponding resources are deleted from the specified stack when deploying e.g. they are overwritten with the new stages lambda/polcies/etc.
I want both serverless deploy
and serverless deploy --stage prod
be deployed to the same stack and create different versions of the resources.
Is there an easy way to have multiple stages under a single stack? Or is this maybe a bad idea from the get-go?
My serverless.yml looks similar to this atm:
service: serviceName
frameworkVersion: "3"
provider:
name: aws
runtime: nodejs18.x
stage: ${opt:stage, 'dev'}
stackName: stackName
region: eu-central-1
deploymentBucket:
name: sl-deployment-bucket
httpApi:
shouldStartNameWithService: true
[...]
functions:
lambdaName:
name: lambdaName-${sls:stage}
handler: handler.handle
events:
- httpApi:
method: get
path: /get-stuff
[...]
2
Answers
You have have to include the environment name in the
stackName
property in theprovider
block.I would recommend against trying to do that as Serverless Framework internals are operating under the assumption that each stage is deployed as a separate CloudFormation stack. Each package/deployment will generate a stage-specific CloudFormation template that will then be deployed, which means that each deploy will remove everything else (unless defined under
resources
explicitly), which you already experienced when trying to manually set thestackName
.Is there any particular reason why you’d like to keep all stages in a single CloudFormation stack?