i'm trying yo create multiple stages in serverless with no success.
Here is my serverless.yml
:
service: some-cache-updater
provider:
name: aws
runtime: nodejs8.10
stage: dev
functions:
scheduledUpdater:
handler: handler.scheduledUpdater
timeout: 120
What i wish to add is a prod stage with a different timeout.
Can i do it in the same yml?
Any way an example or a reference will be helpful... thanks.
I often find myself creating four separate stages for each Serverless Framework project I work on: dev , staging , prod , and local . Obviously the first three are meant to be deployed to the cloud, but the last one, local , is meant to run and test interactions with local resources.
Serverless deployment is one of the latest trends in cloud technologies. It is a development model built for creating and running applications without the need for server management. They are provisioned, maintained, and scaled by a third-party cloud provider, while the developers just write and deploy the code.
Use Serverless' $self
reference interpolation which can include further interpolation.
Define a custom variable where necessary. You can also use a default value if the variable doesn't exist.
Example:
service: some-cache-updater
custom:
functimeout:
prod: 120
uat: 60
provider:
name: aws
runtime: nodejs8.10
stage: ${opt:stage, 'dev'}
functions:
scheduledUpdater:
handler: handler.scheduledUpdater
# Lookup stage key from custom.functimeout. If it doesn't exist
# default to 10
timeout: ${self:custom.functimeout.${self:provider.stage}, '10'}
Then, when you deploy you can pass the --stage prod
or --stage uat
argument. In this example, no setting the stage will default to dev
serverless.yml:
...
provider:
stage: ${opt:stage, 'dev'}
...
Command line:
sls deploy --stage prod
${opt:stage, 'dev'} takes the value passed from command line --stage option. In this case prod. If no option is passed dev is taken as default.
More info here: https://serverless.com/framework/docs/providers/aws/guide/variables/#recursively-reference-properties
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With