Serverless framework ‘dev’ and ‘prod’ seperation

Question:

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:

Answer:

You can deploy to different environments using stages with the serverless framework. The deploy command has the stage option which you can specify using --stage or -s. The option for region is --region or -r Here’s an example:

This option can also be used to deploy an individual lambda function to a specific environment.

You might also want to use serverless variables to make your environment configuration dynamic. You can access environment variables using the syntax ${env:SOME_VAR}.

There’s also a way to make variables stage/region specific using nested variables.

From the docs:

Making your variables stage/region specific:

serverless.env.yml allowed you to have different values for the same
variable based on
the stage/region you’re deploying to. You can achieve the same result
by using the nesting functionality of the new variable system. For
example, if you have two different ARNs, one for dev stage and the
other for prod stage, you can do the following:
${env:${opt:stage}_arn}. This will make sure the correct env var is
referenced based on the stage provided as an option. Of course you’ll
need to export both dev_arn and prod_arn env vars on your local
system.

Links to serverless documentation:

Deploy

https://serverless.com/framework/docs/providers/aws/cli-reference/deploy-function/

Workflow recommendations

https://serverless.com/framework/docs/providers/aws/guide/workflow/#using-stages

Serverless Variables

https://serverless.com/framework/docs/providers/aws/guide/variables/

Leave a Reply