I cannot seem to do a basic fetch on Lambda

So I am trying to do a lambda that does a simple fetch to TripAdvisor, and the request gets timed out before it succeeds.

For reference, my code on Lambda is as such:

export const handler = async (event) => {

    let r = await fetch("http://www.tripadvisor.com")
	let text = await r.text();
	const response = {
		statusCode: r.status,
		body: text,
		header: r.headers
	}
	return response;
	
};

I need to find a way to access the website. Once in a blue moon, the response is returned with the content, but usually, this doesn’t seem to work. What might I be doing wrong?

What’s your lambda timeout limit?

60 seconds, and if I increase it to more time, it doesn’t seem to be doing anything. The TripAdvisor website doesn’t even give me a 40X forbidden website error. It just doesn’t return anything.

Try async handler example from the doc https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html

Two guesses
- you’re using an HTTP URL and the fetch isn’t properly handling the redirect to HTTPS
- TripAdvisor have blocked all the AWS IP ranges because of web scrapers

This is indeed a pickle because I want to really access https://www.tripadvisor.com/Trips to get the name of the trips I created on the page. I’m not even trying to get Tripadvisor information except for the name of trips :cry:

Would there be any way to access the website through a proxy or something else?

It is a bit weird though, because every now and then the fetch is successful.

Your challenge is that all of the travel sites are competing with each other, which means they heavily web scrape each other to try and beat each others prices. There is a cat-and-mouse game going on with the admins of those sites spotting who is scraping them, and blocking IP ranges - which is perhaps why it works every now and again - you’re getting lucky and finding one that works.

Your proxy would have to be on a public IP that isn’t blocked - perhaps another ISP or VPS provider. Seems like quite a faff for your use case.

If you’re doing large-scale scraping, you need to use IP ranges that are designated as home IP lines - that’s why big retailers buy so many home IP connections…

Do you get similar or different results running that code on your local? If the results are the same, Tripadvisor has not blocked AWS IP ranges. If the results are different, it may be a block.

So any time I run on my local, it works 100% of the time. Any time I run it on lambda, it works only on a blood moon XD. What strikes me as odd is that I would expect a blocked AWS IP range would either return a forbidden website error or have no chance of working. Yet I get a 200 response every now and then.

Reproduce this request with curl or Postman for greater visibility.

Is your lambda on a public subnet? Lambdas on a public subnet can not reach out to the public internet because they don’t have public IPs.

The solution is to spin up your lambdas in a private subnet with a NAT gateway on it.

That’s interesting. The fact it doesn’t return an error code makes sense - if you want to block bots, you usually don’t want them to know they’re being blocked. But the occasional success is a little odd.

Three questions:

1.) Does it return real data when it does work? You may want to take a look. Some sneaky anti-bot people will serve a 200 and fake data to bots. Really sneaky anti-bot people will serve a 200 and unique fake data so they can collect evidence that a business is violating their terms.

2.) Does it occasionally work after several hours away? If so, they may be running a tool that detects bots and serves them with temp bans.

3.) I don’t know how your overall application is architected so don’t know much about the IP address you’ll make requests from. You may be set up in such a way that the lambda has a static IP, or it may be dynamic. If it’s dynamic, you may be running into a company that has banned most of (but not all) of a cloud providers IP ranges.

  1. Yes, when I get a 200, it returns real data and I get all the HTML of that trip page with the title of my trip. Please keep in mind I’ve tried this across the board with Lambda’s internal testing, as well as setting up an endpoint via API Gateway and doing a get request on postman
  2. Doesn’t seem like there is any rate limiting, and it works sporatically. Ran 3 times in a row completely fine, then the whole next day it wouldn’t. Then the next day it would maybe return successfully 1 time. I’m generalizing the exact behavior, but it is this level of crazy.
  3. I have not set up anything like a VPC or anything else. I just pressed create lambda, threw my code in the editor and deployed, then did test requests over and over again

I guess that is happening because each time a Lambda executes, it will have a different IP address. So probably TripAdvisor just didn’t block all AWS IP ranges.

When you fetch TripAdvisor on Postman, it works with no issue. But that’s obviously because it’s not using AWS’s IP range

I apologize for not understanding this. I haven’t set up the lambda any more than the basic configuration and no vpc. However, if I access a different website like google.com, I always get a 200 status code with the correct material. Does this mean that the lambda is accessing public internet?

Would running the lambda in a vpc with an elastic IP be included in “all the aws ip ranges”?

We’ve had similar problems calling third party API’s from lambda and they’ve blocked “our” IP supposedly because someone else using lambda is maliciously calling their API.

Elastic IPs are very detectable too - any admin team worth its salt will have a subscription to something like Maxmind GeoIP and will be geolocating every request they receive.

In my experience, admin teams don’t block the entire range at a time - they do individual IPs. They’ll be especially cautious if they’re using AWS / Lambda / etc themselves, but eventually, on a large well known site (such as TripAdvisor) they’ll be getting so much hassle from malicious requests that they will be in terminator mode and just blocking entire ranges.

Scraping small sites is easy - taking on large, well-funded and resourced sites is a simple cat-and-mouse game, and you’re probably going to lose.

Buy 20 home internet lines, stick them in a rack with a remote-PDU so that you can power cycle the routers and get new IPs every 30 minutes and you’ll get good data - anything else is not going to ultimately work.

Rate limiting from aws addresses? With multiple scrapers taking from the quota?