Deploying Flask app to via Elastic Beanstalk which interacts with S3

拟墨画扇 提交于 2021-01-29 02:26:02

问题


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()

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?


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?


回答1:


Did you set up your AWS credentials on your Elastc Beanstalk instance as they are on your local machine (i.e. in ~/.aws/credentials)?




回答2:


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.



来源:https://stackoverflow.com/questions/33512614/deploying-flask-app-to-via-elastic-beanstalk-which-interacts-with-s3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!