Has anyone been able to get native fetch() working on a Lambda

I’m running Node 18 on a Lambda and trying to run fetch(), but still cannot get it to work. No output, no logs, nothing. My timeout is 30 seconds. Any suggestions? Has anyone else gotten it to work?

const url = “https://jsonmock.hackerrank.com/api/movies”;
fetch(url).then(res => {
return res.json();
}).then(data => {
console.log('DATA: ', data);
})

I saw the other day that fetch was introduced in v18 but I haven’t migrated my lambdas yet. It’s on my todo list. I’ll be watching this thread.

r.json() is a promise.

That shouldn’t be an issue when chaining .then() statements (ie promises “collapse” returned promises, if that makes sense)

gotcha well fetch() in node 18 lambda works fine so problem exists in the code somewhere.

Have you assigned your lambda a VPC and security group?

This was my first thought…is your Lambda launching in a VPC? (And getting an IP address?)

You may not be handling the error from the promise. If you switch to async/await, it will throw instead of needing an error callback

yep, we’re using it for all lambdas (including VPC)

What NodeJS runtime version are you using? Your runtime might just not have it. It wasn’t fully released until v18. For anything older than 18 you’ll need to use a package like node-fetch to get access to the same API.

Does the lambda have cloudwatch permissions to write error logs? Often overlooked.

This feels like a bigger picture problem.

  1. Can you verify that you are able to connect to the internet from the Lambda? For example, if you’re deployed in a VPC/subnet without an internet gateway / NAT instance, you may not be able to connect to the public internet.
  2. Can you verify that you’re properly logging errors? Ie is the Lambda permitted to write to cloudwatch? Are you catching the errors (ex .catch(console.error))

const url = “https://jsonmock.hackerrank.com/api/movies”;
fetch(url)
.then(res => {
return res.json();
})
.then(data => {
console.log('DATA: ', data);
})
.catch(console.error);

This. I created some lambda functions using node 18 and aws sdk v3, and used fetch without a problem.

I’m also going to add, if you are trying to connect to a public API and your lambda is in a VPC, you need to have a NAT Gateway (and all the configuration needed in a separate subnet with a route table for all traffic 0.0.0.0/0 to the NAT) to access the public internet (Internet gateways doesn’t work for lambda because lambda functions doesn’t have a public IP like EC2).

Does your lambda have the correct permissions to output to the logs?

Stop trying to make fetch() happen

Feeling like your lambda is not awaiting the end of the executions of the promises. Try async await instead.

You’re not catching an error. I think that your lambda is probably running in a public subnet. It must run in a private subnet with a Nat gateway if you are using VPC.. Does it work when not in a vpc?

I’m having the same issue, on a lambda running node 20, and confirmed I have public internet connectivity (not running in a VPC).

I’ve been able to use the `node-fetch` library from an added layer, but am unable to use the native fetch. No errors of undefined fetch, just a timeout.