通过与S3交互的Elastic Beanstalk部署Flask应用 [英] Deploying Flask app to via Elastic Beanstalk which interacts with S3

查看:61
本文介绍了通过与S3交互的Elastic Beanstalk部署Flask应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Flask应用,看起来像这样:

I have a Flask app which looks like this:

from flask import Flask
import boto3

application = Flask(__name__)

@application.route("/")
def home():
    return "Server successfully loaded"

@application.route("/app")
def frontend_from_aws():
    s3 = boto3.resource("s3")
    frontend = s3.Object(bucket_name = "my_bucket", key = "frontend.html")
    return frontend.get()["Body"].read()

if __name__ == "__main__":
    application.debug = True
    application.run()

当我在本地测试时,一切工作都很好,但是当我将应用程序部署到Elastic Beanstalk时,第二个端点给出了内部服务器错误:

Everything works perfectly when I test locally, but when I deploy the app to Elastic Beanstalk the second endpoint gives an internal server error:

服务器遇到内部错误,无法完成您的请求.服务器超载或应用程序错误.

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

尽管我不确定自己会在哪里看,但在日志中没有看到任何令人担忧的内容.有什么想法吗?

I didn't see anything alarming in the logs, though I'm not completely sure I'd know where to look. Any ideas?

更新:作为一项测试,我将frontend.html移到了另一个存储桶,并相应地修改了"/app"端点,但它神秘地工作正常.因此,显然这与原始存储桶的设置有关.有人知道正确的设置是什么吗?

Update: As a test, I moved frontend.html to a different bucket and modified the "/app" endpoint accordingly, and mysteriously it worked fine. So apparently this has something to do with the settings for the original bucket. Does anybody know what the right settings might be?

推荐答案

我找到了一个快速而肮脏的解决方案:IAM策略(AWS控制台->身份和访问管理->策略).有一个名为AmazonS3FullAccess的现有策略,在将aws-elasticbeanstalk-ec2-role附加到该策略后,我的应用程序便可以随意读写S3.我猜想通过创建自定义角色和策略可以实现更细微的访问管理,但这对我来说已经足够了.

I found a quick and dirty solution: IAM policies (AWS console -> Identity & Access Management -> Policies). There was an existing policy called AmazonS3FullAccess, and after I attached aws-elasticbeanstalk-ec2-role to it my app was able to read and write to S3 at will. I'm guessing that more subtle access management can be achieved by creating custom roles and policies, but this was good enough for my purposes.

这篇关于通过与S3交互的Elastic Beanstalk部署Flask应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆