Attempting to fetch() an IP within a Worker fails

For my use case - a REST API - I use AWS Global Accelerator, which provides static IP’s that makes requests incredibly fast, in terms of getting to the ALB/origin.

The original “performance enhancement” is just to hit an IP address that’s pre-known, without needing any DNS resolution at all. It’s significantly faster than anything else in terms of latency.

I’ve seen these threads - and they don’t resolve my issue:

I need to be able to fetch from an IP inside a worker, with or without a host header. Since the IP address is hidden inside a private worker - the world can’t see it anyway, in addition to the fact that it’s my trusted IP and it’s static. Adding it as an A record publicizes a private IP to the world.

@KentonVarda would resolve-over-ride work, or can I manually add a host header to make this work? I really need this, specifically for performance enhancement at the edge - since going to our origin via IP is significantly faster than any other method.

Hi @tallyfy,

If you configure a DNS A (or AAAA) record mapping to your IP address, then you fetch() from that hostname, you should get the same effect as if you could specify the IP address raw. It should be just as fast – since the DNS is hosted by Cloudflare, the lookup will be instantaneous. It’s important to use an A record, not a CNAME. If you use a CNAME, then Cloudflare has to perform a DNS lookup for the hostname specified by the CNAME, which can take time. But with an A record, Cloudflare “just knows” the destination IP, so the lookup is “free”.

3 Likes

Great - thanks! As a secondary question - we use pages.dev (Cloudflare Pages) to resolve a CNAME for our static client/app (on a custom domain) to resolve/serve from Cloudflare Pages.

How do I apply an A record which points to Cloudflare Pages, or - is that needed to squeeze a bit more performance out of each request?

If the CNAME points to a hostname that is itself on Cloudflare, then there’s no performance concern. A slow DNS lookup would only occur if third-party DNS is involved.

2 Likes