简体   繁体   English

如何在专用 EC2 服务器上执行 Amazon Lambda 功能?

[英]How to execute Amazon Lambda functions on dedicated EC2 server?

I am currently developing the backend for my app based on Amazon Web Services.我目前正在基于 Amazon Web 服务为我的应用程序开发后端。 I pretended to use DynamoDB to store the user's data, but finally opted for MongoDB, which I have already installed in my EC2 instance.我假装使用 DynamoDB 来存储用户的数据,但最终选择了 MongoDB,我已经在我的 EC2 实例中安装了它。

I have some code written in Python to update/query... the DB, so that when a Cognito event triggers my lambda function, this code is directly executed on my instance so I can access my DB.我在 Python 中编写了一些代码来更新/查询...数据库,这样当 Cognito 事件触发我的 lambda function 时,这段代码直接在我的实例上执行,因此我可以访问我的数据库。 Any ideas how can I accomplish this?任何想法我怎样才能做到这一点?

As mentioned by Gustavo Tavares , "the whole point of lambda is to run code without the need to deploy EC2 instances" .正如Gustavo Tavares所提到的, “lambda 的全部意义在于无需部署 EC2 实例即可运行代码” And you do not have to put your EC2 with database to "public" su.nets for Lambda to access them.而且您不必将带有数据库的 EC2 放入“公共” su.nets 以供 Lambda 访问它们。 Actually, you should never do that.实际上,你永远不应该那样做。

When creating/editing Lambda configuration you may select to run it in any of you VPCs (Configuration -> Advanced Settings -> VPC).创建/编辑 Lambda 配置时,您可以 select 在任何 VPC 中运行它(配置 -> 高级设置 -> VPC)。 Then select Su.net(s) to run your Lambda in. This will create ENIs (Elastic Network Interface) for the virtual machines you Lambdas will run on.然后 select Su.net(s) 在其中运行您的 Lambda。这将为您运行 Lambda 的虚拟机创建 ENI(弹性网络接口)。

Your su.nets must have Routing/ACL configured to access the su.nets where Database resides.您的 su.net 必须配置路由/ACL 才能访问数据库所在的 su.net。 At least one of the SecurityGroups associated with Lambda must also have Outbound traffic allowed to the Database su.net on appropriate ports (27017).至少与 Lambda 关联的 SecurityGroups 之一还必须允许出站流量在适当的端口 (27017) 上到达数据库 su.net。

Since you mentioned that your Lambdas are "back-end" then you should probably put them in the same "private" su.nets as your MongoDB and avoid any access/routing headache.由于您提到您的 Lambda 是“后端”,那么您应该将它们放在与您的 MongoDB 相同的“私有” su.net 中,并避免任何访问/路由问题。

One way to accomplish this is to give the Lambda a SAM Template, then use sam local invoke inside of the EC2 instance to execute locally.实现此目的的一种方法是为 Lambda 提供一个 SAM 模板,然后在 EC2 实例内部使用sam local invoke在本地执行。

OK BUT WHY OH WHY WOULD ANYONE DO THIS?好的,但是为什么哦,为什么会有人这样做?

If your Lambda requires access to both a VPC and the Inte.net, and doesn't use a lot of memory and doesn't really require scalability, and you already wrote the code (*) , it's actually 10x cheaper(**) and higher-performing to launch a t3.nano EC2 Spot Instance on a public su.net than to add a NAT Gateway to the Lambda function.如果您的 Lambda 需要访问 VPC 和 Inte.net,并且不使用大量 memory 并且实际上不需要可扩展性,并且您已经编写了代码 (*) ,它实际上便宜 10 倍(**)在公共 su.net 上启动t3.nano EC2 Spot 实例比将 NAT 网关添加到 Lambda function 性能更高。

(*) if you have not written the code yet, don't even bother to make it a Lambda. (*) 如果你还没有编写代码,甚至不用费心把它弄成 Lambda。

(**) 10x cheaper as in $3 vs $30, so this really only applies to hobbyist projects on a shoestring budget. (**) 3 美元比 30 美元便宜 10 倍,所以这实际上只适用于预算有限的业余爱好者项目。 Don't do this at work, because the cost of engineers' time to manage and maintain an EC2 instance will far exceed $30/month over the long term.不要在工作中这样做,因为从长远来看,工程师管理和维护 EC2 实例的时间成本将远远超过 30 美元/月。

If you want Lambda to execute code on your ec2-instances you'll need to use the SDK for the language you're writing your lambda in. Then you can simply use the AWS API to run commands on your EC2 instance.如果您希望 Lambda 在您的 ec2 实例上执行代码,您需要使用 SDK 作为您编写 lambda 的语言。然后您可以简单地使用 AWS API 在您的 EC2 实例上运行命令。

See: http://docs.aws.amazon.com/systems-manager/latest/userguide/run-command.html请参阅: http://docs.aws.amazon.com/systems-manager/latest/userguide/run-command.html

I think you misunderstood the idea of AWS lambda.我认为您误解了 AWS lambda 的概念。

The whole point of lambda is to run code without the need to deploy EC2 instances. lambda 的全部意义在于无需部署 EC2 实例即可运行代码。 You upload the code and the infrastructure is provisioned on the fly.您上传代码,然后即时配置基础设施。 If your application does not need the infrastructure anymore (after a brief period), it vanishes and you will not be charged for the idle time.如果您的应用程序不再需要基础设施(在短暂的一段时间后),它就会消失,并且您不会为空闲时间付费。 If you need it again a new infrastructure is provisioned.如果您再次需要它,则会提供新的基础设施。

If you have a service, like your MongoDB, running in EC2 instances your lambda functions can access it like any other code.如果你有一个服务,比如你的 MongoDB,在 EC2 实例中运行,你的 lambda 函数可以像访问任何其他代码一样访问它。 You just need configure your lambda code to connect to the EC2 instance, like you would be doing if your database were installed in any other inte.net faced server.您只需配置您的 lambda 代码以连接到 EC2 实例,就像如果您的数据库安装在任何其他面向 inte.net 的服务器上一样。

For example: You can put your MongoDB server in a public su.net of your VPC and assign an elastic IP for your server.例如:您可以将您的 MongoDB 服务器放在您的 VPC 的公共 su.net 中,并为您的服务器分配一个弹性 IP。 In your Python lambda code you configure your driver to connect to this elastic IP and update the database.在您的 Python lambda 代码中,您将驱动程序配置为连接到此弹性 IP 并更新数据库。

It will work like every service were deployed in different servers across inte.net: Cognito connect to Lambda functions across inte.net and then the python code deployed in lambda connect to your MongoDB across inte.net.它的工作方式就像每项服务都部署在 inte.net 的不同服务器上:Cognito 连接到 Lambda 跨 inte.net 功能,然后部署在 lambda 中的 python 代码跨 inte.net 连接到您的 MongoDB。

If I can give you an advice, try DynamoDB a little more.如果我可以给你建议,请多尝试一下 DynamoDB。 With DynamoDB it will be even more simple to make all this work, because you will not need to configure a public su.net and request an elastic IP. And the API for DynamoDB is not very different of the MongDB API.使用 DynamoDB,使所有这些工作变得更加简单,因为您不需要配置公共 su.net 和请求弹性 IP。DynamoDB 的 API 与 MongDB 的 API 没有太大区别。

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

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