Score:1

CloudFront does not seem to invoke lambda@edge function

in flag

I have a CloudFront distribution. The origin is an S3 bucket that uses OAI.

I have created a lambda@edge function following directions from https://aws.amazon.com/blogs/compute/implementing-default-directory-indexes-in-amazon-s3-backed-amazon-cloudfront-origins-using-lambdaedge/

Basically, I want the lambda@edge function to redirect URLs ending in / to /index.html. Acting like Apache DirectroyIndex.

The CloudFront distribution works for URLs without the redirect requirement. But CloudFront does not seem to invoke my lamba@edge function.

I have ensured, there is a correct association between the CloudFront distribution and the labda@edge function version.

I made several test requests:

curl -I https://www.sudheer.net/blog/
HTTP/2 403 
content-type: application/xml
date: Sat, 19 Feb 2022 14:35:38 GMT
server: AmazonS3
x-cache: Error from cloudfront
via: 1.1 5d840d432727e3561fd1a3de915212ca.cloudfront.net (CloudFront)
x-amz-cf-pop: EWR53-C2
x-amz-cf-id: leub-Kgu4Bh9xH4Rn5o7bxs62B1NBO4ViEu6hv-_xtGG7DSQlBFEXw=

I get 403. I did not find any lambda@edge logs in any region.

What could be the issue? How do I go about finding it?

The Lambda@Edge function has the principals:

    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "Service": [
                    "edgelambda.amazonaws.com",
                    "lambda.amazonaws.com"
                ]
            },
            "Action": "sts:AssumeRole"
        }
    ]
}

Score:0
cn flag

You can actually do this with CloudFront Functions. Shameless self-promotion, but I encountered exactly this issue and wrote about it on my blog.

Effectively:

I found this SO post, which showed me I needed to create this function:

function handler(event) {
    var request = event.request;
    var uri = request.uri;
    
    // Check whether the URI is missing a file name.
    if (uri.endsWith('/')) {
        request.uri += 'index.html';
    } 
    // Check whether the URI is missing a file extension.
    else if (!uri.includes('.')) {
        request.uri += '/index.html';
    }

    return request;
}

Use the Functions item on the left-hand menu in the CloudFront service to save + publish this function, then edit the default behaviour of your distribution, and changed the Viewer request function association to your new function.

Sudheer Satyanarayana avatar
in flag
Thank you @shearn89. Actually, my issue was incorrect order of cache behaviours. But I did simplify the setup, thanks to CloudFront functions. I ditched Lambda@Edge in favor of simpler solution.
cn flag
Feel free to share your solution and accept it for upvotes!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.