question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

General Issue

Lambda@Edge not connected to Cloudfront Distribution as triggers

The Question

I’m using CDK (Python) to connect a NodeJs based Redirection lambda function to CloudFront Distributions in my stack.

CDK synth and deploy is succesfully creating the necessary resources.

But the addition/Integration of function as Lambda@Edge (Triggers -> Cloudfront) is not working out of the box.

Perhaps I’m missing the connection/binding for the same

redirection_func_lambda_edge = cloudfront.experimental.EdgeFunction(self, "Redirect-{domain_name}",
            runtime=lambda_.Runtime.NODEJS_14_X,
            handler="index.handler",
            code=lambda_.Code.from_asset(os.path.join(dirname, "../../middleware"))
        )

        distribution=cloudfront.Distribution(self, "PartnerS3Cloudfront-{domain_name}",
                    default_behavior=cloudfront.BehaviorOptions(origin=origins.S3Origin(website_bucket),
                        edge_lambdas=[cloudfront.EdgeLambda(
                            function_version=redirection_func_lambda_edge.current_version,
                            event_type=cloudfront.LambdaEdgeEventType.VIEWER_REQUEST)],
                    )
                    )

        redirection_func_lambda_edge.add_permission(
            "AllowEdgeLambda",
            principal=iam.ServicePrincipal("edgelambda.amazonaws.com"),
            action="lambda:GetFunction",
        )

I’ve also tried LambdaFunctionAssociation, function_associations but that does not work either

I have referred the Documentation link https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_cloudfront.experimental/EdgeFunction.html

Can you help me with the missing piece that will link the lambda function to cloudfront distribution (default origin behaviour)

Currently cdk deploy deploys the stack successfully, but the Domain connected to the corresponding stack gives a 503 Error

The request could not be satisfied.
The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.

Generated by cloudfront (CloudFront)
Request ID: qYEiIRrPMcE5Baj8per1c0h64QtjpxsKAo5jsbidFeticsaN_XOtXA==

CDK CLI Version

2.0.0 (build 4b6ce31)

Framework Version

No response

Node.js Version

14.x

OS

macOS Big Sur 11.6

Language

Python

Language Version

Python 3.9.7 NodeJS 14.x

Other information

https://dev.to/evnz/single-cloudfront-distribution-for-s3-web-app-and-api-gateway-15c3#lambda-edge-for-handling-redirects

https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_cloudfront.experimental/EdgeFunction.html

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
OK-Cactuscommented, Oct 25, 2022

While such binding works, but UI/ Trigger does not show up which can paint a false picture and cause head scratching and potentially waste a few hours figuring out unrelated stuff and ultimately finding the reason. Got to know from my colleague that this is not fixed yet, so please update us about the same.

1reaction
OK-Cactuscommented, Dec 7, 2021

This issue is resolved.

While the redirection Lambda function did not show CloudFront as trigger (As is the case if L@E is deployed via AWS console)

But the CloudFront Distribution’s Behaviour’s VIEWER_REQUEST displayed the latest ARN of the Lambda in question, Also L@E CloudFront logs are not necessarily in the region of said Lambda, but the nearest CloudFront Edge’s POP, which I missed initially.

On checking logs, I could see a LambdaExecutionError, which was promptly fixed on further analysis.

So while this may still be a UI/Presentation issue with CDK, the said snippets bind CloudFront and lambda correctly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lambda@Edge - Amazon AWS
Lambda @Edge is a feature of Amazon CloudFront that lets you run code closer to users of your application, which improves performance and...
Read more >
Lambda@Edge example functions - Amazon CloudFront
Lambda @Edge example functions ... See the following sections for examples of using Lambda functions with CloudFront. ... For Node.js functions, each function...
Read more >
What is Lamdba@Edge? Pricing, Examples, Benefits - StormIT
Lambda @Edge allows you to run code across Amazon CloudFront edge locations globally without provisioning or managing servers, responding to end-users at the ......
Read more >
Using Amazon Lambda with CloudFront Lambda@Edge
Lambda @Edge lets you run Node.js and Python Lambda functions to customize content that CloudFront delivers, executing the functions in Amazon locations ...
Read more >
What is Lambda@Edge - Everything you need to know
In this video, I will explain what is Lambda @ Edge and how you can introduce it in your projects. In the first...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found