简体   繁体   中英

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. 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.

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. Should I use API gateway? I saw an article in AWS docs that seems to do something similar, but it doesn't seem very straightforward. Is there an easier way through requests or boto3 or something alike? 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). For example when using JavaScript in your browser to call the 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 .

The easier way includes adding a lambda invocation permissions to EB instance profile. Assuming that you use default profile aws-elasticbeanstalk-ec2-role , you can add the following Inline Policy into it:

{
    "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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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