Skip to content

Not able to access public APIs from Lambda function

0

Hello community, I am not able to access the public APIs like google static map api and also twilio/sendgrid from my lambda functions, however it is accessible from localhost, below is the error;

Error while calling google map api

2022-12-28T13:46:33.405Z adeec805-e3d9-4f5e-9a7e-c4b03fe1b17b ERROR weather_err: AxiosError: connect ETIMEDOUT 142.250.181.138:443 at Function.AxiosError.from (file:///var/task/node_modules/axios/lib/core/AxiosError.js:89:14) at RedirectableRequest.handleRequestError (file:///var/task/node_modules/axios/lib/adapters/http.js:526:25) at RedirectableRequest.emit (node:events:527:28) at ClientRequest.eventHandlers.<computed> (/var/task/node_modules/follow-redirects/index.js:14:24) at ClientRequest.emit (node:events:527:28) at TLSSocket.socketErrorListener (node:_http_client:454:9) at TLSSocket.emit (node:events:527:28) at emitErrorNT (node:internal/streams/destroy:157:8) at emitErrorCloseNT (node:internal/streams/destroy:122:3) at processTicksAndRejections (node:internal/process/task_queues:83:21) { port: 443, address: '142.250.181.138', syscall: 'connect', code: 'ETIMEDOUT', errno: -110, config: { transitional: { silentJSONParsing: true, forcedJSONParsing: true, clarifyTimeoutError: false }, adapter: [ 'xhr', 'http' ], transformRequest: [ [Function: transformRequest] ], transformResponse: [ [Function: transformResponse] ], timeout: 0, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, maxBodyLength: -1, env: { FormData: [Function], Blob: null }, validateStatus: [Function: validateStatus], headers: AxiosHeaders { Accept: 'application/json, text/plain, /', 'User-Agent': 'axios/1.2.1', 'Accept-Encoding': 'gzip, compress, deflate, br' }, method: 'get', url: 'https://maps.googleapis.com/maps/api/staticmap?center=40.714%2c%20-73.998', params: { zoom: '12', size: '400x400', key: 'AIzaSyDvnSePX3KykgtV-i65AU59VXv0rQ6mKuk' }, data: undefined }, request: <ref *1> Writable { _writableState: WritableState { objectMode: false, highWaterMark: 16384, finalCalled: false, needDrain: false, ending: false, ended: false, finished: false, destroyed: false, decodeStrings: true, defaultEncoding: 'utf8', length: 0, writing: false, corked: 0, sync: true, bufferProcessing: false, onwrite: [Function: bound onwrite], writecb: null, writelen: 0, afterWriteTickInfo: null, buffered: [], bufferedIndex: 0, allBuffers: true, allNoop: true, pendingcb: 0, constructed: true, prefinished: false, errorEmitted: false, emitClose: true, autoDestroy: true, errored: null, closed: false, closeEmitted: false, [Symbol(kOnFinished)]: [] }, _events: [Object: null prototype] { response: [Function: handleResponse], error: [Function: handleRequestError], socket: [Function: handleRequestSocket] }, _eventsCount: 3, _maxListeners: undefined, _options: { maxRedirects: 21, maxBodyLength: Infinity, protocol: 'https:', path: '/maps/api/staticmap?center=40.714%2c%20-73.998&zoom=12&size=400x400&key=AIzaSyDvnSePX3KykgtV-i65AU59VXv0rQ6mKuk', method: 'GET', headers: [Object: null prototype], agents: [Object], auth: undefined, beforeRedirect: [Function: dispatchBeforeRedirect], beforeRedirects: [Object], hostname: 'maps.googleapis.com', port: '', agent: undefined, nativeProtocols: [Object], pathname: '/maps/api/staticmap', search: '?center=40.714%2c%20-73.998&zoom=12&size=400x400&key=AIzaSyDvnSePX3KykgtV-i65AU59VXv0rQ6mKuk' }, _ended: true, _ending: true, _redirectCount: 0, _redirects: [], _requestBodyLength: 0, _requestBodyBuffers: [], _onNativeResponse: [Function (anonymous)], _currentRequest: ClientRequest { _events: [Object: null prototype], _eventsCount: 7, _maxListeners: undefined, outputData: [], outputSize: 0, writable: true, destroyed: false, _last: true, chunkedEncoding: false, shouldKeepAlive: false, maxRequestsOnConnectionReached: false, _defaultKeepAlive: true, useChunkedEncodingByDefault: false, sendDate: false, _removedConnection: false, _removedContLen: false, _removedTE: false, _contentLength: 0, _hasBody: true, _trailer: '', finished: true, _headerSent: true, _closed: false, socket: [TLSSocket], _header: 'GET /maps/api/staticmap?center=40.714%2c%20-73.998&zoom=12&size=400x400&key=AIzaSyDvnSePX3KykgtV-i65AU59VXv0rQ6mKuk HTTP/1.1\r\n' + 'Accept: application/json, text/plain, /\r\n' + 'User-Agent: axios/1.2.1\r\n' + 'Accept-Encoding: gzip, compress, deflate, br\r\n' + 'Host: maps.googleapis.com\r\n' + 'Connection: close\r\n' + '\r\n', _keepAliveTimeout: 0, _onPendingData: [Function: nop], agent: [Agent], socketPath: undefined, method: 'GET', maxHeaderSize: undefined, insecureHTTPParser: undefined, path: '/maps/api/staticmap?center=40.714%2c%20-73.998&zoom=12&size=400x400&key=AIzaSyDvnSePX3KykgtV-i65AU59VXv0rQ6mKuk', _ended: false, res: null, aborted: false, timeoutCb: null, upgradeOrConnect: false, parser: null, maxHeadersCount: null, reusedSocket: false, host: 'maps.googleapis.com', protocol: 'https:', _redirectable: [Circular *1], [Symbol(kCapture)]: false, [Symbol(kNeedDrain)]: false, [Symbol(corked)]: 0, [Symbol(kOutHeaders)]: [Object: null prototype] }, _currentUrl: 'https://maps.googleapis.com/maps/api/staticmap?center=40.714%2c%20-73.998&zoom=12&size=400x400&key=AIzaSyDvnSePX3KykgtV-i65AU59VXv0rQ6mKuk', [Symbol(kCapture)]: false }, cause: Error: connect ETIMEDOUT 142.250.181.138:443 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) { errno: -110, code: 'ETIMEDOUT', syscall: 'connect', address: '142.250.181.138', port: 443 } }

  • I did a small change for testing, I remove custom vpc from serverless.yml file and it start working, now the paradigm shifted. I face the issue only when we apply lambda function in my custom VPC.

asked 4 years ago561 views

3 Answers
1
Accepted Answer

If you want a Lambda functions attached to a VPC to access the internet, it must be attached to Private subnets and you must deploy a NAT Gateway / Nat Instance in a Public subnet.

AWS
EXPERT

answered 4 years ago

AWS
EXPERT

reviewed 4 years ago

  • I already did it. Thanks for your suggestion.

0

Likely the VPC you're deploying to doesn't have access to the internet. You can verify by trying to ping 8.8.8.8 (google DNS resolver). You can follow this guide to setup internet access once you've confirmed the issue.

https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Internet_Gateway.html

answered 4 years ago

  • ping works perfectly fine, I just created an instance in the same vpc and subnets, attached with lambda, below is the output of ping; --- google.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4006ms rtt min/avg/max/mdev = 10.607/10.647/10.732/0.102 ms

0

To clarify the above, if you attach your Lambda function to a VPC then outbound access will be done via the VPC mechanisms. Lambdas not attached to VPCs will have direct internet access but no access to private VPC resources.

See https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html#vpc-internet

AWS

answered 4 years ago

  • right, APIs are accessible when I deploy Lambda function without any VPC, but when I do deploy in custom VPC, it does issues and I can not access the APIs even when I deploy the Lambda functions into public subnets.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.