How To Deploy Flask App On AWS EC2 Windows: A Step-by-Step Guide
Step 1: Create an EC2 Instance
- Log in to the AWS Management Console and navigate to the EC2 section.
- Click âLaunch instanceâ and choose a Windows Server AMI (Amazon Machine Image). Popular options include Windows Server 2019 or 2022.
- Select an instance type suitable for your appâs workload.
- Configure security groups to allow inbound traffic on ports 80 (HTTP) and any other ports your application needs.
- Launch your instance and wait for it to start.
Step 2: Connect to Your Instance
- Use Remote Desktop Protocol (RDP) to connect to your Windows instance. Get the public IP address or DNS name from the EC2 console and use your chosen RDP client (Remmina on Ubuntu).
Step 3: Install Python and Flask
- Download and install the Python installer from the official website https://www.python.org/downloads/windows/
- Open a PowerShell window on the EC2 instance.
- Install Flask using pip: âpip install Flaskâ
Step 4: Setup Your Flask Application
- Create a directory for your application (e.g., âC:\myflaskappâ).
- Inside the directory, create a file named âmain.pyâ with your Flask application code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, from Flask on Windows EC2!'
if __name__ == '__main__':
app.run()
Step 5: Install and Configure IIS
- Open Server Manager.
- The below screenshot illustrates about the server manager Dashbaord With Loal server configuration.
- Add Roles and Features
- Click Next>Next and go to Server Selection
- Install âWeb Server (IIS)â and include the âCGIâ role service.
- Next go to Role Services
- Select CGI
- Click Add Features and Install
Step 6: Install wfastcgi
- From PowerShell, install it: âpip install wfastcgiâ
- Then run the following command to enable it in the current environment.
wfastcgi-enable
Output:
Copy the path from the output
Step 7: Create Web Configuration File
- Create a file web.config next to your main flask app file âapp.pyâ.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="Python FastCGI"
path="*"
verb="*"
modules="FastCgiModule"
scriptProcessor="c:\users\Adminstrator\desktop\demoflask\env\scripts\python.exe|c:\users\Adminstrator\desktop\demoflask\env\lib\site-packages\wfastcgi.py"
resourceType="Unspecified"
requireAccess="Script" />
</handlers>
</system.webServer>
<appSettings>
<!-- Required settings -->
<add key="WSGI_HANDLER" value="main.app" />
<add key="PYTHONPATH" value="C:\Users\Adminstrator\Desktop\DemoFlask\myProject" />
</appSettings>
</configuration>
Step 8: Configure IIS Handler
- Open IIS Manager.
- Expand your âSitesâ, right-click on âDefault Web Siteâ, and select âAdd Application.â
- Set an alias (e.g., âmyflaskappâ) and the physical path to your applicationâs directory.
- Go to âHandler Mappings,â and add a Module Mapping:
Request Path: '*'
Module: 'FastCgiModule'
Executable: 'C:\Python3x\python.exe|C:\Adminstrator\root\scripts\wfastcgi.py' (Adjust for your Python installation path)
Step 9: Test Your Application
- Click browse from the right pane to browse your site locally.
- Access your appâs URL using your EC2 instanceâs public IP address and the alias you set (e.g., âhttp://ec2-public-ip/myflaskappâ)
Deploy Flask App on AWS EC2 Windows
Do you want to host a Windows-based Python Flask application on Amazon Web Services (AWS) Elastic Compute Cloud (EC2)? Flask is a frequently utilized framework for building web apps, APIs, and prototypes because of its ease of use and versatility. AWS EC2 provides virtual machines that are scalable, allowing you complete control over your deployment environment. Using Windows and Internet Information Services (IIS) as the web server, weâll take you step-by-step through the process of deploying a Flask application on AWS EC2.