DEV Community

loading...

Making Lambda@Edge and executing `cdk deploy` occurred a 400 Error with "The function cannot have environment variables"

texmeijin profile image meijin ・1 min read

Environment

  • aws-cdk v1.53.0 or above
  • aws-lambda-nodejs

My code

I want to deploy lambda@edge source code by aws-cdk.

    new NodejsFunction(this, 'some-awesome-function', {
      role: new Role(this, 'AllowLambdaServiceToAssumeRole', {
        assumedBy: new CompositePrincipal(
          new ServicePrincipal('lambda.amazonaws.com'),
          new ServicePrincipal('edgelambda.amazonaws.com')
        ),
      }),
    })
Enter fullscreen mode Exit fullscreen mode

My command

cdk deploy
Enter fullscreen mode Exit fullscreen mode

Error Message

CREATE_FAILED        | AWS::CloudFront::Distribution | XXXX/CFDistribution (XXXXCFDistributionXXXX) The function cannot have environment variables. Function: arn:aws:lambda:us-east-1:XXXX:function:CdkStack-XXXXD6AD4BEB-1XXXXXXMFY8E:1 (Service: AmazonCloudFront; Status Code: 400; Error Code: InvalidLambdaFunctionAssociation;
Enter fullscreen mode Exit fullscreen mode

Solution

set awsSdkConnectionReuse to false in your props.

    new NodejsFunction(this, 'some-awesome-function', {
      role: new Role(this, 'AllowLambdaServiceToAssumeRole', {
        assumedBy: new CompositePrincipal(
          new ServicePrincipal('lambda.amazonaws.com'),
          new ServicePrincipal('edgelambda.amazonaws.com')
        ),
      }),
      awsSdkConnectionReuse: false,
    })
Enter fullscreen mode Exit fullscreen mode

Reason

At July 2020, Lambda@Edge doesn't support Environment Values.

But aws-cdk v1.53.0 update contains default true of AWS_NODEJS_CONNECTION_REUSE_ENABLED.

https://github.com/aws/aws-cdk/commit/44c040072362456ebdd15799076a67bbb74e6668

      // Enable connection reuse for aws-sdk
      if (props.awsSdkConnectionReuse ?? true) {
        this.addEnvironment('AWS_NODEJS_CONNECTION_REUSE_ENABLED', '1');
      }
Enter fullscreen mode Exit fullscreen mode

So you must set false the prop.

Discussion (0)

Forem Open with the Forem app