从私有子网到 public 子网的请求
A request from a private subnet to a public subnet
我的 VPC 有两个子网 - public 和私有。
public 子网包含 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)"
]
}
如何纠正?
PS。 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;
};
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'
尝试将 hostname
和 port
作为单独的字段提供。
我的 VPC 有两个子网 - public 和私有。
public 子网包含 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)"
]
}
如何纠正?
PS。 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;
};
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'
尝试将 hostname
和 port
作为单独的字段提供。