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

Lambda@Edge not logging on cloudfront request


As explained in the Docs , I set up Lambda@edge for cloudfront trigger of Viewer Response.

The lambda function code :

'use strict';

exports.handler = (event, context, callback) => {
    console.log('----EXECUTED------');

    const response = event.Records[0].cf.response;      
    console.log(event.Records[0].cf_response);

    callback(null, response);
};

I have set up trigger appropriately for the Viewer Response event.

Now when I make a request through cloudfront, it must be logged in cloudwatch, but it doesn't.
If I do a simple Test Lambda Function (using Button), it is logged properly.

What might be the issue here ?


Solution

  • When you deploy Lambda@Edge function, It is deployed to all edge cache regions across the world with their version Replica of the Lambda Edge function. Regional edge caches are a subset of the main AWS regions and edge locations.

    When a user requests to the nearest pop/edge, the lambda associated with the edge cache region will get called. All logs of Lambda associated with those regions will in their edge cache region CloudWatch logs.

    For example:

    If a user is hitting us-east-1 region then its associated logs will be in us-east-1.

    To know exactly where (on which region) your function is logging, you can run this AWS CLI script:

    FUNCTION_NAME=function_name_without_qualifiers
    for region in $(aws --output text  ec2 describe-regions | cut -f 3) 
    do
        for loggroup in $(aws --output text  logs describe-log-groups --log-group-name "/aws/lambda/us-east-1.$FUNCTION_NAME" --region $region --query 'logGroups[].logGroupName')
        do
            echo $region $loggroup
        done
    done
    

    on which you have to replace "function_name_without_qualifiers" with the name of your lambda@edge. Link

    Hope it helps.