Lambda@Edge
See original GitHub issueGeneral 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
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (1 by maintainers)
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.
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.