aws-cdk v1.53.0 or above update causes 400 error when deploying lambda@edge.
With message "The function cannot have environment variables".
My proposal is add new NodejsFunction props to such as disableAdditionalEnv.
I want to deploy lambda@edge code by following cdk code.
new NodejsFunction(this, 'some-awesome-function', {
role: new Role(this, 'AllowLambdaServiceToAssumeRole', {
assumedBy: new CompositePrincipal(
new ServicePrincipal('lambda.amazonaws.com'),
new ServicePrincipal('edgelambda.amazonaws.com')
),
}),
})
and when I executed cdk deploy and I got an error:
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;
The error reason is following commit in aws-cdk v1.53.0.
And, environment variables cannot be added to lambda@edge as of July 2020.
https://github.com/aws/aws-cdk/commit/44c040072362456ebdd15799076a67bbb74e6668
if (props.awsSdkConnectionReuse ?? true) {
this.addEnvironment('AWS_NODEJS_CONNECTION_REUSE_ENABLED', '1');
}
awsSdkConnectionReuse is default true, and so lambda@edge developers must set the props false following code:
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,
})
The solution is not intuitive for me. And aws-cdk will add props and add environment in the future. Then developers must be set these props false each.
falseThis method is the clearest. I would like you to adopt this policy if possible.
disableAdditionalEnv props and if the prop set true, addEnvironment methods will not work.This method requires additional development, but it makes development easier until environment variables can be added to Lambda@edge.
I think I can implement two choices, so I would like to hear your opinion and decide. Or I'd like to know if there is another clear solution.
This is a :rocket: Feature Request
I can confirm that the issue existed for me, as well, when upgrading. Adding awsSdkConnectionReuse: false to my lamdba@edge definition worked around the issue.
@jogold @nija-at would be nice to be able to identify that the function is deployed @edge, disallow environment variables during synth and not specify them in NodejsFunction in that case.
@jogold
Thanks to open Pull Request!!
The approach seems good because it automatically set environments empty. Developer experiences are better than my proposal.
Most helpful comment
@jogold @nija-at would be nice to be able to identify that the function is deployed @edge, disallow environment variables during synth and not specify them in
NodejsFunctionin that case.