Development, staging and prod environments

Hi @kukula! I recommend handling this with different JSON config files for each environment. You can then tell Serverless which config file to use based on the stage.

For example, let’s say I have an environment variable called DB_CONN that is my db connection string. I want this to vary across dev, staging, and prod environments. I’d set my serverless.yml with:

# serverless.yml

custom:
  defaultStage: dev
  currentStage: ${opt:stage, self:custom.defaultStage} # 'dev' is default unless overriden by --stage flag


provider:
  name: aws
  runtime: nodejs6.10
  stage: ${self:custom.currentStage}
  environment:
      DB_CONN: ${file(./config.${self:custom.currentStage}.json):DB_CONN}

In the custom block, I’m setting up my stage. My default stage is dev, but I can override it by passing a --stage variable at the command line.

Note the DB_CONN line in the environments section – It pulls the DB_CONN value from a file named config.<stage>.json. Now, I can have three different files in my repository:

# config.dev.json
{
    "DB_CONN": "<db_connection_string_for_dev>"
}
# config.staging.json
{
    "DB_CONN": "<db_connection_string_for_staging>"
}
# config.prod.json
{
    "DB_CONN": "<db_connection_string_for_prod>"
}

Let me know if this works for you!

1 Like