Reputation: 846
I am new to node.js as well to AWS lambda. Initially, I have installed node.js and run some basic scripts in it. I was trying to consume third party APIS like Google, Youtube, and IMDb, etc. I have written the my logic script on the local machine, It is working perfectly on the local machine, as I did the same thing on in exports.handler
the method is not working and throwing errors.
Here is the logic of the handler function:
var https = require('https');
exports.handler = async(event) => {
return sendRes(200, 'Hello');
};
const sendRes = (status, body) => {
const options = {
hostname: 'api.themoviedb.org',
path: '3/search/movie?query=avengers&page=1&api_key=MY-API-KEY',
method: 'GET',
agent: false
};
console.log(options);
const req = https.request(options, (res) => {
console.log("statusCode: ", res.statusCode);
res.on('data', (d) => {
console.log('here in request');
});
});
};
Basically, on triggering specific event Lamda will consume API and return JSON response. Initially when I trigger this function using Test it throws the following error:
Task timed out after 3.00 seconds
I have tried the following solutions after looking over the internet:
Can anyone help me with this?
Refereces:
Upvotes: 0
Views: 1396
Reputation: 7770
You basically need a public subnet which has access to the internet so that you can talk to outside world. By default you can only access the things which are inside your vpc.
Upvotes: 1
Reputation: 846
AWS uses specific Network permissions to consume APIS from the outer world. Although you can consume your own aws-endpoints because it is on the same VPC. I was searching on the internet for this issue any I end up with the following findings:
AWS Lambda uses the VPC information you provide to set up ENIs that allow your Lambda function to access VPC resources. Each ENI is assigned a private IP address from the IP address range within the Subnets you specify but is not assigned any public IP addresses. Therefore, if your Lambda function requires Internet access (for example, to access AWS services that don't have VPC endpoints, such as Amazon Kinesis), you can configure a NAT instance inside your VPC or you can use the Amazon VPC NAT gateway. For more information, see NAT Gateways in the Amazon VPC User Guide. You cannot use an Internet gateway attached to your VPC since that requires the ENI to have public IP addresses.
You can look into the links I have mentioned below for details and configurations.
References:
Solution: I am not good with the VPC and NAT gateways, initially I have removed all the VPC rules, were applied on my Lamda and selected No VPC for now. It is not recommended because we have to attach our VPC and NAT rules.
Upvotes: 0