Serverless.yml referencing stage variables in env.yml

Hi, I’m trying to get to grips with setting different variables depending on the stage I am deploying to. I’ve got an env.yml that looks a bit like this:

default_env: &default_env
  DB_SCHEMA:  "client"
  DB_USER:    "client"
  ACCOUNT_ID: '1234567890'

dev:
  <<: *default_env
  DB_HOST: "client.qwer1234.eu-west-1.rds.amazonaws.com"
  DB_PASS: "qwer1234"
  DB_SECURITY_GROUP: "sg-qwer1234"
  DB_SUBNET_1: "subnet-qwer1234"
  DB_SUBNET_2: "subnet-qwer1234"
  DB_SUBNET_3: "subnet-qwer1234"

live:
  <<: *default_env
  DB_HOST: "client.asdf1234.eu-west-1.rds.amazonaws.com"
  DB_PASS: "asdf1234"
  DB_SECURITY_GROUP: "sg-asdf1234"
  DB_SUBNET_1: "subnet-asdf1234"
  DB_SUBNET_2: "subnet-asdf1234"
  DB_SUBNET_3: "subnet-asdf1234"

And I want to reference some of them in my serverless.yml which looks like this:

functions:
  myFunction:
    handler: handler.myFunction
    vpc:
      securityGroupIds:
        - DB_SECURITY_GROUP
      subnetIds:
        - DB_SUBNET_1
        - DB_SUBNET_2
        - DB_SUBNET_3

I’ve tried a few different incantations, but none seem to work - can anyone help me out please?

Thanks!

I believe you could do something like this in serverless.yml:

functions:
  myFunction:
    handler: handler.myFunction
    vpc:
      securityGroupIds:
        - ${file(env.yml):${self:provider.stage}.DB_SECURITY_GROUP}
      subnetIds:
        - ${file(env.yml):${self:provider.stage}.DB_SUBNET_1}
        - ${file(env.yml):${self:provider.stage}.DB_SUBNET_2}
        - ${file(env.yml):${self:provider.stage}.DB_SUBNET_3}
1 Like

Thanks tommedema - that seems really obvious now I know it!!!