AWS Lambda external URI request error: connect ETIMEDOUT - node.js

I coded in my local dev environment a node.js function that does several requests to an external url-uri (asynchronously using bluebird and request-promise). It works fine, the function gets the results and save the information into the EC3 database.
The problem comes when I deploy the code (node modules included), and execute it. It has access to the database, but when tries to access to external url-uri the 'request-promise' module gets an 'connect ETIMEDOUT' error.
I did all the AWS indicates to get it, and read and try a all the solutions I found in Stackoverflow, but still having the problem.
https://www.youtube.com/watch?v=AR1nt3iGR5o
The related role that runs the function has the following policies:
AWSLambdaFullAccess - AWSCodeDeployRoleForLambda - AmazonVPCFullAccess - AWSLambdaExecute - AWSLambdaBasicExecutionRole - AWSLambdaVPCAccessExecutionRole - AWSLambdaRole - oneClick_lambda_basic_execution_1535968782861
Function Network Config
Nat getway
Route Table
Could you help me please, or at least give a hint, please?
CODE:
const Promise = require('bluebird');
const Rp = require('request-promise');
const http = require('http');
var httpAgent = new http.Agent();
httpAgent.maxSockets = 15;
var promises = urls.map(function(url){
return Rp({uri: url.url, pool:httpAgent}).then(function(result){
url.result = result;
// Saving space
delete url.url;
return url;
})
});
Promise.all(promises).then(function(results){
return(processResults(results));
}).catch(Error, function (e) {
console.error("Error doing Request: ", e);
}).error(function (e) {
console.error("Unable get info: ", e);
}).then(function(results){
try{
product.callback(results);
}catch (exception) {
console.error('Error callback: ',exception);
}
}).then(function(){
product.finally();
});
ERROR:
2018-09-28T14:53:48.989Z efb5493a-c32d-11e8-ae42-f73dec33ca2a Error doing Request: { RequestError: Error: connect ETIMEDOUT 147.83.184.65:80
at new RequestError (/var/task/node_modules/request-promise-core/lib/errors.js:14:15)
at Request.plumbing.callback (/var/task/node_modules/request-promise-core/lib/plumbing.js:87:29)
at Request.RP$callback [as _callback] (/var/task/node_modules/request-promise-core/lib/plumbing.js:46:31)
at self.callback (/var/task/node_modules/request/request.js:185:22)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at Request.onRequestError (/var/task/node_modules/request/request.js:881:8)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at Socket.socketErrorListener (_http_client.js:387:9)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)
name: 'RequestError',
message: 'Error: connect ETIMEDOUT 147.83.184.65:80',
cause:
{ Error: connect ETIMEDOUT 147.83.184.65:80
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
code: 'ETIMEDOUT',
errno: 'ETIMEDOUT',
syscall: 'connect',
address: '147.83.184.65',
port: 80 },
error:
{ Error: connect ETIMEDOUT 147.83.184.65:80
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
code: 'ETIMEDOUT',
errno: 'ETIMEDOUT',
syscall: 'connect',
address: '147.83.184.65',
port: 80 },
options:
{ uri: 'http://geoserver.hydsdev.net/geoserver/mhews/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetFeatureInfo&FORMAT=image%2Fjpeg&TRANSPARENT=true&INFO_FORMAT=text%2Fxml&FEATURE_COUNT=50&X=50&Y=50&SRS=EPSG%3A4326&WIDTH=101&HEIGHT=101&QUERY_LAYERS=mhews:ffews_rain_accumulation_15min_opera&LAYERS=mhews:ffews_rain_accumulation_15min_opera&BBOX=0.6319608%2C42.770155%2C0.8319608%2C42.870155000000004&TIME=2018-09-28T17:30:00.000Z',
pool:
Agent {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 80,
protocol: 'http:',
options: [Object],
requests: {},
sockets: {},
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: 15,
maxFreeSockets: 256,
'http:': [Object] },
callback: [Function: RP$callback],
transform: undefined,
simple: true,
resolveWithFullResponse: false,
transform2xxOnly: false },
response: undefined }
Cheers.

Finally I fixed the problem. Don't know why but into 'request-promise' options object I have to put: headers: {'User-Agent':'request' } . Thank you very much #Rajesh!

Related

ssl3_get_record:wrong version number, ERPROTO Error when trying to connect using a proxy from an online list

I am trying to connect to a kahoot using a proxy from an online proxy list. The proxy is up, and works just fine so that isn't the problem, but I can't actually connect to it with a request from the code. I've tried using http, https, socks4 and socks5 proxies, all of them return the same error. Is there something I'm missing or need to add? The "options" and "proxy" is just an http request same as what you would see on https://nodejs.org/api/http.html#http_http_request_options_callback
My code:
proxy: (options)=>{
return Object.assign(options,{
host: "51.104.203.226:33800",
});
The full error:
{
description: 'Request failed or timed out.',
error: RequestError: write EPROTO 14200000:error:0A00010B:SSL routines:ssl3_get_record:wrong version number:c:\ws\deps\openssl\openssl\ssl\record\ssl3_record.c:355:
at ClientRequest.<anonymous> (C:\Users\queba\Desktop\kahootspam\node_modules\kahoot.js-latest\node_modules\got\dist\source\core\index.js:970:111)
at Object.onceWrapper (node:events:628:26)
at ClientRequest.emit (node:events:525:35)
at origin.emit (C:\Users\queba\Desktop\kahootspam\node_modules\kahoot.js-latest\node_modules\#szmarczak\http-timer\dist\source\index.js:43:20)
at TLSSocket.socketErrorListener (node:_http_client:494:9)
at TLSSocket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
at WriteWrap.onWriteComplete [as oncomplete] (node:internal/stream_base_commons:94:16) {
code: 'EPROTO',
timings: {
start: 1667511954639,
socket: 1667511954641,
lookup: 1667511954765,
connect: 1667511954765,
secureConnect: undefined,
upload: 1667511954893,
response: undefined,
end: undefined,
error: 1667511954894,
abort: undefined,
phases: [Object]
}
}
}

ELOGIN error while connecting to the SQL server

I'm learning full stack web development and was trying to connect to SQL server from my backend Node.js. I was following an online video. While running the index.js file I get the below error -
ConnectionError: Login failed for user 'systemadmin'.
at C:\Users\akunjalw\Desktop\FullStack\server\node_modules\mssql\lib\tedious\connection-pool.js:70:17
at Connection.onConnect (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\connection.js:1038:9)
at Object.onceWrapper (node:events:640:26)
at Connection.emit (node:events:520:28)
at Connection.emit (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\connection.js:1066:18)
at Parser.<anonymous> (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\connection.js:2574:20)
at Object.onceWrapper (node:events:639:28)
at Parser.emit (node:events:520:28)
at Readable.<anonymous> (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\token\token-stream-parser.js:32:12)
at Readable.emit (node:events:520:28) {
code: 'ELOGIN',
originalError: ConnectionError: Login failed for user 'systemadmin'.
at Login7TokenHandler.onErrorMessage (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\token\handler.js:239:19)
at Readable.<anonymous> (C:\Users\akunjalw\Desktop\FullStack\server\node_modules\tedious\lib\token\token-stream-parser.js:26:33)
at Readable.emit (node:events:520:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Readable.push (node:internal/streams/readable:228:10)
at next (node:internal/streams/from:98:31)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 'ELOGIN',
isTransient: undefined
}
}
undefined
[nodemon] clean exit - waiting for changes before restart
The code is as follows
const sql = require("mssql");
const config = {
user: "systemadmin",
password: "R#jasthaan1212",
server: "localhost",
database: "ORG_EMPLOYEEDATA",
options: {
trustedconnection: true,
trustServerCertificate: true,
enableArithAbort: true,
instancename: "SQL2019",
},
port: 50685,
};
async function getEmployeeName() {
try {
let pool = await sql.connect(config);
let employeeData = await pool
.request()
.query("select * from dbo.EMPLOYEES_DATA");
return employeeData.recordsets;
} catch (error) {
console.log(error);
}
}
module.exports = { getEmployeeName: getEmployeeName };
const dboperations = require("./dboperations");
dboperations.getEmployeeName().then((result) => {
console.log(result);
});
Please let me know what exactly I'm missing here. I couldn't find the way to resolve this by searching in internet as well, may be I'm terrible at searching. Any help to resolve this is appreciated.

The db connection throws error in model unit test without explicit invoking

I am doing unit test on nodejs 12.x jest 25 app. When the unit test is started with npm test, at the end it throws an error about a startup file db.js which establishes db connection. Here is the db.js file:
const Sql = require("sequelize");
const db = new Sql('emps', 'postgres', `${process.env.DB_PASSWORD}`, {
host: 'localhost',
dialect: 'postgres',
port:5432,
} );
db
.authenticate() //<<<====this line causes the error
.then(() => {
console.log('DB connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
module.exports = db;
Here is the error at the end of jest unit test:
console.error startup/db.js:32
Unable to connect to the database: ConnectionError [SequelizeConnectionError]: password authentication failed for user "postgres"
at C:\D\code\js\emps_bbone\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:182:24
at Connection.connectingErrorHandler (C:\D\code\js\emps_bbone\node_modules\pg\lib\client.js:194:14)
at Connection.emit (events.js:311:20)
at Socket.<anonymous> (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:134:12)
at Socket.emit (events.js:311:20)
at addChunk (_stream_readable.js:294:12)
at readableAddChunk (_stream_readable.js:275:11)
at Socket.Readable.push (_stream_readable.js:209:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
name: 'SequelizeConnectionError',
parent: error: password authentication failed for user "postgres"
at Connection.Object.<anonymous>.Connection.parseE (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:614:13)
at Connection.Object.<anonymous>.Connection.parseMessage (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:413:19)
at Socket.<anonymous> (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:129:22)
at Socket.emit (events.js:311:20)
at addChunk (_stream_readable.js:294:12)
at readableAddChunk (_stream_readable.js:275:11)
at Socket.Readable.push (_stream_readable.js:209:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
name: 'error',
length: 163,
severity: 'FATAL',
code: '28P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'd:\\pginstaller.auto\\postgres.windows-x64\\src\\backend\\libpq\\auth.c',
line: '336',
routine: 'auth_failed'
},
original: error: password authentication failed for user "postgres"
at Connection.Object.<anonymous>.Connection.parseE (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:614:13)
at Connection.Object.<anonymous>.Connection.parseMessage (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:413:19)
at Socket.<anonymous> (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:129:22)
at Socket.emit (events.js:311:20)
at addChunk (_stream_readable.js:294:12)
at readableAddChunk (_stream_readable.js:275:11)
at Socket.Readable.push (_stream_readable.js:209:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
name: 'error',
length: 163,
severity: 'FATAL',
code: '28P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'd:\\pginstaller.auto\\postgres.windows-x64\\src\\backend\\libpq\\auth.c',
line: '336',
routine: 'auth_failed'
}
}
The db.js is required in app's index.js:
require("./startup/db");
Since my unit tests only test models (the db connection works fine. The problem is likely process.env.DB_PASSWORD didn't return a value somehow.), I don't quite understand how the db connection test jumps in by itself and throws error.

Recaptcha Handling Throwing Exception Inside Promise with catch

Does anybody know how to handle the error from below properly? Obviously, I am using a 3rd party library that performs an internal HTTP poll to a remote API, sometimes, it throws a connection refusal error and crashes the whole NodeJS process.
return new Promise((resolve, reject) => {
page.getRecaptchaSolutions(captchas).then(({ solutions, solErrors }) => {
if (solErrors)
reject(solErrors);
resolve(solutions)
})
.catch(error => {
reject(new Error(error))
})
})
Error: getaddrinfo EAI_AGAIN 2captcha.com
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26)
Emitted 'error' event on ClientRequest instance at:
at Socket.socketErrorListener (_http_client.js:406:9)
at Socket.emit (events.js:223:5)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:81:21) {
errno: 'EAI_AGAIN',
code: 'EAI_AGAIN',
syscall: 'getaddrinfo',
hostname: '2captcha.com'
}

RequestError: Error: read ECONNRESET nodejs

I try to use nba.com api, but give me that Error.
"RequestError: Error: read ECONNRESET
at new RequestError (c:\Users\Omer\Desktop\game\node_modules\request-promise-core\lib\errors.js:14:15)
at Request.plumbing.callback (c:\Users\Omer\Desktop\game\node_modules\request-promise-core\lib\plumbing.js:87:29)
at Request.RP$callback [as _callback] (c:\Users\Omer\Desktop\game\node_modules\request-promise-core\lib\plumbing.js:46:31)
at self.callback (c:\Users\Omer\Desktop\game\node_modules\request\request.js:188:22)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at Request.onRequestError (c:\Users\Omer\Desktop\game\node_modules\request\request.js:884:8)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at TLSSocket.socketErrorListener (_http_client.js:387:9)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
From previous event:
at Request.plumbing.init (c:\Users\Omer\Desktop\game\node_modules\request-promise-core\lib\plumbing.js:36:28)
at Request.RP$initInterceptor [as init] (c:\Users\Omer\Desktop\game\node_modules\request-promise-core\configure\request2.js:41:27)
at new Request (c:\Users\Omer\Desktop\game\node_modules\request\request.js:130:8)
at request (c:\Users\Omer\Desktop\game\node_modules\request\index.js:54:10)
at requestStats (c:\Users\Omer\Desktop\game\modules\utils\crawlers\stats\nba.stats.crawler.js:23:12)
at Object.crawl (c:\Users\Omer\Desktop\game\modules\utils\crawlers\stats\nba.stats.crawler.js:12:12)
at Object.crawl (c:\Users\Omer\Desktop\game\modules\utils\crawlers\stats\stats.crawler.js:20:20)
at Object.runCrawl (c:\Users\Omer\Desktop\game\modules\utils\crawlers\utils.crawler.js:27:18)
at startCrawl (c:\Users\Omer\Desktop\game\scripts\useful\crawl.js:19:13)
at loadConfig (c:\Users\Omer\Desktop\game\scripts\useful\crawl.js:12:5)
at c:\Users\Omer\Desktop\game\config\lib\mongoose.js:35:21
at
at process._tickCallback (internal/process/next_tick.js:188:7)"
That my code:
var path = require('path'),
request = require('request-promise'),
format = require('string-template');
module.exports = {
crawl: crawl
};
const STATS_NBA_API = 'http://stats.nba.com/stats/leaguegamelog?Counter=1000&DateFrom=&DateTo=&Direction=DESC&LeagueID=00&PlayerOrTeam=P&Season=2017-18&SeasonType=Regular+Season&Sorter=DATE';
function crawl(options){
return requestStats(STATS_NBA_API)
.then(statsPlayers => { return convertStatsToList(statsPlayers); })
.catch(error =>
console.log(error))
}
function convertStatsToList(statsPlayers){
console.log(statsPlayers)
}
function requestStats(url){
var options = {
method: 'GET',
url: url,
json: true
};
return request(options);
}
function requestStats(url) {
var options = {
method: 'GET',
url: url,
json: true,
headers: {
'Connection': 'keep-alive',
'Accept-Encoding': '',
'Accept-Language': 'en-US,en;q=0.8'
}
};
return request(options);
}

Resources