Search code examples
amazon-cloudfrontaws-lambda-edge

lambda@edge not getting triggered with cloudfront


I am using lambda@edge to redirect my sites with cloudfront.

I have attached my versioned lambda arn to my cloud front cache behavior to all 4 events it has.

when i hit my cloudfront endpoint it says

502 ERROR
The request could not be satisfied.
The Lambda function returned invalid json: The json output must be an object type. 

when i check my lambda logs / invocation metrics i dont see any hits at all .

what may be the reason behind this ?

i tried my best to find the fix why my lambda is not getting triggered ??


Solution

  • i missed adding region under the header in my lambda code.

    since lambda@edge runs in the edge location we need to mention the region dynamically so that it knows where to write the logs when its running in the nearest edge location.

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