Skip to content

How to deploy Flask app on aws lambda?

0

I am using flask to create instagram analytics app. Scrapping part is done by python module "instaloader". So in folder structure we have app.py file(containing python code) , session folder(stores session info whenever you are logging into instagram), .env file which contains 4 variables(insta_username ,password , 2 proxy varibles). I want to integrate this with wix.com as frontend, so this flask application should serve as an api. I tried zappa for lambda deployment but unfortunately that didn't work(thrown some internal server error 500). If someone knows where to find guidance for this or proper steps to follow i would be really grateful

asked 2 years ago2.4K views

3 Answers
1
Accepted Answer

To deploy your Flask app on AWS Lambda and integrate it with Wix.com as a frontend, you can follow these steps:

  1. Prepare your Flask application:

    • Ensure your app.py file contains all the necessary Flask routes and logic.
    • Make sure your requirements.txt file includes all the required dependencies, including Flask and instaloader.
  2. Set up AWS Lambda:

    • Create a new Lambda function in the AWS Console.
    • Choose Python as the runtime environment (use the version that matches your local development environment).
  3. Package your application:

    • Create a deployment package that includes your app.py, the session folder, and any other necessary files.
    • Install the required dependencies in a separate directory and include them in your package.
  4. Configure environment variables:

    • In the Lambda function configuration, set up environment variables for your .env file contents (insta_username, password, and the two proxy variables).
  5. Use AWS API Gateway:

    • Create a new API in API Gateway.
    • Set up routes that correspond to your Flask app's endpoints.
    • Connect these routes to your Lambda function.
  6. Deploy your application:

    • Upload your deployment package to the Lambda function.
    • Deploy your API Gateway configuration.
  7. Integrate with Wix:

    • Use the API Gateway endpoint URL in your Wix frontend to make requests to your Flask app.

Regarding the Zappa deployment issue, internal server error 500 usually indicates a problem with the application code or configuration. You might want to check your Zappa settings, ensure all dependencies are correctly specified, and review your Lambda function logs for more detailed error information.

For additional guidance, you can refer to AWS documentation on deploying Flask applications to Lambda, or consider using the AWS Serverless Application Model (SAM) for a more streamlined deployment process. Remember to handle CORS (Cross-Origin Resource Sharing) if your Wix frontend will be making requests from a different domain.
Sources
flask python app using sqlalchemy | AWS re:Post
Instrumenting web frameworks deployed to serverless environments - AWS X-Ray

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Thanks for answering! How do i handle sessions? I wanted to store sessions in s3 and whenever required with post method lambda will retrieve that session info.

1

by using the Lambda Web Adapter, which serves as a universal adapter for Lambda Runtime API and HTTP API. It allows developers to package familiar HTTP 1.1/1.0 web applications, such as Express.js, Next.js, Flask, SpringBoot, or Laravel, and deploy them on AWS Lambda. This replaces the need to modify the web application to accommodate Lambda’s input and output formats, reducing the complexity of adapting code to meet Lambda’s requirements.

You can use this sample project to get started, and this blog to follow along

answered 2 years ago

0

Hi,

You may want to follow the guidance of this article to run Flask in Lambda: https://www.cloudtechsimplified.com/run-python-flask-in-aws-lambda/

Also, this video is interesting: https://www.youtube.com/watch?v=snDW-5n01ak

Best,

Didier

EXPERT

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.