Vishnu Ranganathan
Vishnu Ranganathan

Reputation: 1355

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 ??

Upvotes: 8

Views: 12085

Answers (2)

Vishnu Ranganathan
Vishnu Ranganathan

Reputation: 1355

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 } ],
        },
    };

Upvotes: 3

Noah Zoschke
Noah Zoschke

Reputation: 621

There are some common "gotchas" to Lambda@Edge and CloudFront. You need to:

  • Publish a new version of you Lambda function
  • Update the CloudFront Lambda association to your new version, e.g. arn:aws:lambda:us-east-1:572007530218:function:gofaas-WebAuthFunction:45
  • Look for Lambda@Edge logs in the region of the requestor

This is different from "normal" Lambda web console flow of saving a code change and jumping to logs from the monitoring tab.

Upvotes: 19

Related Questions