serverless / serverless/serverless

Variable reference passed without interpretation if it contains square brackets.

Open
#8,431 5 comments 0 reactions 1 assignee View on GitHub

@JimBiardVexcel is already working on this.

Since Oct 28, 2020.

question
Dominant language
JavaScript
Stars
46.9k
Forks
5.7k
Avg merge
10h 7m
Merged PRs (30d)
57

Description

A variable reference in a serverless.yml file that has a default value containing square brackets is not interpreted and is passed to the serverless-state.json file without interpretation. That is, if you have a variable reference of the form
${env:TESTVAL, '[0]'}, that entire string will be written to the JSON file instead of [0]. The presence or absence of an environment variable named TESTVAL makes no difference.

serverless.yml
service: simpletest

provider:
  name: aws
  runtime: python2.7

functions:
  hello:
    handler: handler.hello
    environment:
    TESTVAL: ${env:TESTVAL, '[0]'}
serverless package output
Serverless: Packaging service...
Serverless: Excluding development dependencies...

Installed version

Framework Core: 1.58.0
Plugin: 3.8.4
SDK: 2.3.2
Components Core: 1.1.2
Components CLI: 1.5.2

The output AWS template file is

serverless-state.json
{
  "service": {
    "service": "simpletest",
    "serviceObject": {
      "name": "simpletest"
    },
    "provider": {
      "stage": "dev",
      "variableSyntax": "\\${([ ~:a-zA-Z0-9._@'\",\\-\\/\\(\\)*?]+?)}",
      "name": "aws",
      "runtime": "python2.7",
      "region": "us-east-1",
      "versionFunctions": true,
      "remoteFunctionData": null,
      "compiledCloudFormationTemplate": {
        "AWSTemplateFormatVersion": "2010-09-09",
        "Description": "The AWS CloudFormation template for this Serverless application",
        "Resources": {
          "ServerlessDeploymentBucket": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
              "BucketEncryption": {
                "ServerSideEncryptionConfiguration": [
                  {
                    "ServerSideEncryptionByDefault": {
                      "SSEAlgorithm": "AES256"
                    }
                  }
                ]
              }
            }
          },
          "ServerlessDeploymentBucketPolicy": {
            "Type": "AWS::S3::BucketPolicy",
            "Properties": {
              "Bucket": {
                "Ref": "ServerlessDeploymentBucket"
              },
              "PolicyDocument": {
                "Statement": [
                  {
                    "Action": "s3:*",
                    "Effect": "Deny",
                    "Principal": "*",
                    "Resource": [
                      {
                        "Fn::Join": [
                          "",
                          [
                            "arn:",
                            {
                              "Ref": "AWS::Partition"
                            },
                            ":s3:::",
                            {
                              "Ref": "ServerlessDeploymentBucket"
                            },
                            "/*"
                          ]
                        ]
                      }
                    ],
                    "Condition": {
                      "Bool": {
                        "aws:SecureTransport": false
                      }
                    }
                  }
                ]
              }
            }
          },
          "HelloLogGroup": {
            "Type": "AWS::Logs::LogGroup",
            "Properties": {
              "LogGroupName": "/aws/lambda/simpletest-dev-hello"
            }
          },
          "IamRoleLambdaExecution": {
            "Type": "AWS::IAM::Role",
            "Properties": {
              "AssumeRolePolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [
                  {
                    "Effect": "Allow",
                    "Principal": {
                      "Service": [
                        "lambda.amazonaws.com"
                      ]
                    },
                    "Action": [
                      "sts:AssumeRole"
                    ]
                  }
                ]
              },
              "Policies": [
                {
                  "PolicyName": {
                    "Fn::Join": [
                      "-",
                      [
                        "dev",
                        "simpletest",
                        "lambda"
                      ]
                    ]
                  },
                  "PolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                      {
                        "Effect": "Allow",
                        "Action": [
                          "logs:CreateLogStream"
                        ],
                        "Resource": [
                          {
                            "Fn::Sub": "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/simpletest-dev*:*"
                          }
                        ]
                      },
                      {
                        "Effect": "Allow",
                        "Action": [
                          "logs:PutLogEvents"
                        ],
                        "Resource": [
                          {
                            "Fn::Sub": "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/simpletest-dev*:*:*"
                          }
                        ]
                      }
                    ]
                  }
                }
              ],
              "Path": "/",
              "RoleName": {
                "Fn::Join": [
                  "-",
                  [
                    "simpletest",
                    "dev",
                    {
                      "Ref": "AWS::Region"
                    },
                    "lambdaRole"
                  ]
                ]
              }
            }
          },
          "HelloLambdaFunction": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
              "Code": {
                "S3Bucket": {
                  "Ref": "ServerlessDeploymentBucket"
                },
                "S3Key": "serverless/simpletest/dev/1603410984051-2020-10-22T23:56:24.051Z/simpletest.zip"
              },
              "FunctionName": "simpletest-dev-hello",
              "Handler": "handler.hello",
              "MemorySize": 1024,
              "Role": {
                "Fn::GetAtt": [
                  "IamRoleLambdaExecution",
                  "Arn"
                ]
              },
              "Runtime": "python2.7",
              "Timeout": 6
            },
            "DependsOn": [
              "HelloLogGroup",
              "IamRoleLambdaExecution"
            ]
          },
          "HelloLambdaVersionlrnGeLhdPKC5sg8DNEBtFmoDGiu6wOPepjagL7FjxA": {
            "Type": "AWS::Lambda::Version",
            "DeletionPolicy": "Retain",
            "Properties": {
              "FunctionName": {
                "Ref": "HelloLambdaFunction"
              },
              "CodeSha256": "bwcGya4iGKvKt0PVCfYoFSFoHZDk1qrBkb3rWIdwFVk="
            }
          }
        },
        "Outputs": {
          "ServerlessDeploymentBucketName": {
            "Value": {
              "Ref": "ServerlessDeploymentBucket"
            }
          },
          "HelloLambdaFunctionQualifiedArn": {
            "Description": "Current Lambda function version",
            "Value": {
              "Ref": "HelloLambdaVersionlrnGeLhdPKC5sg8DNEBtFmoDGiu6wOPepjagL7FjxA"
            }
          }
        }
      },
      "coreCloudFormationTemplate": {
        "AWSTemplateFormatVersion": "2010-09-09",
        "Description": "The AWS CloudFormation template for this Serverless application",
        "Resources": {
          "ServerlessDeploymentBucket": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
              "BucketEncryption": {
                "ServerSideEncryptionConfiguration": [
                  {
                    "ServerSideEncryptionByDefault": {
                      "SSEAlgorithm": "AES256"
                    }
                  }
                ]
              }
            }
          },
          "ServerlessDeploymentBucketPolicy": {
            "Type": "AWS::S3::BucketPolicy",
            "Properties": {
              "Bucket": {
                "Ref": "ServerlessDeploymentBucket"
              },
              "PolicyDocument": {
                "Statement": [
                  {
                    "Action": "s3:*",
                    "Effect": "Deny",
                    "Principal": "*",
                    "Resource": [
                      {
                        "Fn::Join": [
                          "",
                          [
                            "arn:",
                            {
                              "Ref": "AWS::Partition"
                            },
                            ":s3:::",
                            {
                              "Ref": "ServerlessDeploymentBucket"
                            },
                            "/*"
                          ]
                        ]
                      }
                    ],
                    "Condition": {
                      "Bool": {
                        "aws:SecureTransport": false
                      }
                    }
                  }
                ]
              }
            }
          }
        },
        "Outputs": {
          "ServerlessDeploymentBucketName": {
            "Value": {
              "Ref": "ServerlessDeploymentBucket"
            }
          }
        }
      },
      "vpc": {}
    },
    "pluginsData": {},
    "functions": {
      "hello": {
        "handler": "handler.hello",
        "environment": null,
        "TESTVAL": "${env:TESTVAL, '[0]'}",
        "events": [],
        "name": "simpletest-dev-hello",
        "package": {},
        "memory": 1024,
        "timeout": 6,
        "runtime": "python2.7",
        "vpc": {}
      }
    },
    "serviceFilename": "serverless.yml",
    "layers": {},
    "isDashboardMonitoringPreconfigured": false,
    "artifact": "/Users/jimbiard/Desktop/Copper/code/foo/simpletest/.serverless/simpletest.zip"
  },
  "package": {
    "artifactDirectoryName": "serverless/simpletest/dev/1603410984051-2020-10-22T23:56:24.051Z",
    "artifact": "simpletest.zip"
  }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.