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:

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

within my plugin constructor:

  constructor(serverless: any) {
    console.log(serverless.service.custom.config);
  }

above prints:

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

Thanks!

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: https://github.com/UnitedIncome/serverless-python-requirements/commit/2d1e218c383070d8f8244caa585c12843812f3d4

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: https://github.com/serverless/serverless/pull/3911

1 Like