> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploying the sample application on EC2 instance

> Guide to deploy, run, and verify a Flask sample application on an Amazon EC2 instance, including setup, dependencies, security group configuration, and noting missing RDS database connectivity.

Hello and welcome to this lesson.

In this guide you'll deploy a small Flask-based sample application to an Amazon EC2 instance, run it, and verify the app is reachable from a browser. This lesson demonstrates the application lifecycle on EC2 (installation, running, and basic verification). Database connectivity (RDS configuration) is intentionally excluded here — the app will start, but form submissions will fail until an RDS endpoint and credentials are configured.

What you'll do

* Launch an EC2 instance to host a Flask app.
* Connect to the instance and install packages.
* Clone and run the sample app.
* Verify the web app is reachable externally.
* Attempt a data submission (expected to fail due to missing DB configuration).

Why this matters (SEO keywords): EC2 deploy Flask app, run Flask on EC2, EC2 security group open port 5000, connect EC2 via EC2 Instance Connect, install git and pip on Amazon Linux.

***

## Step 1 — Launch an EC2 instance

1. Open the EC2 console (use the AWS console search bar).
2. Click **Launch Instance** and give the instance a name, for example: `User Information App`.
3. Choose the Amazon Linux AMI.
4. Create or select a security group. For this demo we allow SSH from anywhere, but in production restrict SSH to your IP address only.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/VCFuPHSNLDaVdMaA/images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/aws-ec2-launch-instance-ami-summary.jpg?fit=max&auto=format&n=VCFuPHSNLDaVdMaA&q=85&s=950535731873baf06545e8c488895dfe" alt="A screenshot of the AWS EC2 &#x22;Launch an instance&#x22; console showing the Name and tags field, Application and OS Images (AMIs) selection, and quick-start OS tiles. The right-hand Summary panel lists details like 1 instance, Amazon Linux AMI, t2.micro instance type, and a &#x22;Launch instance&#x22; button." width="1920" height="1080" data-path="images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/aws-ec2-launch-instance-ami-summary.jpg" />
</Frame>

<Callout icon="warning" color="#FF6B6B">
  For security, do not open SSH (port 22) to 0.0.0.0/0 in production. Restrict SSH inbound access to specific IP addresses or your administrator network.
</Callout>

If you encounter a key-pair prompt and haven't created one, for this demo you can select "Proceed without key pair" (not recommended for production). Click **Launch Instance** and wait for the instance state to become **running**. Then select the instance to view its details.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/VCFuPHSNLDaVdMaA/images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/aws-ec2-user-information-app-t2micro.jpg?fit=max&auto=format&n=VCFuPHSNLDaVdMaA&q=85&s=d9d5040b38bbea8a85e7c65ce582bad3" alt="Screenshot of the AWS EC2 Instances page. It shows one running t2.micro instance named &#x22;user-information-app&#x22; with its instance ID and public IPv4 address displayed." width="1920" height="1080" data-path="images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/aws-ec2-user-information-app-t2micro.jpg" />
</Frame>

***

## Step 2 — Connect to the EC2 instance

1. Select the instance in the console and click **Connect**.
2. Use **EC2 Instance Connect** to open a browser-based terminal. For Amazon Linux the username is typically `ec2-user`.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/VCFuPHSNLDaVdMaA/images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/aws-ec2-connect-dialog-ssh-ec2-user.jpg?fit=max&auto=format&n=VCFuPHSNLDaVdMaA&q=85&s=cd86292f49f6808237443485a538de73" alt="A screenshot of the AWS EC2 console &#x22;Connect to instance&#x22; page showing EC2 Instance Connect options, the instance ID, public IPv4 address, and a default username (ec2-user). The dialog includes tabs for Session Manager, SSH client and an orange &#x22;Connect&#x22; button." width="1920" height="1080" data-path="images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/aws-ec2-connect-dialog-ssh-ec2-user.jpg" />
</Frame>

In the browser terminal, become root and switch to the home directory:

```bash theme={null}
sudo su
cd ~
```

***

## Step 3 — Get the application code from GitHub

On the repository page click **Code** and copy the HTTPS URL for cloning.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/VCFuPHSNLDaVdMaA/images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/github-aws-rds-clone-https-readme.jpg?fit=max&auto=format&n=VCFuPHSNLDaVdMaA&q=85&s=6062d01b7c3048147c1b0c4c811e7d72" alt="A screenshot of a GitHub repository page for &#x22;aws-rds&#x22; showing the Clone dialog with the HTTPS URL copied. The README for &#x22;user-information-app-rds&#x22; is visible and the repo language breakdown (Python and HTML) appears on the right." width="1920" height="1080" data-path="images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/github-aws-rds-clone-https-readme.jpg" />
</Frame>

Try cloning the repo. If `git` is not installed you will see an error like:

```bash theme={null}
[root@ip-172-31-19-201 ~]# git clone https://github.com/kodekloudhub/aws-rds.git
bash: git: command not found
```

Install Git and pip (python3-pip on Amazon Linux), then install a PostgreSQL client driver (psycopg2-binary) which the app will use later when you configure an RDS backend:

```bash theme={null}
sudo yum install -y git
sudo yum install -y python3-pip
pip3 install psycopg2-binary
```

***

## Step 4 — Clone the repo and inspect the app

Clone the repository and change into the app folder:

```bash theme={null}
git clone https://github.com/kodekloudhub/aws-rds.git
cd aws-rds/db-app
ls -lrt
```

You should see the main application file and the templates folder:

```bash theme={null}
total 4
-rw-r--r--. 1 root root 3085 Aug 31 22:08 app.py
drwxr-xr-x. 2 root root   65 Aug 31 22:09 templates
```

***

## Step 5 — Install Python dependencies and attempt to start the app

Try running the Flask app:

```bash theme={null}
python3 app.py
```

If Flask is not installed you'll see an import error:

```text theme={null}
Traceback (most recent call last):
  File "/root/aws-rds/db-app/app.py", line 1, in <module>
    from flask import Flask, render_template, request
ModuleNotFoundError: No module named 'flask'
```

The repository includes a requirements.txt file (in this repository it’s inside the `templates` folder). Install the required Python packages:

```bash theme={null}
cd templates
pip3 install -r requirements.txt
```

Typical packages to be installed include Flask, PyMySQL/Psycopg2, Jinja2, and Werkzeug. You may see a warning about running pip as root.

<Callout icon="lightbulb" color="#1CB2FE">
  Tip: For cleaner dependency management, create and activate a Python virtual environment (venv) instead of installing packages globally. This prevents permission conflicts and keeps dependencies isolated.
</Callout>

***

## Step 6 — Start the Flask application

Return to the app folder and start the app:

```bash theme={null}
cd ..
python3 app.py
```

You should see the Flask development server start and bind to port 5000:

```text theme={null}
 * Serving Flask app 'app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://172.31.19.201:5000
Press CTRL+C to quit
```

<Callout icon="warning" color="#FF6B6B">
  The built-in Flask server is for development and testing only. For production, use a WSGI server such as Gunicorn or uWSGI and place it behind a reverse proxy (Nginx, Apache).
</Callout>

***

## Step 7 — Open port 5000 in the instance security group

By default the security group created during instance launch may only allow SSH (22), HTTP (80), and HTTPS (443). Port 5000 is not open, so the Flask app won't be accessible externally until you add an inbound rule.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/VCFuPHSNLDaVdMaA/images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/ec2-launch-wizard5-sg-open-ports.jpg?fit=max&auto=format&n=VCFuPHSNLDaVdMaA&q=85&s=d043c01da895705f53049b6a79569379" alt="A screenshot of the AWS EC2 console showing the security group &#x22;launch-wizard-5&#x22; (sg-0fef0dcd096356b03) details. The inbound rules list shows SSH (port 22), HTTPS (443) and HTTP (80) open to 0.0.0.0/0." width="1920" height="1080" data-path="images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/ec2-launch-wizard5-sg-open-ports.jpg" />
</Frame>

Edit the security group's inbound rules and add a rule allowing TCP port 5000 from your IP (or 0.0.0.0/0 for a quick demo). Save the changes.

Recommended inbound rules (example):

| Type       | Protocol | Port range | Source                         | Use case                            |
| ---------- | -------- | ---------- | ------------------------------ | ----------------------------------- |
| SSH        | TCP      | 22         | Your IP (e.g., 203.0.113.4/32) | Secure shell access (restrict this) |
| HTTP       | TCP      | 80         | 0.0.0.0/0                      | Web traffic (optional)              |
| HTTPS      | TCP      | 443        | 0.0.0.0/0                      | Encrypted web traffic (optional)    |
| Custom TCP | TCP      | 5000       | Your IP or 0.0.0.0/0 (demo)    | Flask dev server port for testing   |

***

## Step 8 — Access the application in a browser

Copy the instance's public IPv4 address from the EC2 console and open the Flask app in a browser:

http\://\<PUBLIC\_IP>:5000

You should see the "User Details" form served by the Flask app:

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/VCFuPHSNLDaVdMaA/images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/user-details-ip-364252128-port5000.jpg?fit=max&auto=format&n=VCFuPHSNLDaVdMaA&q=85&s=6e1f9a2470c9e0daca881257ace25514" alt="A web browser shows a centered &#x22;User Details&#x22; form with fields for Name, Email, Country and a green &#x22;Submit&#x22; button. The page is loaded from an IP address (3.64.252.128:5000) with several tabs open." width="1920" height="1080" data-path="images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/user-details-ip-364252128-port5000.jpg" />
</Frame>

***

## Step 9 — Submit data (expected failure)

The app displays a form to collect user details. If you submit the form now, the app will likely fail to connect to a database and return an error like "Failed to connect to the database." This is expected — you have not yet configured RDS connection details (endpoint, port, username, password, database name) or the necessary network access (VPC, security group, routing).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/VCFuPHSNLDaVdMaA/images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/failed-db-connection-ec2-ip-364252128.jpg?fit=max&auto=format&n=VCFuPHSNLDaVdMaA&q=85&s=6e6377da25a8eecb4b7925683f25b68b" alt="A browser window showing a mostly blank page with the message &#x22;Failed to connect to the database&#x22; in the top-left. The address bar shows 3.64.252.128:5000/submit and several AWS/EC2-related tabs are open." width="1920" height="1080" data-path="images/AWS-RDS/RDS-Architecture-and-Concepts/Deploying-the-sample-application-on-EC2-instance/failed-db-connection-ec2-ip-364252128.jpg" />
</Frame>

***

## Next steps — Connect the app to RDS (not covered in this lesson)

To complete the deployment and persist form submissions you will need to:

* Create an RDS instance (MySQL/Postgres) or use an existing database.
* Provide the application with the database endpoint, port, username, password, and database name (edit config or environment variables in the app).
* Ensure network connectivity: place EC2 and RDS in the same VPC/subnet or allow routing, and update security group inbound/outbound rules to permit traffic between EC2 and RDS.
* Re-test the form submission and verify data is inserted into the database.

Additional resources and references

* [Amazon EC2 documentation](https://docs.aws.amazon.com/ec2/)
* [EC2 Instance Connect](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Connect-using-EC2-Instance-Connect.html)
* [Flask documentation](https://flask.palletsprojects.com/)
* [AWS RDS documentation](https://docs.aws.amazon.com/rds/)
* [Python virtual environments (venv)](https://docs.python.org/3/library/venv.html)

That’s it for this lesson — in the next lesson we will configure the RDS database and connect the app so form submissions persist successfully.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/aws-rds/module/e87c8f86-0a01-4b91-ad95-23e570a8bb2e/lesson/64436ab4-aa3b-4f53-8092-70d4d3dbe4a6" />
</CardGroup>
