Reference sub-variables in an included file in serverless.yml

I’d like to setup my configuration so that I have a single config.json file with different stages in it, like this:

{
  "local": {
    "username": "abc123",
    "password": "mypass",
    "database": "localdb",
    "host": "localhost",
    "dialect": "mysql",
    "redishost": "localhost",
    "redisport": "46379"
  },
  "dev": {
    "username": "devuser",
    "password": "devpass",
    "database": "devdb",
    "host": "localhost",
    "dialect": "mysql",
    "redishost": "localhost",
    "redisport": "6379"
  }
}

The problem I have is how to reference this within serverless.yml. I’ve tried this:


provider:
  name: aws
  runtime: nodejs8.10
  stage: ${opt:stage, 'dev'}
  region: us-west-2
  environment:
    DB_NAME: ${file(config/config.json):${self:provider.stage}.database}
    DB_USER: ${file(config/config.json):${self:provider.stage}.username}
    DB_PASS: ${file(config/config.json):${self:provider.stage}.password}
    DB_HOST: ${file(config/config.json)${self:provider.stage}.host}
    REDIS_HOST: ${file(config/config.json):${self:provider.stage}.redishost}
    REDIS_PORT: ${file(config/config.json):${self:provider.stage}.redisport}

But I get an error Invalid variable syntax when referencing file “config/config.json” sub properties Please use “:” to reference sub properties.

I also tried using the “:” instead of “.” to no avail.

Any help would be appreciated. Thanks!

Jon

Is your config.json in a ‘config’ folder off the root of your project?
From your config, I’m assuming it’s supposed to be, so here’s a working sample:

You can test it with
sls print -s dev
and
sls print -s local

Hope it helps.

Thanks for the suggestion. Actually, that’s exactly how I have it structured. Turns out there was a missing colon on the DB_HOST line. Unfortunately, serverless isn’t clear on what line is causing the problem. Would be helpful if it tied a line number to the error!