Skip to content

resolveConfigurationProperty() unable to resolve env variables properly if undefined or defined in .env #9105

Closed

Description

@neverendingqs

When using JavaScript files, resolveConfigurationProperty() fails if it references an env variable if it is not defined at all or if declared in .env, even if a fallback value was declared. Note: using an environment variable set outside of .env works as expected.

serverless.yml
service: foo
useDotenv: true   # Same error when `useDotenv` is commented out
variablesResolutionMode: 20210219

custom:
  config: ${file(configs.js)}

provider:
  name: aws
  # region: ${opt:region, 'us-east-1'}    # This resolves without issues
  region: ${opt:region, env:AWS_REGION, 'us-east-1'}
  runtime: nodejs14.x
configs.js
module.exports = async ({ options, resolveConfigurationProperty }) => {
  console.log(
    // expected value: 'us-east-2' if `useDotenv: true`, otherwise `us-east-1`
    await resolveConfigurationProperty(['provider', 'region'])
  );
  return {};
}
.env
AWS_REGION=us-east-2
sls package output
 Serverless Error ----------------------------------------

  ServerlessError: Cannot resolve serverless.yml: Variables resolution errored with:
    - Cannot resolve variable at "custom.config": Cannot resolve "configs.js": Returned JS function errored with: ServerlessError: Cannot resolve variable
      at VariablesResolver.resolveDependentProperty (<path-to-repo>\node_modules\serverless\lib\configuration\variables\resolve.js:298:15)
      at async module.exports (<path-to-repo>\configs.js:3:5)
      at async <path-to-repo>\node_modules\serverless\lib\configuration\variables\sources\file.js:116:22
      at async Object.resolve (<path-to-repo>\node_modules\serverless\lib\configuration\variables\sources\file.js:53:21)
      at async VariablesResolver.Object.defineProperties.memoizeMethods.resolveSource.d.normalizer.type.type (<path-to-repo>\node_modules\serverless\lib\configuration\variables\resolve.js:435:24)
      at async <path-to-repo>\node_modules\serverless\lib\configuration\variables\resolve.js:214:20
      at async VariablesResolver.resolveVariableSource (<path-to-repo>\node_modules\serverless\lib\configuration\variables\resolve.js:212:9)
      at async VariablesResolver.resolveVariable (<path-to-repo>\node_modules\serverless\lib\configuration\variables\resolve.js:128:24)
      at async <path-to-repo>\node_modules\serverless\lib\configuration\variables\resolve.js:60:9
      at async Promise.all (index 0)
      at async VariablesResolver.resolveVariables (<path-to-repo>\node_modules\serverless\lib\configuration\variables\resolve.js:54:5)
      at async VariablesResolver.self (<path-to-repo>\node_modules\serverless\lib\configuration\variables\resolve.js:334:9)
      at async Promise.all (index 0)
      at async <path-to-repo>\node_modules\serverless\scripts\serverless.js:122:11
      at module.exports (<path-to-repo>\node_modules\serverless\lib\configuration\variables\eventually-report-resolution-errors.js:24:13)
      at <path-to-repo>\node_modules\serverless\scripts\serverless.js:124:41

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

  Your Environment Information ---------------------------
     Operating System:          win32
     Node Version:              14.15.4
     Framework Version:         2.29.0 (local)
     Plugin Version:            4.5.0
     SDK Version:               n/a
     Components Version:        3.7.3

Installed version
Note: serverless was not installed globally

Framework Core: 2.29.0 (local)
Plugin: 4.5.0
SDK: 4.2.0
Components: 3.7.3

Activity

  1. medikoo commented on Mar 15, 2021

    @medikoo
    Contributor

    @neverendingqs great thanks for reporting. Will be fixed with #9110

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