serverless-export-env icon indicating copy to clipboard operation
serverless-export-env copied to clipboard

Environment variables mixed between stages on SF => 2.60

Open serverlesspolska opened this issue 2 years ago • 3 comments

Hi @arabold,

Recently, I spotted strange behavior which results in wrong environment variables being fetched by the plugin.

I want to highlight that this problem does not occur with Serverless Framework v2.22 installed globally. However, most recent versions since 2.60 suffer from it.

First problem

Locally installed SF does not see prod stage.

$ node_modules/.bin/sls export-env --all -s qa
Serverless: Deprecation warning: Variables resolver reports following resolution errors:
              - Cannot resolve variable at "custom.deployment": Cannot load file from outside of a project directory (configure "projectDir" to extend project boundary)
            From a next major this will be communicated with a thrown error.
            Set "variablesResolutionMode: 20210326" in your service config, to adapt to new behavior now
            More Info: https://www.serverless.com/framework/docs/deprecations/#NEW_VARIABLES_RESOLVER

$ node_modules/.bin/sls export-env --all -s prod
 
 Serverless Error ----------------------------------------
 
  Stack with id <reducted>-prod does not exist
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              14.16.1
     Framework Version:         2.64.1 (local)
     Plugin Version:            5.5.0
     SDK Version:               4.3.0
     Components Version:        3.17.1
 
Serverless: Deprecation warning: Variables resolver reports following resolution errors:
              - Cannot resolve variable at "custom.deployment": Cannot load file from outside of a project directory (configure "projectDir" to extend project boundary)
            From a next major this will be communicated with a thrown error.
            Set "variablesResolutionMode: 20210326" in your service config, to adapt to new behavior now
            More Info: https://www.serverless.com/framework/docs/deprecations/#NEW_VARIABLES_RESOLVER

But globally installed SFv2.22 sees prod stage.

$ sls export-env --all -s prod 
Serverless: Running "serverless" installed locally (in service node_modules)
Serverless: Deprecation warning: Variables resolver reports following resolution errors:
              - Cannot resolve variable at "custom.deployment": Cannot load file from outside of a project directory (configure "projectDir" to extend project boundary),
              - Cannot resolve variable at "functions.addPurchasedCredits.environment.eventBusName": AWS profile "${self:custom.deployment.myProfile.${self:custom.myStage}}" doesn't seem to be configured,
              - Cannot resolve variable at "functions.addPurchasedCredits.iamRoleStatements.1.Resource.0": AWS profile "${self:custom.deployment.myProfile.${self:custom.myStage}}" doesn't seem to be configured,
              - Cannot resolve variable at "functions.storeUtilization.environment.eventBusName": AWS profile "${self:custom.deployment.myProfile.${self:custom.myStage}}" doesn't seem to be configured,
              - Cannot resolve variable at "functions.storeUtilization.iamRoleStatements.1.Resource.0": AWS profile "${self:custom.deployment.myProfile.${self:custom.myStage}}" doesn't seem to be configured
            From a next major this will be communicated with a thrown error.
            Set "variablesResolutionMode: 20210326" in your service config, to adapt to new behavior now
            More Info: https://www.serverless.com/framework/docs/deprecations/#NEW_VARIABLES_RESOLVER

Second problem

The plugin does not fetch properly environment variables, mixes them from different stages. This happens regardless of local / global Serverless Framework installation. In my CI/CD I have globally installed SFv2.60 and the same problem exists. This does not happen on SFv2.22.

For example, on SFv2.64.1 (local) sls export-env --all -s qa results in output which is:

STAGE=dev
region=us-west-2
service=<reducted>
dlq=https://<reducted>-qa-dead-letter
athenaOutputLocation=s3://<reducted>/queries-from-lambda-dev/
utilizationTopicArn=arn:aws:sns:<reducted>-qa-utilization
tableName=<reducted>-dev
eventBusName=<reducted>-dev

As you can see, some are from dev stage and other from qa. All of them should be from qa.

serverlesspolska avatar Oct 27 '21 08:10 serverlesspolska

I am getting exactly the same behaviour as issue 2 above: "serverless": "^2.71.0", "serverless-export-env": "^2.0.0",

conorw avatar Jan 16 '22 00:01 conorw

Exactly the same is happening on those versions too

"serverless": "2.70.0", "serverless-export-env": "2.0.0",

argjentsahiti avatar Jan 31 '22 11:01 argjentsahiti

Some more information.

Even if this the 1st call to be made, it will still always default to the "dev" environment.

i.e. running serverless export-env --stage test --verbose will always evaluate the "dev" stage, inside the serverless.yml

provider: name: aws stage: ${opt:stage,'dev'}

In my case, I only need to run the export-env for my test env, so by changing the default stage in the "stage" property, means that it will always default to the "test" stage:

provider: name: aws stage: ${opt:stage,'test'}

conorw avatar Feb 15 '22 19:02 conorw