Cloudfront 函数总是 returns 503
Cloudfront Function always returns 503
这是一个已知问题,但我想知道的是,如何在 CDK 中设置 lambda。我使用了下面的解决方案,但是当我访问该站点时,我收到了 503 响应
The CloudFront function returned an invalid value: response.statusCode is missing
在 AWS 控制台中测试成功,为什么它不能在托管网站上运行?
重定向处理程序
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;
}
云端设置
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
}]
]}
来自restrictions page of Lambda@Edge
The Lambda function must be in the US East (N. Virginia) Region.
你的代码仍然会在离用户最近的 Edge 位置执行,但函数本身必须位于 us-east-1。
根据您的用例(这似乎是一个简单的 url 重定向),您可能需要考虑使用更新的 CloudFront Functions 功能,它更快、更轻便。这个documentation page比较好table.
编辑:
我以前没有使用过 CloudFront 功能,但查看 CDK 文档和您的 link,我可以提出一些更改建议。
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: myFunction,
eventType: FunctionEventType.VIEWER_REQUEST
}],
isDefaultBehavior: true
}]
]}
- Function Association interface only supports the L2 construct Function and not CfnFunction.
- 您 link 编辑的 GitHub sample 提到了查看者请求。
这是一个已知问题,但我想知道的是,如何在 CDK 中设置 lambda。我使用了下面的解决方案,但是当我访问该站点时,我收到了 503 响应
The CloudFront function returned an invalid value: response.statusCode is missing
在 AWS 控制台中测试成功,为什么它不能在托管网站上运行?
重定向处理程序
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;
}
云端设置
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
}]
]}
来自restrictions page of Lambda@Edge
The Lambda function must be in the US East (N. Virginia) Region.
你的代码仍然会在离用户最近的 Edge 位置执行,但函数本身必须位于 us-east-1。
根据您的用例(这似乎是一个简单的 url 重定向),您可能需要考虑使用更新的 CloudFront Functions 功能,它更快、更轻便。这个documentation page比较好table.
编辑:
我以前没有使用过 CloudFront 功能,但查看 CDK 文档和您的 link,我可以提出一些更改建议。
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: myFunction,
eventType: FunctionEventType.VIEWER_REQUEST
}],
isDefaultBehavior: true
}]
]}
- Function Association interface only supports the L2 construct Function and not CfnFunction.
- 您 link 编辑的 GitHub sample 提到了查看者请求。