Serverless Plugin: resolving variable references

I’m trying to write a simple serverless plugin. The main reason for this tool to exist as a plugin is so that I can make the serverless.yml serve as source of truth for some configuration data that needs to exist in some lambda’s as well as some other automation invoked by plugin.
How do I resolve the variable references in the serverless.yml from my plugin?
For example – i have a key in serverless.yml:

  defaultStage: stage
  stage: ${opt:stage, self:custom.defaultStage}
  config: ${file(env-vars/${self:custom.stage}.yml)}

within my plugin constructor:

  constructor(serverless: any) {

above prints:



I also ran into this in serverless-python-requirements. Serverless changed it’s behavior a few releases ago. That variable substitution is not called by the time plugin constructors get called. Unless you actually need it then, just access it in your lifecycle hooks, it’ should be resolved by then.

here was my fix:

1 Like

This is the issue – and the fix!! Many thanks! It might be nice to explicitly call out in the documentation that variables aren’t resolved until after plugin creation has completed.

Thanks again!

That’s a good idea. I’ve gone ahead and made a PR :smiley:

1 Like