如何解决无法从Lambda函数访问公共API?

0

【以下的问题经过翻译处理】 您好社区,我无法从我的 lambda 函数访问公共 API,例如 google static map api 和 twilio/sendgrid,但是可以从 localhost 访问它,下面是错误;

调用 google map api 时出错

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 } }
profile picture
专家
已提问 5 个月前3 查看次数
1 回答
0

【以下的回答经过翻译处理】 如果您想让连接到虚拟私有云的Lambda函数访问互联网,它必须连接到私有子网,并且必须在公共子网中部署NAT网关/ NAT实例。

profile picture
专家
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则