Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic content from cloudfront - can I put API results (JSON) from Rails?

I see that CloudFront supports dynamic content.

Could anyone guide how I could go about doing this in Rails? For instance, I have, api.test.com/popular.json which I would like to be served from CloudFront preferably, but probably update itself every few hours.

For static assets, I have been able to do it using Rails asset_host setting. But I have not been able to find enough guides on dynamic content. Any pointers would be useful.

like image 300
ddb Avatar asked Oct 31 '12 09:10

ddb


People also ask

Can CloudFront distribute dynamic content?

If you are serving dynamic content such as web applications or APIs directly from an Amazon Elastic Load Balancer (ELB) or Amazon EC2 instances to end users on the internet, you can improve the performance, availability, and security of your content by using Amazon CloudFront as your content delivery network.

What is difference between global accelerator and CloudFront?

CloudFront uses Edge Locations to cache content while Global Accelerator uses Edge Locations to find an optimal pathway to the nearest regional endpoint. CloudFront is designed to handle HTTP protocol meanwhile Global Accelerator is best used for both HTTP and non-HTTP protocols such as TCP and UDP.

Can CloudFront cache POST requests?

CloudFront always caches responses to GET and HEAD requests. You can also configure CloudFront to cache responses to OPTIONS requests. CloudFront does not cache responses to requests that use the other methods.


1 Answers

You don't need to integrate your rails app with CloudFront. Instead, you can set up CloudFront to cache your website:

  1. Go to AWS Console and add a new CloudFront distribution
    • web delivery method
    • origin domain name: myexistingdomain.com

You can leave all defaults as they are. Minimum/Default/Maximum TTL options can be set to enforce a certain cache time, and might be necessary depending on your server settings.

  1. at myexistingdomain.com, you must set Access-Control-Allow-Originheader to include the domain name of your Cloudfront distribution.

As you probably already have experienced, most updates on CloudFront, or adding an origin usually takes ~20 minutes, so be patient.

Also, if you need to use query parameters, cookies or special headers at your rails app, you'll have to include those in cache settings at your CloudFront distribution. Note that this will reduce the efficiency of the distribution.

like image 156
tomfa Avatar answered Sep 28 '22 09:09

tomfa