Should I use CloudFront in front of API/Web just because I want them on a single domain?
I typically develop my applications with frontend single page application on S3 and backend APIs on a server typically elastic beanstalk (so ELB + EC2). I am confused over what is a good choice to put in front of my ELB & S3 - mainly CloudFront or API Gateway. My main need now is caching and performance (reduced load on servers). Of course costs will be bonus too. Traffic is often limited to a single region/country (Singapore) in my case.
So assuming I want to keep to a single domain, I think I will definately require CloudFront? So CloudFront will have 2 origins S3 and ELB. S3 origin can be configured with a long cache TTL and ELB a shorter one, just to handle spikes. Is this a good idea?
But what if some APIs, especially listing API should not be cached. If I set TTL to 0, it will not cache but my understanding is its slower? How best to handle these?
That said, when do I use API Gateway? Assuming I dont need all that SDK stuff and my current systems are still on ELB/EC2. It seems all it will be in my case, will be a proxy and caching system just like cloudfront?
Solution 1:
Given the choice between CloudFront and APi Gateway, CloudFront is the correct solution. API Gateway is not really suited for hosting an entire site, and there are complications related to large payloads and binary content.
Both can be used as reverse proxies but CloudFront is more straightforward for use cases beyond APIs and has unlimited cache storage.
it will not cache but my understanding is its slower
It's only "slower" in the sense that it isn't as fast as cached responses would be. But using CloudFront for content that isn't cacheable is valid and supported. It typically improves performance for all content because it optimizes the connection between browser and server... but this effect is most notable as distance increases... so it isn't clear what advantage there might be with your viewers typically in such close proximity as they should be, if they are also in Singapore.
Of course costs will be bonus too.
There's another detail that you need to understand, since you say your traffic will be limited to Singapore.
In Singapore:
- Traffic from EC2 or S3 to the Internet is $0.12/GB.
- Traffic from EC2 or S3 to CloudFront is $0.00/GB.
- Traffic from CloudFront to the Internet is $0.14/GB.
So you have a cost inversion, here. In some areas, using CloudFront costs the same or less than not using CloudFront (e.g. in us-east-2, using CloudFront actually costs $0.005/GB less than not using CloudFront). Singapore isn't one of those areas.
Given your desire to keep everything under a single domain, CloudFront still seems like a good option.