Lambda@Edge fallback to custom origin from s3 with cloudfront without hardcoded domains

Viewed 277

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?

0 Answers
Related