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:
- Our backend generates the images on the EC2 instance.
- We upload them to S3, as soon as we can, but it's not immediate.
- 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
Best Answer
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.