question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

variablesResolutionMode: 20210326 failing to resolve variable with multiple defaults

See original GitHub issue

I have been working on upgrading my project from v1 to v2, and the variable resolution broke on a certain condition. When there are multiple defaults, if the second one fails to resolve, the whole resolution will fail.

serverless.yml
service: aws-node-project

frameworkVersion: '2'
variablesResolutionMode: 20210326

provider:
  name: aws
  runtime: nodejs12.x
  stage: ${opt:stage, env:STAGE, env:AWS_STAGE, "local"}
  lambdaHashingVersion: 20201221

functions:
  hello:
    handler: handler.hello

npx serverless package output
❯ npx serverless package

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

  Cannot resolve serverless.yml: "provider.stage" property is not accessible (configured behind variables which cannot be resolved at this stage)

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

  Your Environment Information ---------------------------
     Operating System:          linux
     Node Version:              14.17.1
     Framework Version:         2.54.0 (local)
     Plugin Version:            5.4.3
     SDK Version:               4.2.6
     Components Version:        3.15.0

Running STAGE=foo npx serverless package will work correctly since then the first default will resolve.

Installed version

Framework Core: 2.54.0 (local)
Plugin: 5.4.3
SDK: 4.2.6
Components: 3.15.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
perrin4869commented, Aug 25, 2021

I see! Yeah I’ve been using provider.stage since like forever, didn’t notice sls:stage until recently 😃 Indeed changing it to provider.defaultStage would clarify the intended use of the option. Still, there are practical difficulties not being able to use the env STAGE as a default for stage.

Usually I do not run serverless directly, but rather define a script inside package.json, like "deploy": "serverless deploy --verbose". This would mean changing the scripts to be rather "deploy": "serverless deploy --verbose --stage $STAGE". The difficulty here is, what if I run this locally, and I do not have $STAGE defined? In this case I want to default to "local" or "development" or something, which is what I was achieving by doing stage: ${opt:stage, env:STAGE, "local"}.

Maybe stage could be a special case, where it does rely on an environment variable when setting it the first time?

Edit: Of course I can always work around this issue to use serverless as is intended, but I think there is practicality in allowing the stage to depend on the environment and would prefer it to stay like that.

1reaction
medikoocommented, Aug 25, 2021

The staging pipeline will have STAGE=staging and the production pipeline will have STAGE=production. I do need to reevaluate the usage of AWS_STAGING variable.

Do you rely on one CI configuration for different pipelines? If such you may also solve it by keeping setting the STAGE, but by propagating it to sls via --stage=$STAGE flag.

Note that provider.stage setting in a configuratioin is technically a setting for default stage. It’s a last resort fallback if there’s no --stage flag passed (the default is normally dev, but if you wish to change it, it’s when provider.stage should be configured). We also have proposal of renaming this to provider.defaultStage -> https://github.com/serverless/serverless/issues/8684 so it’s not confusing, but we’re also considering totally removing support for this property (as it’s hard to find a good use case for dev not being a default stage)

Additionally if you want to rely on resolved stage in configuration you should simply refer to sls:stage (until it was introduced, developers quite often ensured that actual stage was resolved on provider.stage, and then referring to that in variables, but it was just dirty workaround for fact that there wasn’t anything like sls:stage available. It is now)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serverless deprecation warning on .env files
useDotenv: true variablesResolutionMode: 20210326. The second warning message will be an error, provider.profile wasn't able to be resolved.
Read more >
Variables
Note: the method described below works by default in Serverless v3, but it requires the variablesResolutionMode: 20210326 option in v2. A variable resolver ......
Read more >
Using AWS Lambda environment variables
Choose Add environment variable. Enter a key and value. Requirements. Keys start with a letter and are at least two characters.
Read more >
Docker Stack Deploy doesn't resolve environment ...
Docker compose is able to resolve the default value of the environment variable, however docker stack deploy is unable to do the same ......
Read more >
Allow default value for variables (&7437) · Epics · GitLab.org
Allow variables defined global or on job level to have a default value as for example: ... so in merge requests the build...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found