来自Lambda@Edge 的限制页面 https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/edge-functions-restrictions.html#lambda-at-edge-function-restrictions
Lambda 函数必须位于美国东部(弗吉尼亚北部)区域。
您的代码仍将在距离用户最近的 Edge 位置执行,但函数本身必须位于 us-east-1 中。
根据您的用例(这似乎是一个简单的 url 重定向),您可能需要考虑使用更新的 CloudFront Functions 功能,该功能更快、更轻量。这文档页 https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/edge-functions.html有一个很好的比较表。
Edit:
我以前没有使用过 CloudFront 函数,但是查看 CDK 文档和您的链接,我可以建议一些更改。
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
}]
]}
- The 函数关联 https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.FunctionAssociation.html接口仅支持 L2 结构Function https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.Function.html并不是Cfn函数 https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_cloudfront.CfnFunction.html.
- The GitHub 示例 https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/url-rewrite-single-page-apps您链接提到了查看者请求。