Search code examples
amazon-web-servicesamazon-cloudfrontaws-lambda-edge

lambda@edge logs and invocation counts are not showing?


I have created the cloud front distribution and attached the lambda with the trigger

`Event type: viewer-requestPath pattern: something/index.html`

 Event type: origin-requestPath pattern: something/index.html

When i hit the endpoint it's redirecting to the page where i want to redirect, according to my lambda.

but i was not able to see my lambda logs in any region.

it was not showing the invocation count also.

Did anyone faced this issue ??

Here is my lambda code

'use strict';

exports.handler = (event, context, callback) => {
    /*
     * Generate HTTP redirect response with 302 status code and Location header.
     */
    console.log('event',event);
    const response = {
        status: '302',
        statusDescription: 'Found',
        headers: {
            location: [{
                key: 'Location',
                value: 'http://<domainname>/something/root.html',
            }],
        },
    };
    callback(null, response);
};

Solution

  • Modify your function like this:

    const response = {
            status: '302',
            statusDescription: 'Found',
            headers: {
                location: [{
                    key: 'Location',
                    value: 'http://<domainname>/something/root.html',
                }],
                'x-lae-region': [ { key: 'x-lae-region', value: process.env.AWS_REGION } ],
            },
        };
    

    What this does is capture the region where your lambda function is running -- it will show us-east-1 during test, but show an accurate value once deployed.

    Responses captured by your browser, curl, etc., will now include x-lae-region: some-aws-region to indicate the region linked to the edge where your specific requests are being handled. Check the logs for that specific region -- you should see logs and invocations there.

    Also note that for an Origin Request (but not Viewer Request) trigger, CloudFront caches the response generated by Lambda, so the function will only be invoked when there is a cache miss. If CloudFront has cached the response, the trigger will not fire -- the cached response is served without contacting the origin. If you make this function live and you don't see the change in the responses, then you are almost certainly looking at cached responses, and will want to do an invalidation.