0
votes

I have a Cloudfront distribution pointing at a custom url. In that distribution, I have setup 2 lambda function associations. 1 on the Cloudfront event of viewer-request to query parameter store and redirect to the correct url The other on the Cloudfront event of origin-response to gather response stats from the call.

The viewer-reqest function is as follows - note dummy urls for purpose of question

exports.handler = async (event, context) => {
    const origin = "main";
    const primaryUrl = "https://www.google.com";
    const drUrl = "https://www.yahoo.com";

    let url = primaryUrl;

    if (origin != "main") {
        url = drUrl;
    } else {
        url = primaryUrl;
    }

    const response = {
        status: '302',
        statusDescription: 'Found',
        headers: {
            location: [{
                key: 'Location',
                value: url,
            }],
        },
    };

    return response;
};

The origin response lambda is very simple for now var AWS = require('aws-sdk'); AWS.config.update({ region: 'us-east-1' }); var cloudwatch = new AWS.CloudWatch();

exports.handler = async (event, context) => {
    console.log("Custom Metrics Event");
    console.log(JSON.stringify(event));

    const response = {
        status: '200', 
    };

    return response;
};

When I call the cloudfront distribution domain, the viewer-request lambda@edge kicks in but the origin-response lambda does not trigger and I see nothing in cloudwatch If I remove the viewer-request lambda - the origin-response lambda does trigger

Am I doing anything wrong here or does anyone have any recommendations?

Thank you Damien

1
If you remove the redirect part of the viewer-request Lambda does it work?Chris Williams
let me try that nowDamien
@chriswilliams no joy when I try thatDamien
The best examples I have are docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/… and docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/…. Request events return request, response events return response.Chris Williams
I'm not 100% on that one, I generally see the redirects normally occur on the origin response.Chris Williams

1 Answers

0
votes

@ChrisWilliams comment was accurate I changed my code to perform the redirect in the origin response and all worked fine then