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

[lambda] Identify lambda@edge does not support environment variables #9328

Closed
2 tasks done
TeXmeijin opened this issue Jul 29, 2020 · 4 comments · Fixed by #9562
Closed
2 tasks done

[lambda] Identify lambda@edge does not support environment variables #9328

TeXmeijin opened this issue Jul 29, 2020 · 4 comments · Fixed by #9562
Assignees
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/medium Medium work item – several days of effort in-progress This issue is being actively worked on. p2

Comments

@TeXmeijin
Copy link

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.

Use Case

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.

44c0400

      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.

Proposed Solution

  1. set awsSdkConnectionReuse default value to false

This method is the clearest. I would like you to adopt this policy if possible.

  1. set 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.

Other

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.

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

@TeXmeijin TeXmeijin added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jul 29, 2020
@blimmer
Copy link
Contributor

blimmer commented Aug 3, 2020

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.

@eladb
Copy link
Contributor

eladb commented Aug 4, 2020

@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.

@eladb eladb changed the title [lambda-nodejs] Deploying lambda@edge causes 400 error with "The function cannot have environment variables" [lambda] Identify lambda@edge does not support environment variables Aug 4, 2020
@eladb eladb assigned nija-at and unassigned eladb Aug 4, 2020
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Aug 4, 2020
@nija-at nija-at added bug This issue is a bug. effort/medium Medium work item – several days of effort p2 and removed @aws-cdk/aws-lambda Related to AWS Lambda feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Aug 4, 2020
jogold added a commit to jogold/aws-cdk that referenced this issue Aug 10, 2020
…at edge

Automatically remove environment variables of a Lambda function when
it's used at edge. A warning is also shown.

Closes aws#9328
Closes aws#9453
jogold added a commit to jogold/aws-cdk that referenced this issue Aug 10, 2020
…at edge

Automatically remove environment variables of a Lambda function when
it's used at edge. A warning is also shown.

Works with singleton functions.

Closes aws#9328
Closes aws#9453
jogold added a commit to jogold/aws-cdk that referenced this issue Aug 10, 2020
…at edge

Automatically remove environment variables of a Lambda function when
it's used at edge. A warning is also shown.

Works with singleton functions.

Closes aws#9328
Closes aws#9453
@SomayaB SomayaB added the in-progress This issue is being actively worked on. label Aug 10, 2020
@TeXmeijin
Copy link
Author

@jogold
Thanks to open Pull Request!!
The approach seems good because it automatically set environments empty. Developer experiences are better than my proposal.

@mergify mergify bot closed this as completed in #9562 Aug 19, 2020
mergify bot pushed a commit that referenced this issue Aug 19, 2020
…@edge (#9562)

Check version and function compatibility when a Lambda is used for
Lambda@Edge. Environment variables can be marked as "removable" when
used for Lambda@Edge.

Closes #9328
Closes #9453


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
misterjoshua pushed a commit to misterjoshua/aws-cdk that referenced this issue Aug 19, 2020
…@edge (aws#9562)

Check version and function compatibility when a Lambda is used for
Lambda@Edge. Environment variables can be marked as "removable" when
used for Lambda@Edge.

Closes aws#9328
Closes aws#9453


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@ismail-icanovic
Copy link

Hello. Can you pleas link me to documentation on how to specify the NodejsFunction in CDK v2 for Lambda@Edge since this PR is closed and I couldn't find anything here documented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/medium Medium work item – several days of effort in-progress This issue is being actively worked on. p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants