Skip to content

Overwriting variable resolution require string fallbacks to be fully resolvable #7435

Closed

Description

@sschulze

Bug Report

Description

1. What did you do?
In my serverless.yaml I declare a variable with this syntax: ${env:SomeUrl, "${cf:my-stack.ServiceEndpoint}/api"}.
My intent is to use the cloudformation resolution (which only gives part of url) in most cases. But in some cases I want to override the URL by a sysenv variable. For example when there is no "my-stack" deployed.
But if I set env variable SomeUrl and my-stack is not deployed deployed, it fails :-(

2. What happened?

ServerlessError: Stack with id my-stack does not exist

3. What should've happened?
The resolver should succeed by just successfully resolve env:SomeUrl. Because the second part is only some kind of fallback it is of no interest if the first part is resolvable.

4. What's the content of your serverless.yml file?

custom:
  - someurl: ${env:SomeUrl, "${cf:my-stack.ServiceEndpoint}/api"}

5. What's the output you get when you use the SLS_DEBUG=* environment variable (e.g. SLS_DEBUG=* serverless deploy)
I call SLS_DEBUG=* SomeUrl=http://localhost/foo sls package

The output is

[...]
Serverless: [AWS cloudformation 400 0.267s 0 retries] describeStacks({ StackName: 'my-stack' })

  Serverless Error ---------------------------------------

  ServerlessError: Stack with id my-stack does not exist
      at C:\Users\xyz\AppData\Roaming\npm\node_modules\serverless\lib\plugins\aws\provider\awsProvider.js:329:27
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information ---------------------------
     Operating System:          win32
     Node Version:              12.16.1
     Framework Version:         1.65.0
     Plugin Version:            3.4.1
     SDK Version:               2.3.0
     Components Version:        2.22.3

Activity

  1. changed the title [-]Overwriting variable resolution require valid options[/-] [+]Overwriting variable resolution require valid fallbacks[/+] on Mar 7, 2020
  2. changed the title [-]Overwriting variable resolution require valid fallbacks[/-] [+]Overwriting variable resolution require string fallbacks to be fully resolvable[/+] on Mar 7, 2020
  3. medikoo commented on Mar 7, 2020

    @medikoo
    Contributor

    @sschulze I agree, ideally fallback should be resolved only when we fallback to it. It might be an issue in design of variable resolver. We're open for PR that improves that.

    I'm adding @erikerikson to the discussion, as he has very good understanding of variable resolution mechanism in the Framework, and may provide a valuable insight.

  4. erikerikson commented on Mar 7, 2020

    @erikerikson
    Contributor

    Hmm... Nothing great to say off hand. I'd guess the regex is picking up the inner variable in the string before noticing the outer variable and therefore is unaware of the outer fallback context. Without thinking too much, this may be a limit of the current implementation strategy. There was some code that identified these things separately and there may have been a regression but I can't be certain without taking a look.

  5. AhmedFat7y commented on May 14, 2020

    @AhmedFat7y
    Contributor

    Given we have Env variable SomeUrl=http://localhost/foo The Steps followed to resolve the variable with default you provided ${env:SomeUrl, "${cf:my-stack.ServiceEndpoint}/api"} are as follows:

    1. Use Variable Syntax regex \${([^{}]+?)} to extract first variable to resolve, which detects the most inner part of nested vars as it excludes the curly brackets }{. ${cf:my-stack.ServiceEndpoint}
    2. Render the value of the resolved variable. ${env:SomeUrl, "http://example.com/api"}
    3. Run detection again. ${env:SomeUrl, "http://example.com/api"}
    4. Split on comma that is not inside quotes. env:SomeUrl
    5. Resolve
    6. Replace
    7. Decide

    Not sure if the process can be reversed though without a huge refactor. I have hard time following all the paths.

  6. medikoo commented on May 14, 2020

    @medikoo
    Contributor

    Not sure if the process can be reversed though without a huge refactor. I have hard time following all the paths.

    I totally agree. It's very hard to solve without rethinking (and probably rewriting) whole variable resolution strategy.
    I think it should be solved together with refactor proposed here: #7207

  7. medikoo commented on Apr 12, 2021

    @medikoo
    Contributor

    Closing as it's works as proposed with new variables resolver, which was shipped recently

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