简体   繁体   English

使用VPC中运行的Lambda提供的AWS开发工具包

[英]Using AWS SDK from Lambda running in VPC

I have a simple lambda function as follows 我有一个简单的lambda函数,如下所示

var AWS = require("aws-sdk");

exports.handler = (event, context, callback) => {

var ec2 = new AWS.EC2({region:'us-east-1'});
return ec2.describeRegions({}).promise()
.then(function(regionResponse) {
    console.log(regionResponse.Regions)
    callback(null, regionResponse.Regions);
})
.catch(
    function (err) {
        console.log({"error" : err});
        callback(err, null);
    }
)

}; };

I can run this function outside of a VPC successfully. 我可以在VPC外部成功运行此功能。

I create a VPC using the VPC wizard and create a VPC with a single public subnet and an Internet Gateway. 我使用VPC向导创建了VPC,并创建了具有单个公共子网和Internet网关的VPC。 I place the function in the VPC and give it an execution role with Lambda VPC Execution rights. 我将该功能放在VPC中,并赋予其具有Lambda VPC执行权的执行角色。 It now fails with a timeout, which I have set to 10 seconds (normal execution 1 sec) 现在它因超时而失败,我将其设置为10秒(正常执行为1秒)

What am I missing from my config that prevents the function from accessing the AWS SDK inside the VPC? 我的配置中缺少什么,阻止该功能访问VPC内的AWS开发工具包?

  1. You are putting callback after return statement. 您将回调放在return语句之后。 Of course it will never be executed because you returned from the function. 当然,它将永远不会执行,因为您是从函数返回的。

  2. If the subnet you are running the Lambda is not public or does not have NAT Gateway, it won't be able to connect to Internet, thus to AWS API's. 如果您正在运行Lambda的子网不是公共的或没有NAT网关,则它将无法连接到Internet,从而无法连接到AWS API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM