Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple stages in serverless framwork

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.

like image 997
MTZ4 Avatar asked Jun 17 '18 09:06

MTZ4


People also ask

What are stages in serverless?

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.

What are serverless deployments?

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.


2 Answers

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

like image 182
Alastair McCormack Avatar answered Jan 24 '23 23:01

Alastair McCormack


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

like image 29
oz19 Avatar answered Jan 24 '23 22:01

oz19