Skip to content

Using CloudFront as a Non-Cached Backend Request Proxy

0

We have CloudFront running for our app domain (with many different micro frontends beneath; routing handled via CloudFront functions): app.example.com

At the same time, our central API management is under a different root domain: api.example.cloud

This can't be changed.

We've now built an Authentication BFF/Reverse Proxy into the API management, so we can use cookies instead of LocalStorage for storing access & refresh tokens. The problem: with the API Management's domain, we can neither use strict or lax cookie settings and Safari blocks such cookies by default. Google at least says that they'll do the same soon.

Idea: we also proxy everything through CloudFront with a disabled cache by adding an ordered cache behavior that matches the backend URLs (which luckily all start with /api in their path) and directs the requests to our API management origin. Then the cookies are also on app.example.com and everything fits.

I've estimated additional costs and as we're not running super heavy traffic (~100m/r month), it seems very acceptable (with $0.75/1m requests & $0.085/GB egress traffic it would net to ~$104 - excluding the free tier benefit). We also do not care about the additional hop, as it only adds ~15ms to the response times.

I do like the solution and already tested it on our staging environment, and it works like a charm, but still have some subconscious feelings that I've missed something really critical.

Edit: Just came up with the fact that we could just create a CNAME for a new subdomain under example.com (e.g. api.example.com) that points to api.example.cloud and re-issue the certificate for the API management with one alternative name. This would avoid the CloudFront proxy solution completely, but we'd only get lax instead of strict cookie settings.

4 Answers
1

Your approach of using CloudFront as a non-cached backend request proxy for your API management is a valid and effective solution to the cookie domain issue you're facing. This setup allows you to maintain your existing domain structure while solving the cookie security problem.

Here's a breakdown of your solution and its implications:

  1. CloudFront Configuration: You're adding an ordered cache behavior that matches the backend URLs (starting with '/api') and directs these requests to your API management origin. This is a correct approach for routing specific paths to a different origin.

  2. Caching Disabled: For this behavior, you're disabling caching. This is crucial for API requests that need to be fresh and not served from cache. You can achieve this by setting the cache policy to "CachingDisabled" for the API-related behavior.

  3. Cookie Handling: By proxying the API requests through CloudFront on your app domain, you're allowing cookies to be set on the same domain as your application. This solves the issue with strict and lax cookie settings, making your setup compatible with Safari's current restrictions and future browser changes.

  4. Cost Estimation: Your cost estimation seems reasonable. CloudFront's pricing model is indeed based on the number of requests and data transfer, so your calculations appear to be on the right track.

  5. Performance Impact: The additional hop through CloudFront typically adds minimal latency, as you've observed. CloudFront's global network often compensates for this by reducing the distance to the origin for many users.

  6. Security: This setup can provide additional security benefits. CloudFront can act as a shield for your API, offering features like DDoS protection and SSL/TLS termination.

  7. Origin Request Policy: Ensure you've set up an appropriate origin request policy. For API requests, you typically want to forward all headers, cookies, and query strings to the origin.

  8. HTTPS: Make sure you're using HTTPS for all communications, both between the client and CloudFront, and between CloudFront and your API origin.

Your solution is a common and recommended approach for solving cross-domain cookie issues while leveraging the benefits of a CDN. The only potential drawback is the slight increase in complexity in your infrastructure, but the benefits in terms of security and functionality outweigh this concern.

Remember to thoroughly test all API endpoints, especially those involving authentication and session management, to ensure everything works as expected in this new setup.
Sources
cloudfront distribution with several origins | AWS re:Post
Amazon CloudFront now accepts your applications’ gRPC calls | AWS News Blog
OriginRequestPolicyCookiesConfig - Amazon CloudFront
CachePolicyCookiesConfig - Amazon CloudFront

answered 2 years ago

EXPERT

reviewed 2 years ago

1

Hi,

To implement your solution, you may want to use Lambda@Edge and their local memory to store the cookie on the edge and avoid having to go back each time to central location (only when you have a local cache miss)

For a sample of Lambda@Edge memory, see "Cache your data in Lambda@Edge Memory" of https://aws.amazon.com/blogs/networking-and-content-delivery/leveraging-external-data-in-lambdaedge/

Also https://medium.com/uniplacesgeeks/cache-control-with-lambda-edge-95645b3aa4f0 may be useful

For Lambda@Edge (in you don't use it already): https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-at-the-edge.html

Best,

Didier

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

0

Your approach is sound and commonly used. Here's a quick validation of your solution:

Pros:

Solves the cookie domain issue

Maintains security with same-origin policy

Minimal latency impact

Reasonable cost estimation

Simple implementation

Key considerations:

Ensure proper cache settings:

{ "CachePolicyConfig": { "MinTTL": 0, "MaxTTL": 0, "DefaultTTL": 0, "ParametersInCacheKeyAndForwardedToOrigin": { "EnableAcceptEncodingGzip": true, "EnableAcceptEncodingBrotli": true, "HeadersConfig": { "HeaderBehavior": "whitelist", "Headers": ["Authorization", "Host"] } } } }

Security measures:

Use HTTPS only

Configure appropriate security headers

Consider implementing WAF rules

Set up proper origin request policies

The solution is architecturally sound and commonly implemented for similar use cases.

answered 2 years ago

0

Your approach is sound and commonly used. Here's a quick validation of your solution:

Pros:

Solves the cookie domain issue

Maintains security with same-origin policy

Minimal latency impact

Reasonable cost estimation

Simple implementation

Key considerations:

Ensure proper cache settings:

{ "CachePolicyConfig": { "MinTTL": 0, "MaxTTL": 0, "DefaultTTL": 0, "ParametersInCacheKeyAndForwardedToOrigin": { "EnableAcceptEncodingGzip": true, "EnableAcceptEncodingBrotli": true, "HeadersConfig": { "HeaderBehavior": "allowlist", "Headers": ["Authorization", "Host"] } } } }

Security measures:

Use HTTPS only

Configure appropriate security headers

Consider implementing WAF rules

Set up proper origin request policies

The solution is architecturally sound and commonly implemented for similar use cases

answered 2 years 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.