Lambda@Edge 从 s3 回退到带有 cloudfront 的自定义源,无需硬编码域

发布于 2025-01-09 01:38:28 字数 1368 浏览 0 评论 0原文

我有带有源组 [s3,自定义] 的 CloudFront 分发,

我希望从 s3 提供内容,但当给定某个标头时 - 回退到自定义源。 我有一个在源请求上运行的 lambda@edge 函数,并且我已经检查了标头 - 现在我需要将请求重定向到自定义源。

问题:

  1. 据我了解,Lambd@edge 应该使用后备原点运行两次,如此处所述 https://docs.aws.amazon.com/AmazonCloudFront/latest/ DeveloperGuide/high_availability_origin_failover.html 因此,如果源是 s3 并从 CloudFront 回退到自定义源,我想返回 {status: 404} - 我应该怎么做? event.origin 是否包含一次 s3 和一次自定义?或两者兼而有之?

当我返回 404 时,用户会收到 404 响应,并且不会执行回退。

if (!request.origin.s3) {
  callback(null, { status: '404' });
  return;
}
  1. 为了执行回退,我按照此处所述覆盖原点 https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-examples.html#lambda-examples-content-based-custom-origin-request-trigger
request.origin = {
  custom: {
  domainName: 'www.example.com',
  port: 443,
  protocol: 'https',
  path: '',
  sslProtocols: ['TLSv1'],
  readTimeout: 10,
  keepaliveTimeout: 5,
  customHeaders: {},
  },
};

我可以获取域名以某种方式来自 CloudFront 分配?

I have CloudFront distribution with origin group [s3, custom]

I want to have the content served from s3, but when given a certain header - fallback to the custom origin.
I have a lambda@edge function that runs on origin request and I've checked the headers - now I need to redirect the request to the custom origin.

Questions:

  1. As I understand, the Lambd@edge should run twice with a fallback origin as described here
    https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/high_availability_origin_failover.html
    So I want to return {status: 404} if the origin is s3 and fallback from CloudFront to the custom origin - how should I do that? does event.origin contain once s3 and once custom? or both?

When I return 404, the user gets a 404 response and no fallback is performed.

if (!request.origin.s3) {
  callback(null, { status: '404' });
  return;
}
  1. To perform the fallback, I override the origin as described here
    https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-examples.html#lambda-examples-content-based-custom-origin-request-trigger
request.origin = {
  custom: {
  domainName: 'www.example.com',
  port: 443,
  protocol: 'https',
  path: '',
  sslProtocols: ['TLSv1'],
  readTimeout: 10,
  keepaliveTimeout: 5,
  customHeaders: {},
  },
};

Can I get the domain name somehow from the CloudFront distribution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文