Search code examples
amazon-web-servicesaws-lambdaamazon-cloudfrontaws-lambda-edge

In my Lambda@Edge origin request function, the "event" parameter is empty


I have a Cloudfront distribution with a default behavior and the following function association:

origin request: Lambda@Edge / arn:aws:lambda:us-east-1:...:function:*functionName*:4

I can see the CloudFront trigger in the triggers tab in my lambda function version 4:

Event type: origin-request
Include Body: Yes
Path pattern: *
Service principal: replicator.lambda.amazonaws.com
Statement ID: replicator.lambda.GetFunction

My function's code looks like this:

'use strict';


exports.handler = (event, context, callback) => {
    console.log("context: ", context);
    console.log("event: ", event);

I made a GET request to my Cloudfront distribution using the Distribution domain name and this is what I found in the logs using the AWS CLI:

2023-04-18T20:50:37.021Z    e52078b4-2efa-40a8-9a28-51ed50ac0132    INFO    event:  {}

How can the event be empty here?


Solution

  • This is a two-fold answer.

    1. I was reading the logs in the wrong region (us-east-1 while my execution region was us-east-2), and the empty event log did not come from a proper execution of my lambda@edge function (I am not sure where they came from, maybe the initialization of the function).
    2. I hadn't given the Lambda function the proper permissions to write and create logs in CloudWatch, and thus none were being produced. After adding those permissions and re-triggering my function, I was able to see the correct logs in us-east-2 and the event argument was properly passed.

    To allow the lambda function to create/write logs, I had to add the AWSLambdaEdgeExecutionRole role to it (under lambda/configuration/permissions/execution role -> edit), which should look like this:

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "logs:CreateLogGroup",
                    "logs:CreateLogStream",
                    "logs:PutLogEvents"
                ],
                "Resource": [
                    "arn:aws:logs:*:*:*"
                ]
            }
        ]
    }