How to create multiple stages in serverless framwork

橙三吉。 提交于 2019-12-23 16:43:08

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/50895367/how-to-create-multiple-stages-in-serverless-framwork

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!