【问题标题】:A request from a private subnet to a public subnet从私有子网到公共子网的请求
【发布时间】:2020-12-11 18:05:42
【问题描述】:

我的 VPC 有两个子网 - 公有和私有。

公有子网包含 ECS Fargate 任务:

Network mode -> awsvpc
ENI Id -> eni-xxxxxxxxxx
Private IP -> 10.0.0.36
Public IP -> 34.243.XXX.XXX

私有子网包含一个 Lambda 函数。

当我尝试从 IP 10.0.0.36 的 Lambda 连接时,出现错误:

{
  "errorType":"Error",
  "errorMessage":"getaddrinfo ENOTFOUND 10.0.0.36:3000",
  "trace":[
    "Error:getaddrinfo ENOTFOUND 10.0.0.36:3000",
    "at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)"
  ]
}

如何纠正?

附言。 Lambda 不需要 Internet 访问权限。

加法:

我的 Lambda 的简化代码:

const http = () => {
    const body = '{"vehicles":[{"id":0,"profile":"driving-car","start":[32.41,34.784],"end":[32.41,34.784]}],"jobs":[{"id":0,"location":[32.41,34.784]},{"id":1,"location":[32.480,34.835]}],"options":{"g":true}}';
    const options = {
        hostname: '10.0.0.36:3000',
        path: '/',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json; charset=utf-8',
            'Content-Length': Buffer.byteLength( body ),
        }
    };
    return new Promise(resolve => {
        const req = require('http' ).request(options, res => {
            res.setEncoding('utf8'); 
            var str = '';
            res.on('data', chunk => str += chunk);
            res.on('end', () => resolve(str));
        });
        req.write(body);
        req.end();
    });
};
exports.handler = async (event) => {
    const response = {
        statusCode: 200,
        body: await http(),
    };
    return response;
};

【问题讨论】:

  • Fargate任务的网卡上的安全组配置是什么?它可能不允许在该端口上进行入站访问。
  • 一切似乎都很好。 HTTP|TCP|80|0.0.0.0/0;自定义 TCP|TCP|3000|0.0.0.0/0
  • 同时,当我不是通过 Lambda 而是直接从 Internet 运行 Task 时,一切正常。
  • Lambda 函数是连接到任务的公有 IP 地址还是私有 IP 地址?
  • Lambda 敲入私有 IP。我已将 Lambda 代码添加到问题中。

标签: amazon-web-services aws-lambda amazon-ecs amazon-vpc


【解决方案1】:

Making HTTP requests with Node.js 显示了一些示例代码:

const https = require('https')
const options = {
  hostname: 'whatever.com',
  port: 443,
  path: '/todos',
  method: 'GET'
}

const req = https.request(options, res => {
  console.log(`statusCode: ${res.statusCode}`)

  res.on('data', d => {
    process.stdout.write(d)
  })
})

req.on('error', error => {
  console.error(error)
})

req.end()

我注意到他们已将 port: 列为单独的字段,而不是将其添加到 hostname

但是,您的代码显示:

hostname: '10.0.0.36:3000'

尝试将hostnameport 作为单独的字段提供。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 2020-11-24
    • 1970-01-01
    • 2019-05-30
    相关资源
    最近更新 更多