简体   繁体   English

在 Python 中将 Elastic Beanstalk 与 AWS Lambda 集成的最佳方法

[英]best way to integrate Elastic Beanstalk with AWS Lambda in Python

The last days I've been trying to understand AWS' products enough to implement a semi-large app.最近几天,我一直在尝试充分了解 AWS 的产品,以便实施一个半大型应用程序。 My conclusion was that I should store my files in S3, perform heavy duty on Lambda and the app itself should be ran through Elastic Beanstalk.我的结论是,我应该将我的文件存储在 S3 中,在 Lambda 上执行重任务,应用程序本身应该通过 Elastic Beanstalk 运行。

So, what I'm trying to do now is to call my lambda functions inside my app script, but I'm not sure on how to proceed.所以,我现在要做的是在我的应用程序脚本中调用我的 lambda 函数,但我不确定如何继续。 Should I use API gateway?我应该使用 API 网关吗? I saw an article in AWS docs that seems to do something similar, but it doesn't seem very straightforward.在 AWS docs 上看到一篇文章似乎做了类似的事情,但它似乎不是很简单。 Is there an easier way through requests or boto3 or something alike?有没有更简单的方法通过请求或 boto3 或类似的东西? Any input would be appreciated.任何输入将不胜感激。

Thanks.谢谢。

The article you mentioned uses API gateway in front of lambda. This is the recommended way of invoking lambda anonymously (without aws credentials).你提到的文章在lambda前面使用API网关。这是匿名调用lambda的推荐方式(没有aws凭据)。 For example when using JavaScript in your browser to call the function.例如,在浏览器中使用 JavaScript 来调用 function。

However, if you want to invoke your lambda function from EB environment, ie, from a python running on an EB instance, then there is an easier way .但是,如果您想从 EB 环境调用您的 lambda function,即从运行在 EB 实例上的 python,那么有一个更简单的方法

The easier way includes adding a lambda invocation permissions to EB instance profile.更简单的方法包括向 EB 实例配置文件添加 lambda调用权限 Assuming that you use default profile aws-elasticbeanstalk-ec2-role , you can add the following Inline Policy into it:假设您使用默认配置文件aws-elasticbeanstalk-ec2-role ,您可以将以下内联策略添加到其中:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "lambda:InvokeFunction",
            "Resource": "*"
        }
    ]
}

This will allow any script that uses boto3 (or any other aws sdk) to invoke your function through IAM roles for EC2 .这将允许任何使用 boto3(或任何其他 aws sdk)的脚本通过EC2 的 IAM 角色调用您的 function。

Having this you can just use boto3 on EB instances as you would normally do it on your local workstation to invoke lambda. boto3 will know how to properly get the credentials from the profile , and no special action is required from you or your script.有了这个,您就可以像通常在本地工作站上那样在 EB 实例上使用boto3来调用boto3将知道如何从配置文件中正确获取凭据,并且您或您的脚本不需要执行任何特殊操作。

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

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