Serverless framework 'dev' and 'prod' seperation

Viewed 3167

I am writing a backend service using the Serverless framework.

What is the best way for separating "Dev" and "Prod" environments?

Let's say I want my dev profile to deploy in a certain region and my prod profile to deploy in another? Is there a way to achieve this in the Serverless framework?

I would like to do something like:

serverless deploy --profile dev (--> use serverless-dev.yml)
serverless deploy --profile prod (--> use serverless-prod.yml)
2 Answers

This question was asked 5 years ago and I assume back to the time there was not much support for multi-environment deployment using serverless framework.


Nowadays, there are few ways to achieve your goals:

  1. Use of separate serverless.yml
    Assume you have two serverless.yml: serverless.dev.yml and serverless.prod.yml.
    You can run serverless deploy --config=serverless.dev.yml and serverless deploy --config=serverless.prod.yml to use different yaml files. For more, read this.

  2. Use of stage in serverless.yml
    You can provide stage variable by using -s or --stage in your serverless command, like serverless deploy -s dev. You can then reference to this value in serverless.yml using ${opt:stage}, in this example, it becomes dev. You can use this inline with the other string, like ${opt:stage}-User-Table becomes dev-UserTable.

  3. Use of param in serverless.yml
    If using stage does not satisfy you, you can use --param to pass a key-value pair to the serverless.yml. For example, you have ${param: TABLE_NAME} in yaml, you can then set the value by, for example, serverless deploy --param="TABLE_NAME=dev-UserTable". Like Method 2, the value is replaced by what you give in the command.


I think method 2 and 3 are the most suitable ways to do staging because you only maintain one serverless.yml, you only need method 1 in case you need multiple API Gateways in your service. You could imagine if you use serverless.prod.yml as stable version and serverless.dev.yml as development, when there is a release, you have to replace everything from serverless.dev.yml to serverless.prod.yml, which is not a good practice in terms of DevOps, use your VCS's branching function instead!

Hope this help!

Related