Aws-cdk: Environment variable parsing of CDK lambda is not working properly

Created on 3 Sep 2019  Â·  9Comments  Â·  Source: aws/aws-cdk

:bug: Bug Report

What is the problem?

When using environment variables to define lambda function, the value will become totally different in AWS console panel.

Reproduction Steps

  1. Define a TIMETABLE variable with 17:00 string.

    const lambdaFn = new lambda.Function(this, 'Singleton', {
      environment: {VALUE: 123, 'TIMETABLE': '17:00'}
    });
    
  2. Generate template.yaml.

    cdk synth --no-staging > template.yaml
    

    And the TIMETABLE seems weird from here.

    SingletonXXXXXXX:
      Type: AWS::Lambda::Function
      Environment:
        Variables:
          VALUE: "123"
          TIMETABLE: 17:00
    
  3. Go to lambda function of AWS console panel, and the value of environment variable TIMETABLE is 1020.

Environment

  • CDK CLI Version: 1.6.1
  • Module Version: 1.6.1
  • OS: OSX Mojave
  • Language: TypeScript
bug closing-soon packagtools

All 9 comments

This is why the cloudformation team needs to update to a version of YAML that isn't 100 Million years old.

https://medium.com/@sidneyliebrand/the-greatnesses-and-gotchas-of-yaml-5e3377ef0c55

We’ve already seen some weird behavior with some unquoted string values magically turning into booleans but there is more! YAML parses numbers in ii:jj format in base 60!

I'm not sure there's anything CDK can do because CDK is outputting the correct JSON, but YAML (in its infinite wisdom) is stripping the quotes off the "17:00"

@rhboyd If CDK can provide an option to use template.json as stack building input, it will work around this.

yeah, that's what's confusing to me. CDK has been pretty insistent on sticking with JSON as the output format, but they deploy the YAML version to S3 and the JSON->YAML conversion is what's stripping the " off the "17:00"

As @rhboyd pointed out, this is coming from the serializing function of the underlying YAML library.

You could use the --json option of cdk synth to generate the output in JSON, or use the one we generate for every synth command that you can find in the cdk.out folder.
@tpai - Would this sufficiently mitigate what you're trying to do here?

cdk deploy uses the JSON format, so this shouldn't occur when the stack is deployed via the deploy command.

However, I'm unable to reproduce this -

Here's my CDK app -

#!/usr/bin/env node
import cdk = require('@aws-cdk/core');
import lambda = require('@aws-cdk/aws-lambda');

class FirstStack extends cdk.Stack {

    constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
      super(scope, id, props);

    const firstLambda = new lambda.Function(this, 'firstLambda', {
        functionName: 'FirstLambda',
        code: lambda.Code.asset('resources'),
        handler: 'index.handler',
        runtime: lambda.Runtime.NODEJS_10_X,
        environment: {
          'TIME': '17:00'
        }
      });
    }
}

const app = new cdk.App();
new FirstStack(app, 'FirstStack');
app.synth();

Running cdk synth --no-staging generates YAML with this snippet in it -

  firstLambda395F9ADE:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.handler
      Role:
        Fn::GetAtt:
          - firstLambdaServiceRoleB6408C31
          - Arn
      Runtime: nodejs10.x
      Environment:
        Variables:
          TIME: "17:00"
      FunctionName: FirstLambda

And deploying the YAML file through Cloudformation yields a Lambda function -

⇒  aws lambda get-function-configuration --function-name FirstLambda
{
    ...
    "Environment": {
        "Variables": {
            "TIME": "17:00"
        }
    },
    ...
}

Can you make sure you're running the latest version of CDK, as well as, have pulled the latest version of the YAML npm package? I'm running on 1.7.0 (from package-lock.json).

I was able to reproduce this several different ways. I’ll get some sample
code posted shortly.

On Tuesday, October 1, 2019, Niranjan Jayakar notifications@github.com
wrote:

However, I'm unable to reproduce this -

Here's my CDK app -

!/usr/bin/env node

import cdk = require('@aws-cdk/core');
import lambda = require('@aws-cdk/aws-lambda');

class FirstStack extends cdk.Stack {

constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {

  super(scope, id, props);



const firstLambda = new lambda.Function(this, 'firstLambda', {

    functionName: 'FirstLambda',

    code: lambda.Code.asset('resources'),

    handler: 'index.handler',

    runtime: lambda.Runtime.NODEJS_10_X,

    environment: {

      'TIME': '17:00'

    }

  });

}

}

const app = new cdk.App();
new FirstStack(app, 'FirstStack');
app.synth();

Running cdk synth --no-staging generates YAML with this snippet in it -

firstLambda395F9ADE:

Type: AWS::Lambda::Function

Properties:

  Handler: index.handler

  Role:

    Fn::GetAtt:

      - firstLambdaServiceRoleB6408C31

      - Arn

  Runtime: nodejs10.x

  Environment:

    Variables:

      TIME: "17:00"

  FunctionName: FirstLambda

And deploying the YAML file through Cloudformation yields a Lambda
function -

⇒ aws lambda get-function-configuration --function-name FirstLambda

{

...

"Environment": {

    "Variables": {

        "TIME": "17:00"

    }

},

...

}

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/aws/aws-cdk/issues/3918?email_source=notifications&email_token=AF3KL47DWTZKLRNQURIHC6DQMNKL3A5CNFSM4ITIS2N2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEABMJWI#issuecomment-537052377,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AF3KL4Y4EK7S3VSZW5PVMDDQMNKL3ANCNFSM4ITIS2NQ
.

This is strange. It's working now. I remember reproducing this several times trying to escape the string characters so it wouldn't strip them.

It's possible that it was fixed in the underlying YAML package we use - https://github.com/eemeli/yaml/.

Possibly this - https://github.com/eemeli/yaml/commit/884fe078566a3c569e3da1112ff21f45830df2fe ?

Closing this for now. Re-open if you see it again.

Was this page helpful?
0 / 5 - 0 ratings