AskHandle

AskHandle Blog

The Simplest Method to Deploy a Python Flask App on AWS

June 6, 2025Junjie Shi3 min read
  • Flask
  • Python
  • AWS Beanstalk
  • AskHandle

The Simplest Method to Deploy a Python Flask App on AWS Elastic Beanstalk

Deploying your Python Flask web application on Amazon Web Services (AWS) has never been easier with the use of AWS Elastic Beanstalk. AWS offers a comprehensive set of services, allowing you to launch your Flask app seamlessly to the web. This guide will walk you through the process step by step, ensuring a smooth deployment. For example, you can use this gude to deploy AskHandle widget as an independent web app on AWS.

Step 1: Prepare Your Flask App

Before diving into AWS, ensure your Flask app is ready for deployment. Verify that your app runs smoothly on your local machine. Ensure you have your dependencies listed in a requirements.txt file, and that your app follows best practices for security and performance.

Here is an example structure of your Flask app:

text
1my-flask-app/
2├── application.py
3├── requirements.txt
4├── Procfile
5├── .ebextensions/
6│   └── flask.config
7└── static/
8    └── images/
9        └── favicon.png
10    └── css/
11        └── styles.css
12    └── js/
13        └── scripts.js

Example application.py

python
1from flask import Flask, jsonify, send_file
2
3app = Flask(__name__, static_folder='static')
4
5@app.route('/')
6def index():
7    return send_file('index.html')
8
9@app.route('/api/message')
10def message():
11    return jsonify({"message": "Hello from Flask!"})
12
13@app.route('/api/config')
14def config():
15    # Logic to securely handle and return configuration data
16    return jsonify({
17        "widgetType": "messenger",
18        "token": "your_token_here"
19    })
20
21if __name__ == '__main__':
22    app.run(debug=True, host='0.0.0.0')

Example requirements.txt

text
1Flask==2.1.1
2Werkzeug==2.0.3
3gunicorn==20.1.0

Example Procfile

text
1web: gunicorn application:app

Example .ebextensions/flask.config

yaml
1option_settings:
2  aws:elasticbeanstalk:container:python:
3    WSGIPath: application:app

Step 2: Set Up an AWS Account

If you haven't already, head over to the AWS website and create an account. AWS offers a free tier that allows new users to explore and experiment with their services. Once your account is set up, navigate to the AWS Management Console.

Step 3: Install the AWS Elastic Beanstalk CLI

Install the AWS Elastic Beanstalk Command Line Interface (EB CLI) to manage your Elastic Beanstalk applications.

sh
1pip install awsebcli

Step 4: Initialize Your Elastic Beanstalk Application

Navigate to your project directory and initialize the Elastic Beanstalk application.

sh
1cd my-flask-app
2eb init -p python-3.7 my-flask-app

Follow the prompts to configure your AWS credentials and select the appropriate region.

Step 5: Create an Elastic Beanstalk Environment

Create an environment for your Flask application.

sh
1eb create my-flask-app-env

This command will set up an Elastic Beanstalk environment for your application.

Step 6: Deploy Your Application

Deploy your Flask application to the Elastic Beanstalk environment.

sh
1eb deploy

Step 7: Configure HTTPS for Your Application

Obtain an SSL Certificate

  1. Navigate to AWS Certificate Manager (ACM):

  2. Request a Public Certificate:

    • Click on "Request a certificate".
    • Choose "Request a public certificate".
    • Enter your domain name (e.g., example.com and *.example.com for a wildcard certificate).
    • Follow the prompts to complete the request using DNS validation.

Configure HTTPS in Elastic Beanstalk

  1. Open Elastic Beanstalk Console:

  2. Select Your Environment:

    • Select your environment.
  3. Modify Environment Configuration:

    • Click on "Configuration".
    • Under the "Load balancer" category, click "Edit".
  4. Add HTTPS Listener:

    • Scroll down to the "Listeners" section.
    • Add an HTTPS listener on port 443.
    • For "SSL certificate", select the SSL certificate you requested from ACM.
  5. Save Changes:

    • Apply the changes and wait for the environment to update.

Update DNS Settings

Point your domain to your Elastic Beanstalk environment:

  1. Get Your Elastic Beanstalk Environment URL:

    • Note the URL of your Elastic Beanstalk environment (e.g., my-flask-app-env.elasticbeanstalk.com).
  2. Log in to Your DNS Provider:

    • Log in to the DNS management console of your domain registrar (e.g., GoDaddy, Namecheap, Cloudflare).
  3. Create a CNAME Record:

    • Create a new CNAME record pointing to your Elastic Beanstalk environment URL.
    • For example:
      • Name: www (or @ for the root domain if your provider allows)
      • Type: CNAME
      • Value: my-flask-app-env.elasticbeanstalk.com
  4. Save the DNS Record and wait for the changes to propagate.

Validate HTTPS Setup

Once the changes are applied, access your application using the HTTPS protocol. Verify that your website loads correctly without any security warnings or errors. You can use online tools like SSL Labs to check the SSL configuration.

Step 8: Monitor and Scale Your Application

Congratulations on successfully deploying your Flask app on AWS Elastic Beanstalk! Remember to monitor your app's performance and scale it as needed. AWS offers a range of services such as Elastic Load Balancing (ELB) and auto-scaling to help you manage and scale your app effectively.