Dan*_*ill 4 amazon-web-services amazon-vpc aws-lambda
我正在尝试让 Lambda 在公共子网内运行以与互联网进行通信。我可以让 Lambda 在没有 VPC 的情况下访问 www.google.com(文档称其在幕后运行),但如果我在 VPC 中运行 Lambda,则无法访问。
重现步骤:
我尝试过对此方法的修改,但没有取得任何成功(例如,实际上将子网与 vpc 关联,放松安全组和网络 ACL 上的所有设置)。
我最初尝试遵循一个公共文档和一个私人文档,但未能成功。
有任何想法吗?谢谢!- 丹
const http = require('http');
exports.handler = async (event) => {
return httprequest().then((data) => {
const response = {
statusCode: 200,
body: JSON.stringify(data),
};
return response;
});
};
function httprequest() {
return new Promise((resolve, reject) => {
const options = {
host: 'www.google.com',
path: '/',
port: 80,
method: 'GET'
};
const req = http.request(options, (res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error('statusCode=' + res.statusCode));
}
var body = [];
res.on('data', function(chunk) {
body.push(chunk);
});
res.on('end', function() {
try {
body = Buffer.concat(body).toString();
} catch(e) {
reject(e);
}
resolve(body);
});
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.end();
});
}
Run Code Online (Sandbox Code Playgroud)
AWS Lambda 函数在 VPC 中时永远不会分配公共 IP 地址,即使它们位于公共子网中也是如此。因此,它们在 VPC 中运行时永远无法直接访问 Internet。您必须将 Lambda 函数放置在具有 NAT 网关路由的私有子网中,以便它们能够从您的 VPC 内访问 Internet。
| 归档时间: |
|
| 查看次数: |
1152 次 |
| 最近记录: |