This is a known issue but what I want to know is, how to set up the lambda in CDK. I have used the solution below but when I access the site I get a 503 response
The CloudFront function returned an invalid value: response.statusCode is missing
Testing this in the AWS console is successful so why wouldn't it work on the hosted site?
redirect handler
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;
}
cloudfront setup
myFunction = new Function(this, 'ViewerResponseFunction', {
functionName: 'RedirectURIFunction',
code: FunctionCode.fromFile({filePath: myFilePath}).render(),
comment: "Comment about the function"
});
originConfigs: [
{
s3OriginSource: {
s3BucketSource: myBucket,
originAccessIdentity: myOAI,
},
behaviors: [{
functionAssociations: [{
function: myCfnFunction,
eventType: FunctionEventType.VIEWER_RESPONSE
}],
isDefaultBehavior: true
}]
]}