difficulty with CORS on an "alternate" domain name - amazon-web-services

I have an Amazon CloudFront setup that points to an S3 bucket as a CDN. I also have an alternate domain name (not on Route53) that points to this CloudFront.
I kept having trouble getting scripts to pull through the CDN when using the alternate domain name - but if I use the native one from the CloudFront control panel, it works.
Is there something special I need to do other than just set the domain name CNAME to point to the amazon CloudFront address for CORS to work?

An important part of correctly caching web requests is to ensure that a response to be served from the cache is "correct," in the sense if whether it will match the response generated by the origin, for the same request.
This isn't as simple as it sounds, since responses can vary based on the content if certain request headers.
CloudFront adopts a conservative and safe approach, by stripping most request headers as it forwards requests to the origin server -- if the server can't see the header, it can't use the header to vary its response.
In the case of CORS, it's critical for the origin server to see the Origin:, Access-Control-Request-Headers:, and Access-Control-Request-Method: headers so it can respond accordingly.
But forwarding unnecessary headers to the origin server causes inefficient caching, since the cached response will only be served against identical future requests, identical including the forwarded headers.
So the three CORS request headers must be "whitelisted" in the CloudFront cache behavior, so that they will be forwarded to the origin server (in this case, S3).
http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html#header-caching-web-cors

Related

Cloudfront path as a proxy

I'm new to AWS and setting up a Cloudfront distribution. From what I understand Cloudfront is designed to be used as a CDN.
I have a single-page-app that requires to communicate with the api from the same domain under /api/graphql path pointing to a GQL server that is not hosted in AWS.
My question is is there a way to bypass the cloudfront cache for /api* and proxy to the server?
So far I tried to create a new custom origin in the same distribution setup a cache behavior for the /api* path to point to the custom origin, but it seems the viewer request headers are not sent to the origin server and things don't work properly.
You can add/attach a cache policy for /api*.
See here: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/working-with-policies.html
This way, you can disable cache for Cloudfront and forward all headers to your origin.

Set both https and http from one origin in CloudFront

I'm using cloudfront to redirect a web app to a S3 bucket for some media content. As we trying to add HTTPS to our test environment, we wanted to add the https://app.foo.com origin to our cloudfront distribution. We've tried two different ways:
On the Create Origin tab, creating the second origin with the https
By editing the first working origin (in http) and adding a second origin header beneath the first one (in the origin settings tab).
None of these solutions seems to works, the app with http origin can access the bucket content. But the redirection with https does not work. I must precise that our authorizations on the bucket are ok, we can access the bucket content with the CloundFront link, and the CORS rules accept both http and https for the app. It looks like the https origin is not processed by ClondFront.
Thanks in advance
You need to understand what CloudFront is. It isn't "redirecting" users to an S3 bucket like you state in your question. It is loading, and caching the contents of the S3 bucket, and serving it to the user on request. An origin isn't a location for CloudFront to redirect users to. An Origin is a location for CloudFront to load resources from. In the context of http vs. https connections and CloudFront, you have the following decisions to make:
Will CloudFront communicate with the origin via http or https. This decision will not affect your users ability to load http or https resources in any way.
Will CloudFront serve both http and https content to your users, or will it redirect all http requests to https. This decision is not impacted by the origin configuration in any way.
The user's web browser is making an HTTP connection to a CloudFront server, and receiving the response from CloudFront. The user's web browser is never making a connection directly to S3.
You can't have two origins that only differ by http/https protocol. Both of those origins would be at the same path, and contain the same content. CloudFront only wants one of those origins, which it will connect to as needed to populate its cache.

Express serving static files vs AWS S3

are there any benefits to running a static website via S3/CloudFront vs running one via Express (via Fargate) sitting behind API Gateway? In terms of performance or any other considerations? The latter approach allowing api endpoints to also be hosted via Express/API Gateway on the same domain.
Scaling in case of Cloudfront/S3 is better.
Caching and serving from edge locations in Cloudfront S3.
Cloudfront gives you options to alter req and response, say you want to add CSP headers to all response going out from your domain or say you want to prevent open redirect from you domain URL, you can use origin request and origin response lambdas in cloudfront.

Cloudfront X-Cache shows "Miss from cloudfront" all the time

I have a cloudfront web distribution setup for an API Gateway proxy. The API is IAM enabled mock call. I also created a customer name which has the same domain name as the cname I used to create cloudfront distribution. I set cloudfront up to pass Authorization and Host only for headers. And set Query String forwarding and Forward Cookie to None.
When I hit the cloud front using Postman, I kept on getting Miss from cloudfront from the response. I also disabled Postman's send no-cache header and I also tried manually adding a Cache-Control: max-age=3600.
But None of these works.
Anyone knows why?
I just solved a similar issue by noticing that the API Gateway domain name for my RestAPI was always returning an X-Cache header with "Miss from cloudfront"; but after realizing I had used the execute api domain, my second request to my *.cloudfront.net domain resulted in a Cloudfront cache hit.

AWS Cloudfront for VPC/VPN

Does AWS allow usage of Cloudfront for websites usage, eg:- caching web pages.
Website should be accessible within corporate VPN only. Is it a good idea to cache webpages on cloudfront when using Application restricted within one network?
As #daxlerod points out, it is possible to use the relatively new Web Application Firewall service with CloudFront, to restrict access to the content, for example, by IP address ranges.
And, of course, there's no requirement that the web site actually be hosted inside AWS in order to use CloudFront in front of it.
However, "will it work?" and "are all the implications of the required configuration acceptable from a security perspective?" are two different questions.
In order to use CloudFront on a site, the origin server (the web server where CloudFront fetches content that isn't in the cache at the edge node where the content is being requested) has to be accessible from the Internet, in order for CloudFront to connect to it, which means your private site has to be exposed, at some level, to the Internet.
The CloudFront IP address ranges are public information, so you could partially secure access to the origin server with the origin server's firewall, but this only prevents access from anywhere other than through CloudFront -- and that isn't enough, because if I knew the name of your "secured" server, I could create my own CloudFront distribution and access it through CloudFront, since the IP addresses would be in the same range.
The mechanism CloudFront provides for ensuring that requests came from and through an authorized CloudFront distribution is custom origin headers, which allows CloudFront to inject an unknown custom header and secret value into each request it sends to your origin server, to allow your server to authenticate the fact that the request not only came from CloudFront, but from your specific CloudFront distribution. Your origin server would reject requests not accompanied by this header, without explanation, of course.
See http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/forward-custom-headers.html#forward-custom-headers-restrict-access.
And, of course, you need https between the browser and CloudFront and https between CloudFront and the origin server. It is possible to configure CloudFront to use (or require) https on the front side or the back side separately, so you will want to ensure it's configured appropriately for both, if the security considerations addressed above make it a viable solution for your needs.
For information that is not highly sensitive, this seems like a sensible approach if caching or other features of CloudFront would be beneficial to your site.
Yes, you CloudFront is designed as a caching layer in front of a web site.
If you want to restrict access to CloudFront, you can use the Web Application Firewall service.
Put your website into public network > In CloudFront distribution attach WAF rules > In WAF rules whitelist range of your company's IP's and blacklist everything else