Site icon EASY2DIGITAL

Chapter 73 – Ultimate Guide to Deploy Flask App on AWS EC2

This piece you don’t want to miss out notably because it’s all about the ultimate, juicy, and creamy guide to deploying your Flask app on AWS for free. By the end of this piece, you can have one more resource to consider to leverage, and the most important thing is going to be free.

Table of Contents: Ultimate Guide to Deploy Flask App on AWS EC2

Create AWS EC2 Free Tier Account Using Ubuntu 22.4 LTS

As one of the best cloud eco-system worldwide, it’s surprisingly easy to sign up for a free AWS account by using email and adding one payment card. It’s just like as easy as eating straight up a piece of Miyazaki beef. After this, you just need to search EC2 and create an instance. More details are as follows and here we go.

Connect with SSH on Ubuntu

That’s. We have created an EC2 instance, in which the steps are thin, clear, and light. No difficulties! Then, it’s going to connect with your instance using your local machine immediately. As usual, I would take Mac OS as an example:

Both commands can be copied from the instance connect page. Just go and explore man! You might find more updated features than this article. AWS is just like a piece fattier and oilier of a tube compared to competitors.

Add a Flask App on EC2

Now you are in. Now it’s just a regular common way to set up Flask and deploy flask app.py. Here you go:

$ sudo apt-get update

$ sudo apt-get install python3-venv

// Create directory

$ mkdir lovely

$ cd lovely

// Create the virtual environment

$ python3 -m venv venv

// Activate the virtual environment

$ source venv/bin/activate

// Install Flask

$ pip install Flask

$ sudo nano app.py

// Add this to app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')

def lovely():

return 'Lovely!!!'

if __name__ == "__main__":

app.run()

Activate Gunicorn WSGI server to run Flask App

Running Flask in fact is running Werkzeugh’s development WSGI server, which forward requests from a web server.

Since Werkezeug is only for development, we have to use Gunicorn, which is a production-ready WSGI server-to-server Flask application. Here are the steps as follows and here we go!

Update system settings with Gunicorn

Systemd is a boot manager for Linux. We are using it to restart gunicorn if the EC2 restarts or reboots for some reason.

We create a <projectname>.service file in the /etc/systemd/system folder, and specify what would happen to gunicorn when the system reboots.

We will be adding 3 parts to systemd Unit file — Unit, Service, Install

$ sudo nano /etc/systemd/system/lovely.service

[Unit]

Description=Gunicorn instance for a simple lovely app

After=network.target

[Service]

User=ubuntu

Group=www-data

WorkingDirectory=/home/ubuntu/lovely

ExecStart=/home/ubuntu/lovely/venv/bin/gunicorn -b localhost:8000 app:app

Restart=always

[Install]

WantedBy=multi-user.target

$ sudo systemctl daemon-reload

$ sudo systemctl start lovely

$ sudo systemctl enable lovely

Run the Nginx web server to accept route requests to Gunicorn

Finally, we set up Nginx as a reverse proxy to accept the requests from the user and route it to gunicorn. 

$ sudo systemctl start nginx

$ sudo systemctl enable nginx

$ sudo nano /etc/nginx/sites-available/default

upstream lovely {

   server 127.0.0.1:8000;

}

# Some code above

location / {

   proxy_pass http://lovely;

}

Connect domain with EC2 Instance without using Route53

Tastes delicate and delicious right? Basically your app is already up online. However, things are not yet to go because we need to connect it with your domain.

Route 53 is one of the most popular solutions mentioned out there, but it’s not free which is opposite to our theme. In fact, it’s going to be free which is possible. Here is the way to go

And it’s done! So amazingly delicious

Set up SSL Certificate

Selecting an SSL ceritificate provider kind of blows you away as there are so many options out there no matter if it’s free or not. This piece I recommend is to use a free Alibabacloud SSL certificate to add to your app. What you are waiting for? Let’s go!

server {

    listen 80;

    server_name example.com;

    rewrite ^(.*)$ https://$server_name$1 permanent;

}

server {

    listen       443 ssl;

    server_name your domain;

    #  ssl on;

    ssl_certificate /etc/nginx/ssl/your digital certificate body file name.pem;

    ssl_certificate_key /etc/nginx/ssl/your digital certificate key file.key;

    ssl_session_timeout 5m;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;

    ssl_prefer_server_ciphers on;

    location / {

        root /data/web/;

        index index.jsp index.html index.htm;

    }

Full Scripts & Commands of Flask App Deployment on AWS EC2

If you are interested in Chapter 73 – Ultimate Guide to Deploy Flask App on AWS EC2, please subscribe to our newsletter by adding the message ‘Chapter 73 + AWS EC2’. We would send you the script immediately to your mailbox.

I hope you enjoy reading Chapter 73 – Ultimate Guide to Deploy Flask App on AWS EC2. If you did, please support us by doing one of the things listed below, because it always helps out our channel.

FAQ:

Q1: What is AWS EC2?

A: AWS EC2 is a web service that provides resizable compute capacity in the cloud. It allows you to quickly scale computing resources to meet your application demands.

Q2: How can I launch an EC2 instance?

A: To launch an EC2 instance, you need to first select an Amazon Machine Image (AMI), choose an instance type, configure your instance details, add storage, configure security groups, and launch the instance.

Q3: What is an Amazon Machine Image (AMI)?

A: An Amazon Machine Image (AMI) is a pre-configured template that contains the operating system, software, and configurations required to launch an EC2 instance.

Q4: What is an instance type?

A: An instance type determines the hardware of the host computer used for your instance. It defines the amount of memory, the number of virtual CPUs, and the network performance of your instance.

Q5: How can I manage the security of my EC2 instances?

A: You can manage the security of your EC2 instances by using security groups, which act as virtual firewalls to control inbound and outbound traffic.

Q6: What is Elastic IP?

A: Elastic IP is a static IPv4 address that you can allocate to your AWS account. You can associate an Elastic IP with your EC2 instance to ensure that it remains the same even if the instance is stopped or terminated.

Q7: How can I scale my EC2 instances?

A: You can scale your EC2 instances horizontally by adding or removing instances from an Auto Scaling group, or vertically by changing the instance type to one with more or less resources.

Q8: What is Amazon EBS?

A: Amazon Elastic Block Store (EBS) provides block-level storage volumes for use with EC2 instances. It allows you to create, attach, and detach persistent storage to your instances.

Q9: Can I use my own Windows Server license on EC2?

A: Yes, you can bring your own Windows Server license and use it on EC2. This is known as licensing mobility and allows you to leverage your existing licenses in the cloud.

Q10: What is the pricing model for EC2 instances?

A: EC2 instances are charged on a pay-as-you-go basis, where you only pay for the resources you use. The pricing varies based on the instance type, region, and usage.

Exit mobile version