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:
Html
Example application.py
Python
Example requirements.txt
Html
Example Procfile
Html
Example .ebextensions/flask.config
Yaml
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
Step 4: Initialize Your Elastic Beanstalk Application
Navigate to your project directory and initialize the Elastic Beanstalk application.
Sh
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
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
Step 7: Configure HTTPS for Your Application
Obtain an SSL Certificate
-
Navigate to AWS Certificate Manager (ACM):
- Go to the AWS Certificate Manager Console.
-
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
-
Open Elastic Beanstalk Console:
- Navigate to the Elastic Beanstalk Console.
-
Select Your Environment:
- Select your environment.
-
Modify Environment Configuration:
- Click on "Configuration".
- Under the "Load balancer" category, click "Edit".
-
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.
-
Save Changes:
- Apply the changes and wait for the environment to update.
Update DNS Settings
Point your domain to your Elastic Beanstalk environment:
-
Get Your Elastic Beanstalk Environment URL:
- Note the URL of your Elastic Beanstalk environment (e.g.,
my-flask-app-env.elasticbeanstalk.com
).
- Note the URL of your Elastic Beanstalk environment (e.g.,
-
Log in to Your DNS Provider:
- Log in to the DNS management console of your domain registrar (e.g., GoDaddy, Namecheap, Cloudflare).
-
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
- Name:
-
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.