5

We're trying to set up a Cloudfront distribution where the default origin is S3, but if the content is not found on S3, we want it to fetch it from an EC2 instance.

Can we setup these multiple origins: S3 and EC2 and two behaviors, both defaulting to (*) and have it fail over to the 2nd origin (EC2), when needed?

The process is as follows:

  1. Our backend generates the images on the EC2 instance.
  2. We upload them to S3, as soon as we can, but it's not immediate.
  3. When a user tries to access cdn.example.com/images/picture.jpg it directs Cloudfront to our S3 bucket, and if the image is not there, yet, we want to redirect that request to the EC2 server.

We're fine with a temporary redirect solutions here, until it's updated on S3.

S3 redirects are not the answer, as S3 redirects seem to work just for existing objects or entire buckets, not missing objects.

How can we achieve that?

Thanks, Adoram

1

CloudFront does not support your use case. You'd need to handle this in the application - don't serve CDN URLs for an item until your system has finished the transfer to S3.

| improve this answer | |
0

With CloudFront’s Origin Failover capability, you can setup two origins for your distributions - primary and secondary, such that your content is served from your secondary origin if CloudFront detects that your primary origin is unavailable. CloudFront already allows you to configure custom error pages or generate redirects with Lambda@Edge if your origin is unavailable. Now with Origin Failover, you can easily setup failover logic between combinations of AWS origins or non-AWS custom HTTP origins such that there is minimal interruption to your viewer’s experience. For example, you can have two Amazon S3 buckets that serve as your origin, that you independently upload your content to. If an object that CloudFront requests from your primary bucket is not present or if connection to your primary bucket times-out, CloudFront will request the object from your secondary bucket. So, you can configure CloudFront to trigger a failover in response to either HTTP 4xx or 5xx status codes.

Source : https://aws.amazon.com/about-aws/whats-new/2018/11/amazon-cloudfront-announces-support-for-origin-failover/

Documentation : https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/high_availability_origin_failover.html

Add your both origins in Cloudfront configuration.

Add your failover group rules in 'Origin Groups' section.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.