Cloudfront adding extra latency for API Gateway

54 Views Asked by At

When calling the regional API Gateway Endpoints, response time is ~300ms, (same with custom domain name).

Our webapp gets served from Cloudfront + S3 at https://myexample.com

Now to avoid CORS, I added another origin (API Gateway) in Cloudfront with behavior of /api/* to point to API Gateway

Basically Cloudfront is adding those extra 500ms, is there a way to optimize this?

I found similar question, but that was unanswered: API Gateway edge-optimized added latency

1

There are 1 best solutions below

0
MarcC On

Every request to this API path will be routed to your origin. I mean the client will connect to the closest POP then CloudFront will connect to the API Gateway to forward the request.

We faced the same kind of results with a custom origin.

You should review your connection settings. Establishing an HTTPs connection is a lengthy operation, check the connection timeout setting for your origin. You need of course to warmup the POP before your tests.

You can collect server-timing headers. That's a great way to check if you are reusing a connection or not and where you waste time.

I'm not 100% sure why, but we also noticed that activating the origin shield option for custom origins reduces the latency.