Open In App

How To Configure Flask API To Your Domain Name On AWS EC2?

In this article, We are going to create a Flask API on an EC2 Instant and we will learn how to configure it with a domain name using Gunicorn and nginx(NGINX). Flask is a web microframework, it’s a Python module that lets you develop web applications easily. A domain name is a string of text that maps to an alphanumeric IP address, used to access a website from client software. In plain English, a domain name is the text that a user types into a browser window to reach a particular website.

Configuring Flask API To Your Domain Name On AWS EC2: A Step-By-Step Guide

Step 1: Sign in to your AWS Console and search EC2, click on it.



Step 2: Navigate to EC2 Dashboard and then go to instances section.



Step 3: Check the status of the created Instance.

Step 4: Connect to the instance by clicking on instance id and click on connect button to connect instance.

Step 5: Create API using Flask framework and open your console of linu. Firstly check python version with the following command:

python --version

mkdir API

Make the created director as a current folder by running the following command:

cd API

Step 6: Now set Flask API environment , it’s a best practices to create a virtual environment for flask app and it isolated dependencies . to create virtual environment with the following command:

python3 -m venv venv

Activate virtual environment with the below command:

source venv/bin/activate

Step 7: Now install Flask package using pip (python package manager) with the following command:

pip install Flask

Step 8: Now create app.py file for write Flask code logic for application . here is command for this touch filename.py with the below command:

touch app.py

The following command make empty file with name app.py. Write a code in it open it in text editor and write below code:

nano app.py

Step 9: Now make a subdirectory named “templates” in main directory with below command:

mkdir templates && cd templates

touch welcome.html
nano welcome.html

Step 10: Quit to templates folder with below command and Now API is ready to run

cd ..

Step 6: Run The API with the following command:

python3 app.py

Step 11: Run API without port using gunicorn.

python3 -m pip install --upgrade pip

Now install gunicorn in virtual environment using the following command:

pip install gunicorn

gunicorn -w 4 -b 0.0.0.0:8080 app:app

Step 12: Configure EnginX (NGINX)

sudo amazon-linux-extras install nginx1

sudo nano /etc/nginx/conf.d/ctrlaltelite.conf

sudo systemctl restart nginx
gunicorn -w 4 -b 0.0.0.0:8080 app:app 

Step 13: Configure domain name with Route 53

Step 14: Fill details like domain name and description select public hosted zone and click on create host zone. It add in our hosted zone list.

Step 15: Now click on hosted zone name there are already two record are created and add to two more record there to create record click on create record and add details like name, select type ‘A’ and in value column add domain name ip address click on create record.

Step 16: Now we have to add type NS record traffic to our domain provider here I use bigrock as domain provider so I login to bigrock and click on name servers

Step 17: Configure our Flask API to domain name

sudo nano /etc/nginx/conf.d/ctrlaltelite.conf

Step 18: Restart enginX(NGINX) & Configure with gunicorn

sudo systemctl restart nginx
gunicorn -w 4 -b 0.0.0.0:8080 app:app

Step 19: Run API by Domain Name

Configuring Flask API And AWS EC2 – FAQs

What Is AWS EC2, And How Does It Relate To Hosting My Flask API?

Amazon Elastic Compute Cloud (EC2) is a web service offered by AWS that provides resizable compute capacity in the cloud. You can use EC2 instances to host your Flask API, making it accessible over the internet.

How Do I Acquire A Domain Name For My Flask API Hosted On AWS EC2?

You can register a domain name through domain registrars like GoDaddy, Namecheap, or AWS Route 53. Once registered, you’ll configure the DNS settings to point to your EC2 instance’s IP address.

What Is DNS, And Why Do I Need To Configure It For My Domain Name?

DNS (Domain Name System) is like the internet’s phonebook. It translates human-friendly domain names (e.g., example.com) into IP addresses that computers use to identify each other on the internet. Configuring DNS allows your domain name to resolve to your EC2 instance’s IP address.

What Steps Are Involved In Associating My Domain Name With My EC2 instance’s IP Address?

You need to create DNS records (usually an “A” record) in your domain registrar’s dashboard. This record should point to your EC2 instance’s public IP address. The exact steps depend on your registrar, but the process typically involves specifying the IP address in your domain’s DNS settings.


Article Tags :