Skip to content

Variables: Introduce access to resolved stage via sls:stage #6031

Closed

Description

@mnapoli

At the moment to reference the region or stage you have to do:

${opt:stage, self:provider.stage, 'dev'}
${opt:region, self:provider.region, 'us-east-1'}

Since this is pretty common, it would be awesome to have simple variables that aliase that, e.g.

${sls:stage}
${sls:region}

Maintainer note

region is AWS specific, therefore it's expected to be handled in other proposal: #9005

Activity

  1. mnapoli commented on Apr 23, 2019

    @mnapoli
    ContributorAuthor

    @pmuens you added the needs feedback label, are you waiting for feedback from me? Let me know if I need to do anything to make the issue clearer.

  2. mnapoli commented on Apr 25, 2019

    @mnapoli
    ContributorAuthor

    I have opened a PR: #6049

  3. changed the title [-]DX: simplify region and stage variables[/-] [+]Improve design of variables contexts[/+] on May 22, 2020
  4. medikoo commented on Apr 12, 2021

    @medikoo
    Contributor

    This is pretty easy now to implement with a new variables resolver. It's a question of extending the sls source configured here: https://github.com/serverless/serverless/blob/master/lib/configuration/variables/sources/instance-dependent/get-sls.js.

    All properties that are passed to resolve are listed here:

    params: sourceData.params && sourceData.params.map((param) => param.value),
    address: sourceData.address && sourceData.address.value,
    options: this.options,
    isSourceFulfilled: this.fulfilledSources.has(sourceData.type),
    servicePath: this.servicePath,
    resolveConfigurationProperty: async (dependencyPropertyPathKeys) =>
    this.resolveDependentProperty(
    resolutionBatchId,
    propertyPath,
    ensureArray(dependencyPropertyPathKeys, { ensureItem: ensureString })
    ),

    Therefore e.g. stage can be resolved as:

    module.exports = (serverlessInstance) => {
      return {
        resolve: async ({ resolveConfigurationProperty, options }) => {
          let stage = options.stage;
          if (!stage) stage = await resolveConfigurationProperty(["provider", stage"]);
          if (!stage) stage = "dev";
      }
    };

    PR's welcome!

  5. changed the title [-]Improve design of variables contexts[/-] [+]Variables: Introduce access to resolved `stage` via `sls:stage`[/+] on Apr 12, 2021
  6. mnapoli commented on Apr 12, 2021

    @mnapoli
    ContributorAuthor

    Ohh yeah, and thanks a lot for the guidance! I'll try to work on this tomorrow! (famous last words)

  7. mnapoli commented on Apr 13, 2021

    @mnapoli
    ContributorAuthor

    @medikoo I'm confused by the fact that ${sls:XXX} variables are defined in 2 places:

    Should I edit both?

    Regarding tests as well: should I also add tests in Variables.test.js?

    So far I'm editing get-sls.test.js, are there functional tests to edit/create somewhere?

  8. medikoo commented on Apr 13, 2021

    @medikoo
    Contributor

    @mnapoli Variables.js is old resolver which will be removed with v3, we keep it just for legacy compliance but do not extend with new features. So let's not update that one.

    Concerning testing, all related need to be put it into get-sls.test.js (it'll be nice to keep there 100% tests coverage)

  9. mnapoli commented on Apr 13, 2021

    @mnapoli
    ContributorAuthor

    OK thanks, that answers my questions. Because I need to run tests in different scenarios:

    • without options nor provider.stage
    • without options but with provider.stage defined
    • with CLI options

    I need to edit the test a little bit. I'll open the PR and feel free to let me know how to reorganize things the way you prefer.

  10. mnapoli commented on Apr 13, 2021

    @mnapoli
    ContributorAuthor

    I've opened #9296

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