Caleb Pitman
Caleb Pitman

Reputation: 1165

Cloudfront not using lambda function

I'm trying to set a custom cloudfront response header. I created a lambda function with nodejs 12.x, added the trigger CloudFront, distribution using the correct ID, cache as *, event as Viewer response, then deploy.

Then I went to CloudFront > Distributions > IDxxx > Behaviors > Edit > * It shows the lambda function listed as Viewer Response.

When I access any from that cloudfront distribution (cache disabled locally/cloudfront miss as well), the header is never sent in the response. In CloudWatch, I see logs from the "Test" button, but none from production, so I assume that the function isn't being executed on CloudWatch, despite it being in the behaviors.

'use strict';

exports.handler = (event, context, callback) => {
    try { 
        console.log("REQUEST");
        const response = event.Records[0].cf.response;
        const headers = response.headers;
         
        response.headers['test'] = [{key: 'test', value: 'test'}];
         
        callback(null, response);
    } catch(err) {
        console.log("ERROR");
        console.log(err);
        
    }
};

Edit: Also I did use Publish new version. I assume that once it's published, cloudfront should be automatically using it?

Upvotes: 0

Views: 622

Answers (1)

Caleb Pitman
Caleb Pitman

Reputation: 1165

After changing a lambda function, it must be published, and then deployed again.

In CloudFront, it shows the lambda function string like: arn:aws:lambda:us-east-1:000000000000:function:testfunction:3

The end "3" is the version number. If you published 3 versions, you'll need it to show 3. Mine was still set to 1.

Upvotes: 1

Related Questions