Cloudfront Edge functions

0

I'm trying to play Instagram Video assets. The challenge is the videos are expirable. They expire every N mins.

I'm brainstorming a solution where I set up my CDN (Cloudfront) which forwards the incoming requests to the original server (Instagram in this case), caches the video at CDN, and then keeps serving it without the need to request Instagram again. I don't want to download the videos and keep them in my bucket.

I'd a look at CloudFront functions and was able to redirect the incoming requests to another URL, basis on some conditions. Following is the code.

function handler(event) {
    var request = event.request;
    var headers = request.headers;
    
    if request.uri == '/assets/1.jpg'{
        var newurl = 'https://instagram.com/media/1.jpg'
      
        var response = {
            statusCode: 302,
            statusDescription: 'Found',
            headers:
                { "location": { "value": newurl } }
        }

        return response;
     }
   return request
}

However, this redirects it to the newURL. What I'm looking for is not a redirect, but the following

  1. when the request is made to my server CDN, ie mydomain.com/assets/1.jpg, the file 1.jpg should be served from the Instagram server, whose value is the newURL in the above code snippet. This should be done without changing my domain URL (in the address bar) to Instagram.

  2. The following requests to mydomain.com/assets/1.jpg should be directly served from the cache, and should not be routed again to Instagram.

Any help in this regard is highly appreciated.

Praful
asked 2 years ago250 views
1 Answer
0

Hello,

I understand you are trying to create a CloudFront distribution that directs traffic to an origin which is pointed to instagram.com, then caches instagram.com videos at the Content Delivery Network, to eliminate the necessity to send traffic to instagram.com again. I used the information you provided in the post to recreate the environment on my console. From what I am able to see, I am unable to use instagram.com as the origin for the CloudFront Distribution due to the origin’s privacy configurations. When I open the CloudFront distribution domain name, I am getting an error that says “Page couldn’t load” which implies that instagram.com cannot be used as an origin for a CF distribution.

A potential solution that can be used in this scenario is to utilize S3 and CloudFront to host the videos. Unfortunately, since CF cannot direct to instagram.com, the only way to access the videos through CloudFront is to download them and upload to an S3 Bucket. After uploading the videos, create a CloudFront Origin Access Identity to make sure viewers cannot bypass CloudFront to directly access the videos from S3. A CloudFront Distribution must be created afterward to serve, distribute, and cache the videos.

I hope this solution helped!

Documentation: Screenshot: Instagram Error via CloudFront Distribution: https://imgur.com/a/jHtJRB9 Tutorial: Hosting on-demand streaming video with Amazon S3, Amazon CloudFront, and Amazon Route 53: https://docs.aws.amazon.com/AmazonS3/latest/userguide/tutorial-s3-cloudfront-route53-video-streaming.html

sachin
answered 8 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions