Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In serverless versions 2.3.0 and higher, Resolution of Nested Variables is incomplete / broken #8487

Closed
logicalor opened this issue Nov 9, 2020 · 2 comments
Assignees
Labels

Comments

@logicalor
Copy link

logicalor commented Nov 9, 2020

Serverless 2.3.0 and higher does not always resolve nested variables correctly. Presumably Serverless supports nested variables, as they are mentioned in Serverless' documentation, and resolved correctly prior to v2.3.0.

The example below is a version of the aws-nodejs template (ie what is created when running serverless create --template aws-nodejs) modified to include a nested variable to define provider.region. provider.region is then used to provide an environment variable to the handler. The handler code is modified to emit the provided environment variable.

As the examples demonstrate, in serverless v2.2.0 and below, the region is correctly interpreted from the yml config and passed into the handler. In 2.3.0 and higher, the region is not correctly interpreted, and instead a literal fragment of the nested variable is passed instead.

I believe this is due to a regex change that was introduced in v2.3.0 to address CloudFormation variable label collisions, which has had the inadvertent effect of breaking nested variable resolution. My belief that this is the issue is further reinforced by the fact if I revert the regex in question in any ^2.2.0 version of serverless, the problem goes away.

serverless.yml
service: serverless-bork
frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x
  region: ${self:custom.${env.STAGE, 'development'}.regions.${self:custom.${env.STAGE, 'development'}.defaultRegion}, ${env:AWS_REGION, 'ap-southeast-2'}}
  environment:
    TEST_REGION: ${self:provider.region}

functions:
  hello:
    handler: handler.hello

custom:
  development:
    defaultRegion: "0"
    regions:
      - ap-southeast-2
      - ap-southeast-1
  staging:
    defaultRegion: "0"
    regions:
      - ap-southeast-2
      - ap-southeast-1
  production:
    defaultRegion: "0"
    regions:
      - ap-southeast-2
      - ap-southeast-1
handler.js
'use strict';

module.exports.hello = async event => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: `Region is ${process.env.TEST_REGION}`,
        input: event,
      },
      null,
      2
    ),
  };
};
(serverless v2.2.0) serverless invoke local -f hello --data {} output
{
    "statusCode": 200,
    "body": "{\n  \"message\": \"Region is ap-southeast-2\",\n  \"input\": {}\n}"
}

(serverless ^v2.2.0) serverless invoke local -f hello --data {} output
Serverless: Configuration warning at 'provider.region': should be equal to one of the allowed values [us-east-1, us-east-2, us-gov-east-1, us-gov-west-1, us-west-1, us-west-2, af-south-1, ap-east-1, ap-northeast-1, ap-northeast-2, ap-northeast-3, ap-south-1, ap-southeast-1, ap-southeast-2, ca-central-1, cn-north-1, cn-northwest-1, eu-central-1, eu-north-1, eu-south-1, eu-west-1, eu-west-2, eu-west-3, me-south-1, sa-east-1]
Serverless:  
Serverless: Learn more about configuration validation here: http://slss.io/configuration-validation
Serverless:  
{
    "statusCode": 200,
    "body": "{\n  \"message\": \"Region is ${self:custom.${env.STAGE, 'development'}.regions.${self:custom.${env.STAGE, 'development'}.defaultRegion}, ap-southeast-2}\",\n  \"input\": {}\n}"
}

Installed version

Framework Core: 2.11.0
Plugin: 4.1.2
SDK: 2.3.2
Components: 3.3.0
@logicalor logicalor changed the title In serverless versions 2.3.0 and higher, Resolution of Variable Variables is incomplete / broken In serverless versions 2.3.0 and higher, Resolution of Nested Variables is incomplete / broken Nov 9, 2020
@medikoo
Copy link
Contributor

medikoo commented Nov 9, 2020

@logicalor there's an error in your configuration, it should be env:STAGE not env.STAGE

@logicalor
Copy link
Author

@medikoo you're right - well spotted! Thank you for pointing that out - all is working fine with v2.11.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants