how to call rest api inside aws lambda function using nodejs
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Calling a REST API from AWS Lambda with Node.js is straightforward because a Lambda function is just Node.js code running inside a managed runtime. The important parts are choosing a simple HTTP client, setting timeouts, handling retries carefully, and understanding that network access depends on whether the function runs in a VPC.
Use the Built-In fetch in Modern Lambda Runtimes
In current Node.js Lambda runtimes, the easiest option is the built-in fetch API. That avoids an extra dependency such as axios for many use cases.
A minimal handler that calls a JSON API looks like this:
That is enough for public APIs or internal APIs reachable from the Lambda runtime.
Add Timeouts and Clear Error Handling
The default failure mode for remote calls is often too vague. A production Lambda should fail with enough context to debug the upstream problem.
This gives you a defined upper bound on the outbound request instead of waiting until the whole Lambda invocation times out.
POST JSON to an API
Sending data is just as simple. Serialize the body and set the content type explicitly.
This pattern is common when Lambda is acting as a small integration layer between AWS services and an external system.
VPC Networking Changes the Failure Mode
A Lambda function without VPC attachment can reach the public internet by default. Once you attach it to private subnets in a VPC, outbound internet access requires the right route through a NAT gateway or equivalent egress path.
So if your code works locally but times out in Lambda, check networking before rewriting the handler.
Typical signs of a network problem:
- long timeouts instead of fast HTTP errors
- DNS resolution errors
- connection refused from a private target that is not reachable from the selected subnets
This operational detail matters more than the choice between fetch and axios.
Keep Secrets Out of the Source Code
API keys and bearer tokens should come from environment variables or AWS Secrets Manager, not hard-coded strings.
If the secret changes frequently or is shared across multiple functions, Secrets Manager is usually a better operational choice than plain environment variables.
Reuse Connections Across Invocations
A Lambda execution environment can be reused for multiple invocations. Put reusable clients outside the handler when you can. For simple fetch calls this matters less, but for database or SDK clients it helps reduce cold-start and per-request overhead.
You should also avoid blind retries inside the handler. If the event source already retries, doubling the retry logic can make failures noisier and more expensive.
Example With axios
If your team already uses axios, it works fine too.
The built-in fetch is usually enough, but axios still provides a familiar API for teams already standardized on it.
Common Pitfalls
Assuming the code has internet access after placing Lambda in private subnets. VPC routing often causes the real failure.
Leaving the outbound request without a timeout. Then the Lambda timeout becomes your only protection.
Hard-coding API keys in the source code. Use environment variables or Secrets Manager.
Retrying too aggressively inside the handler. Coordinate retries with the event source.
Ignoring non-200 responses and trying to parse everything as success JSON.
Summary
- A Lambda function can call REST APIs with the built-in
fetchin modern Node.js runtimes. - Add explicit timeouts and clear error messages.
- Use environment variables or Secrets Manager for credentials.
- Check VPC egress if requests time out in AWS but work locally.
- Keep the handler small and let the network and retry design stay deliberate.

