AWS Lambda函数无法解析或连接到仅支持IPv6的域名。

0

【以下的问题经过翻译处理】 我实现了一个AWS Lambda函数,它应该将Alexa自定义技能事件传递给我的域名进行处理,代码如下所示。

const https = require('https'); exports.handler = (event, context, callback) => { var options = { hostname: '<my.domain.com>', path: '/<mypath>', port: 443, method: 'POST', rejectUnauthorized: false, headers: { 'Content-Type': 'application/json', 'Authorization': '<my base64 user:password>' } }; const req = https.request(options, (res) => { let body = ''; console.log('Status:', res.statusCode); console.log('Headers:', JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', (chunk) => { body += chunk; }); res.on('end', () => { console.log('Successfully processed HTTPS response'); body = JSON.parse(body); callback(null, body); }); }); req.on('error', callback); req.write(JSON.stringify(event)); req.end(); };

该函数运行在无服务器环境中,没有连接到VPC。

域名<my.domain.com>解析到IPv6地址,我能够从互联网实例使用curl连接到我的主机并收到预期的响应。

curl -i -k -v -X POST -d testcase.json -u [user:password] https://<my.domain.com>:<my port>/<my path>

我在AWS中实现了一个测试案例并运行它。测试返回了错误的ENOTFOUND,从函数getaddrinfo尝试解析我的域名,执行结果如下。

Test Event Name Test0001

Response { "errorType": "Error", "errorMessage": "getaddrinfo ENOTFOUND <my.domain.com>", "trace": [ "Error: getaddrinfo ENOTFOUND <my.domain.com>", " at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)" ] }


Function Logs LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [platform] EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [SHUTDOWN,INVOKE] START RequestId: 78314f37-e991-4d3d-b4f2-03da64bf91b7 Version: $LATEST 2022-09-24T04:59:06.966Z 78314f37-e991-4d3d-b4f2-03da64bf91b7 ERROR Invoke Error {"errorType":"Error","errorMessage":"getaddrinfo ENOTFOUND <my.domain.com>","code":"ENOTFOUND","errno":-3008,"syscall":"getaddrinfo","hostname":"<my.domain.com>","stack":["Error: getaddrinfo ENOTFOUND <my.domain.com>"," at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)"]} END RequestId: 78314f37-e991-4d3d-b4f2-03da64bf91b7 REPORT RequestId: 78314f37-e991-4d3d-b4f2-03da64bf91b7 Duration: 425.43 ms Billed Duration: 426 ms Memory Size: 128 MB Max Memory Used: 76 MB Init Duration: 248.14 ms

在调查过程中,我发现了添加选项 "family: 6, "的提示。使用该选项后,测试用例将域解析为正确的 ipv6 地址,但在尝试连接该地址时却返回 EAFNOSUPPORT,执行结果如下。


Request ID 78314f37-e991-4d3d-b4f2-03da64bf91b7

Test Event Name Test0001

Response { "errorType": "Error", "errorMessage": "connect EAFNOSUPPORT xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:443 - Local (undefined:undefined)", "trace": [ "Error: connect EAFNOSUPPORT xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:443 - Local (undefined:undefined)", " at internalConnect (node:net:953:16)", " at defaultTriggerAsyncIdScope (node:internal/async_hooks:465:18)", " at GetAddrInfoReqWrap.emitLookup [as callback] (node:net:1097:9)", " at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:73:8)" ] }

Function Logs LOGS Name: cloudwatch_lambda_agent State: Subscribed Types: [platform] EXTENSION Name: cloudwatch_lambda_agent State: Ready Events: [INVOKE,SHUTDOWN] START RequestId: f3493148-071f-466d-94c7-d29a0d715640 Version: $LATEST 2022-09-24T05:06:52.877Z f3493148-071f-466d-94c7-d29a0d715640 ERROR Invoke Error {"errorType":"Error","errorMessage":"connect EAFNOSUPPORT xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:443 - Local (undefined:undefined)","code":"EAFNOSUPPORT","errno":-97,"syscall":"connect","address":"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx","port":443,"stack":["Error: connect EAFNOSUPPORT xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:443 - Local (undefined:undefined)"," at internalConnect (node:net:953:16)"," at defaultTriggerAsyncIdScope (node:internal/async_hooks:465:18)"," at GetAddrInfoReqWrap.emitLookup [as callback] (node:net:1097:9)"," at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:73:8)"]} END RequestId: f3493148-071f-466d-94c7-d29a0d715640 REPORT RequestId: f3493148-071f-466d-94c7-d29a0d715640 Duration: 447.45 ms Billed Duration: 448 ms Memory Size: 128 MB Max Memory Used: 76 MB Init Duration: 231.52 ms

Request ID f3493148-071f-466d-94c7-d29a0d715640

profile picture
EXPERTE
gefragt vor 6 Monaten60 Aufrufe
1 Antwort
0

【以下的回答经过翻译处理】 目前Lambda不支持IPv6出站连接。您需要使用IPv4或创建一些双栈代理,将请求从v4转发到v6。

profile picture
EXPERTE
beantwortet vor 6 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen