amazon-cloudfront - Cloudfront 函数始终返回 503

标签 amazon-cloudfront aws-cdk

How do you set a default root object for subdirectories for a statically hosted website on Cloudfront?

这是一个已知问题,但我想知道的是,如何在 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有一个很好的比较表。

编辑:

我以前没有使用过 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
    }]
]}

关于amazon-cloudfront - Cloudfront 函数始终返回 503,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70717168/

相关文章:

java - AWS CDK : Is there a way to create database schema using CDK?

aws-cloudformation - CDK 创建的同级堆栈中的竞争条件导致 "No export named X found"

amazon-web-services - AWS 不允许我访问静态网站的子页面

amazon-cloudfront - 固定政策和自定义政策有什么区别?

c# - ASP.Net Combres 和 Combres MVC with Cloudfront

amazon-s3 - Cloudfront 和 Lambda@Edge - 根据用户代理从自定义来源获取

aws-cdk - 如何使用 #AWS-CDK 从另一个堆栈导入安全组?

amazon-web-services - 无法通过代码管道对代码管道操作 AWS CDK 承担角色

aws-lambda - 能够使用 WAF 将 AWS Lambda 函数列入白名单或在 VPC 中运行 Lambda

c# - 在 AWS CDK 中使用常量 JSON 参数创建规则 (C#)