Skip to content

File variable resolution happens before properties it depends on are resolved #7895

Closed

Description

@walery

Our serverless.yml file has dynamic service name. It looks like this:

service: ${self:custom.product}-${self:custom.component}

custom:
  product: foo
  component: backend

We can request the name of stack inside of ${file(foo.js):bar} call with:

module.exports.bar = (serverless) => {
  serverless.providers.aws.naming.getStackName();
  // ...
};

Result of serverless.providers.aws.naming.getStackName() is wrong:

actual ${self:custom.product}-${self:custom.component}-dev
expected foo-backend-dev

I guess that this is timing problem, so that jump to function happens before service variable is resolved.

serverless.yml
service: ${self:custom.product}-${self:custom.component}

provider:
  name: aws

custom:
  product: foo
  component: backend

resources:
  - Outputs:
      Foo:
        Value: ${file(foo.js):bar}
foo.js
module.exports.bar = (serverless) => {
  serverless.cli.consoleLog('');
  serverless.cli.consoleLog(serverless.providers.aws.naming.getStackName());
  serverless.cli.consoleLog('');

  return 'just dummy...';
};
sls print output
Serverless: Load command interactiveCli
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command config:tabcompletion
Serverless: Load command config:tabcompletion:install
Serverless: Load command config:tabcompletion:uninstall
Serverless: Load command create
Serverless: Load command install
Serverless: Load command package
Serverless: Load command deploy
Serverless: Load command deploy:function
Serverless: Load command deploy:list
Serverless: Load command deploy:list:functions
Serverless: Load command invoke
Serverless: Load command invoke:local
Serverless: Load command info
Serverless: Load command logs
Serverless: Load command metrics
Serverless: Load command print
Serverless: Load command remove
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command slstats
Serverless: Load command plugin
Serverless: Load command plugin
Serverless: Load command plugin:install
Serverless: Load command plugin
Serverless: Load command plugin:uninstall
Serverless: Load command plugin
Serverless: Load command plugin:list
Serverless: Load command plugin
Serverless: Load command plugin:search
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command upgrade
Serverless: Load command uninstall
Serverless: Load command login
Serverless: Load command logout
Serverless: Load command generate-event
Serverless: Load command test
Serverless: Load command dashboard
Serverless: Load command output
Serverless: Load command output:get
Serverless: Load command output:list
Serverless: Load command param
Serverless: Load command param:get
Serverless: Load command param:list
Serverless: Load command studio
Serverless: Load command dev

${self:custom.product}-${self:custom.component}-dev

service: foo-backend
provider:
  name: aws
custom:
  product: foo
  component: backend
resources:
  - Outputs:
      Foo:
        Value: just dummy...

Installed version

Framework Core: 1.74.1
Plugin: 3.6.15
SDK: 2.3.1
Components: 2.31.9

Activity

  1. walery commented on Jul 1, 2020

    @walery
    ContributorAuthor

    I've found out that serverless.variables.populateProperty(...) can resolve string with variables. But sadly it looks like there is another issue that prevent to use it. See #7896 for more details.

  2. changed the title [-]`providers.aws.naming.getStackName()` didn't resolve variables inside of ${file(...):func} call[/-] [+]File variable resolution happens before properties it depends on are resolved[/+] on Jul 2, 2020
  3. medikoo commented on Jul 2, 2020

    @medikoo
    Contributor

    @walery These are known issues with variables resolution, which we discuss here #7207

    This case in interesting, as here you'd prefer that variables in custom block are resolved, before file source variables are resolved. On the other hand we have cases where user would prefer to keep their custom block in external file, and in that case it's file source that has to be resolved upfront.

    Problem with file source, is that in case of JS files, it's not deductible on which properties it depends. So that problem is not solveable easily

  4. walery commented on Jul 2, 2020

    @walery
    ContributorAuthor

    This case in interesting, as here you'd prefer that variables in custom block are resolved, before file source variables are resolved. On the other hand we have cases where user would prefer to keep their custom block in external file, and in that case it's file source that has to be resolved upfront.

    Well, I think both cases are valid and both are probably needed.
    It would be nice to have a way to resolve unresolved variables (like serverless.variables.populateProperty(...) witch didn't work because of #7896).

  5. medikoo commented on Jul 2, 2020

    @medikoo
    Contributor

    @walery We have tons issues like that with variable resolution, and I don't think any specific case should be handled individually. Ideally we should prepare new good design of variables resolution, which solves all known issues.

    See #7207 it's where we tackle that problem, any constructive input is welcome

  6. medikoo commented on Mar 3, 2021

    @medikoo
    Contributor

    @walery we've just introduced a new variables resolvedr, which addresses that issue, and you can access all configuration properties in resolved shape in JS variable resolver (ofc as long as it does not trigger circular dependency).

    Check: https://www.serverless.com/framework/docs/providers/aws/guide/variables/#exporting-a-function

    One limitation (made for a good reason) is that you cannot access serverless instance, resolver is just provided with resolveConfigurationProperty function, through which you may retrieve values of other configuration properties. In your case stack name can be resolved as:

    module.exports = async ({ options, resolveConfigurationProperty }) => {
      let stage = options.stage;
      if (!stage) {
        stage = await resolveConfigurationProperty(['provider', 'stage']);
        if (!stage) stage = 'dev'; // Framework default
      }
      const stackName = `${await resolveConfigurationProperty('service')}-${stage}`;
      
      ...
    }

    Anyway we're open to provide some shortcut methods for above

  7. walery commented on Mar 19, 2021

    @walery
    ContributorAuthor

    Thank you for taking care and actively mentioning my issue. And sorry for the late response...

    I'll take a look at the new way of resolution and give feedback. It might take few days, but this is now on my active TODO list.

  8. medikoo commented on Apr 12, 2021

    @medikoo
    Contributor

    Closing as I believe it's sorted out by new variables resolver

  9. walery commented on Apr 22, 2021

    @walery
    ContributorAuthor

    I was looking at the new variable resolver. This works fine 👍
    But I miss the serverless instance 😢

    I understand the motivation for removing this instance. And in general, I agree with this decision.

    IMO there is a gap between giving the user access to serverless instance (and all the internal stuff) and giving nothing (except variable resolution and cli options).

    In my initial post, there are already two things that are not accessible anymore without the serverless instance.

    1. serverless.providers.aws.naming.getStackName() - I can build it by my own (service + stage). But doing this requires knowing all the possible ways how this could be changed. For example, the stackName could be overwritten by provider.stackName. Taking the stackName from serverless instance gives me the guarantee to have the correct stack name.

    2. serverless.cli.consoleLog - Using the serverless consoleLog has the benefit to keep all the config setted for looking by the framework. As an example: if consoleLog logs into different targets (like console and file) then logs from my plugin would sink in all targets as well. Another example, if consoleLog is printing date+time before the log itself.

    I know that I'm using some more of the stuff from serverless instance - and I'm afraid that with version 3 my plugins won't work anymore. I believe having an additional object that gives restricted access to some properties/functions of running serverless instance would be great.

  10. medikoo commented on Apr 23, 2021

    @medikoo
    Contributor

    @walery great thanks for valuable insight.

    While we don't have ready solutions yet, I believe all of your points can be addressed well.

    serverless.providers.aws.naming.getStackName()

    We may introduce sls:stackName variable (as we recently introduced sls:stage, and then with #9112 done (I'm working on it correctly, so that should be arrive with one of next releases) you'd be able to resolve it as:

    module.exports = async ({ resolveSource }) => {
      const stackName = await resolveSource("sls", { address: "stackName" })`;
    }

    So if you find that useful please open a new issue to request sls:stackName

    serverless.cli.consoleLog

    This will be addressed well with #1720, as after that's done, you'd be able to require our logger as:

    const slsLog = require('serverless/log);
    ...
    slsLog.info("some log");
  11. walery commented on Apr 26, 2021

    @walery
    ContributorAuthor

    This is awesome, thank you 🙏
    You're doing great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions