variablesResolutionMode: 20210326 failing to resolve variable with multiple defaults
See original GitHub issueI 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:
- Created 2 years ago
- Comments:9 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I see! Yeah I’ve been using
provider.stage
since like forever, didn’t noticesls:stage
until recently 😃 Indeed changing it toprovider.defaultStage
would clarify the intended use of the option. Still, there are practical difficulties not being able to use the envSTAGE
as a default for stage.Usually I do not run
serverless
directly, but rather define a script insidepackage.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 doingstage: ${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.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 tosls
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 normallydev
, but if you wish to change it, it’s whenprovider.stage
should be configured). We also have proposal of renaming this toprovider.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 fordev
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 onprovider.stage
, and then referring to that in variables, but it was just dirty workaround for fact that there wasn’t anything likesls:stage
available. It is now)