1

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

7
  • If you remove the redirect part of the viewer-request Lambda does it work? Commented Aug 24, 2020 at 11:20
  • let me try that now Commented Aug 24, 2020 at 12:03
  • @chriswilliams no joy when I try that Commented Aug 24, 2020 at 14:36
  • 2
    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. Commented Aug 25, 2020 at 20:46
  • 1
    I'm not 100% on that one, I generally see the redirects normally occur on the origin response. Commented Aug 26, 2020 at 8:40

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.