Use file for custom settings and add settings to yml

I have a file containing custom settings that are shared across multiple serverless services.
custom: ${file(../shared/all)}

Is it possible to add additional custom settings in my serverless yml configuration that are specific to the one service? What is the syntax? Below is an example of what doesn’t work :wink:

custom: 
  - ${file(../shared/all)}
  scripts:
    commands:
      hello: echo Hello from ${self:service} service!  

The goal is to use the common custom file plus those custom settings specific to just this single service.

checkout https://serverless.com/framework/docs/providers/aws/guide/variables#reference-variables-in-other-files

# serverless.yml
service: new-service
provider: aws
custom: ${file(../myCustomFile.yml)} # You can reference the entire file
functions:
  hello:
      handler: handler.hello
      events:
        - schedule: ${file(../myCustomFile.yml):globalSchedule} # Or you can reference a specific property
  world:
      handler: handler.world
      events:
        - schedule: ${self:custom.globalSchedule} # This would also work in this case
2 Likes

This does not answer my question. This response demonstrates how to use an external file that contains custom values. I’m already doing that as stated in my original post.

The goal is to use the common custom file plus those custom settings specific to just this single service.

Hmm I don’t think this is possible.

You could have separate configs based on the env though and use the stage in the config file name.

custom:
  repoName: serverless
  defaultStage: prod
  currentStage: ${opt:stage, self:custom.defaultStage}
  currentRegion: ${file(./config.${self:custom.currentStage}.json):region}

Like this:

FYI ${self:provider.stage} seems to always have the current stage even it’s been overridden via a command line option.

Yeah, the “right” way to get the stage is ${self:opt.stage, provider.stage} so that you get the option (if set), or the default.

Hmm… in earlier releases of Serverless ${self:provider.stage} was always the stage you’re actually deploying into (i.e. if you changed it using -s it would contain that value instead of the one in the config). I’ve just tested with the latest release and it’s no longer doing that. Time to upgrade my configs :frowning:

Using ${self:opt.stage, provider.stage} failed for me with an Invalid variable reference syntax for variable provider.stage error. I had to specify the self: prefix for provider.stage as well, so this is what I ultimately used that worked:
${self:opt.stage, self:provider.stage}

(Serverless 1.18/ Node 6.11/ OS: darwin)

@AxeOfMen,

I just ran into the same problem – and trying to add the same plugin! I got this to work:

custom: 
  custom: ${file(../shared/all)}
  scripts:
    commands:
      hello: echo Hello from ${self:service} service!

and then reference your variables like
${self:custom.custom.your.var.path}

1 Like