Aliaksei Bulhak
Aliaksei Bulhak

Reputation: 6208

Access Api Gateway from Lambda under VPC

I need to fetch some data from ApiGateway endpoint and then based on response store this data in database.

I created simple Lambda function that just fetch data from ApiGateway endpoint and print it in console. My first Lambda function did not have any VPC configuration and fetch operation worked like a charm.

const fetch = require('node-fetch');

exports.handler = async () => {
  const data = await fetch("https://<<ag-api-key>>.execute-api.us-east-1.amazonaws.com/v1/data");
  const response = await data.json();
  console.log(data, response);
}

As I need to store data received from endpoint into database which run under VPC I decided to put Lambda in same VPC(this vpc has configured Internet Gateways and other stuff to have access to internet). As a result fetch operation start to fail with 403 response code and {"message":"Forbidden"} response body.

Api Gateway resource does not have any custom domain configuration and maintained by other team so I do not have direct access to its configuration

May be anyone can suggest how I can fix this

Upvotes: 3

Views: 2425

Answers (1)

Mikes3ds
Mikes3ds

Reputation: 600

  1. Security Groups, check if port 443 is open
  2. Check your CORS Setting on API Gateway.
  3. Try Hitting the API Gateway with Postman/Fiddler, or any other testing tool to make sure your API Gateway is online and you can get the response you want.
  4. If you are using a Private API-Gateway (Sounds like you are using public looking at the URL) you will need some header data and different URL. I can guide you through it if needed. I would avoid private API gateway if I were you.

Let me know if any of that helps. I have ran into that issue many times in different situations.

Upvotes: 1

Related Questions