0

I have this an URL, let's assume, "www.sample.com/hello". Now I have triggered a lambda function on viewer request where I just need to change the url to "www.sample.com/hello2". I did it using lambda edge functions but it is throwing me an error.

This is the code I wrote in lamda

const path = require('path');



exports.handler = (event, context, callback) => {
        const cf = event.Records[0].cf;
    const request = cf.request;
    const response = cf.response;
    const statusCode = response.status;
    const path = request.uri;
    const afterpath = path.substring(path.indexOf("/")+1);

if (afterpath == 'sample') {
    request.uri = request.uri
        .replace(afterpath,'samplepathitis')
}

    return callback(null, request);
};

I am getting this error

503 ERROR
The request could not be satisfied.
The Lambda function associated with the CloudFront distribution is invalid 
or doesn't have the required permissions. 
If you received this error while trying to use an app or access a website, 
please contact the provider or website owner for assistance. 
If you provide content to customers through CloudFront, you can find steps 
to troubleshoot and help prevent this error by following steps in the 
CloudFront documentation 
(http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/http- 
503-service-unavailable.html). 
Generated by cloudfront (CloudFront)
Request ID: MsN6aG8qvI9ttt3_VLhQAqpY8kF2pHk3V095lAFVU_sWmDvF3IfqAA==
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

As the error message states it clearly : either the function is invalid or there is no permission to call the function.

To check if the function is valid : try to invoke it from the Lamba console. Use the Test button. You will need to pass a request as input. The console will propose you sample request that you can adjust to simulate your use case.

Also very in the doc the return value of the function. Is request the correct return value expected by Cloudfront ?

Once you are sure about the two above, verify the permission to invoke that function. What is the trigger ? Is Cloudfront authorized to invoke your function ?

Sébastien Stormacq
  • 14,301
  • 5
  • 41
  • 64
  • Unfortunately, this error message is one that cannot always be taken at face value. It is thrown in some cases completely unrelated to either the function being "invalid" (whatever exactly that might mean; perhaps it isn't intended to mean what we assume) or the function not having the right permissions. I'll look for an example. – Michael - sqlbot Aug 03 '19 at 17:04
  • 1
    Also, `return callback(null, request);` is correct for a viewer-request trigger that isn't trying stop CloudFront processing and emit a spontaneous response. Here, `request` is either the original or a (correctly) modified version of `event.Records[0].cf.request`. – Michael - sqlbot Aug 03 '19 at 17:04