Merging environment configs

I have the following:

provider:
  environment: ${file(env.yml):${self:provider.stage}}

and in my env.yml I have the following:

As you can see it gets very repetitive duplicating variables into each stage. Is there a way I can have a second file for globals that I can merge with this file somehow? That way I can remove duplicates from each stage.

YAML allows:

default_env: &default_env
  SCOPE: 'read'
  TABLE: 'apple'

dev:
  <<: *default_env
  TABLE: 'banana'
  PASSWORD: 'my-password'

If you want the longer version I wrote it here

3 Likes

Perfect, thank you!!

The better solution will be to use SSM parameters. It will help not only for yml file but also it will allow you to share variables between multiple serverless projects / lambda functions.
Following link might help you.

hey @buggy and @dhavalyours,

Thanks for your answers I am using following for my service level environment which will be shared across functions.

environment: ${ssm:/aws/reference/secretsmanager/myServiceEnv~true}

How can I add additional env variables which are not in my secrets manager or something I want to generate through serverless opts. example:

environment: 
    TABLE_NAME: ${self:provider.stage}-${self:service}
    VAR2: "Value"

How to merge both so that I have secrets from secrets manager and additional variables provided in serverless.yml?

# outcome
environment: 
    SECRET: "From Secrets Manager" 
    TABLE_NAME: ${self:provider.stage}-${self:service}
    VAR2: "Value"

I tried merging yaml objects using tips provided in this post.