How I Created EasyAsPy from Scratch Using Flask

Step-by-step guide to building and deploying a website using Flask, AWS Elastic Beanstalk, Route 53 and NameCheap

Posted 2018-12-28 11:20:02 by Ronie Martinez

Building and deploying a website seems a difficult task for beginners particularly if you do not have any background on cloud hosting. Here is a simple guide using Flask, AWS Elastic Beanstalk, Route 53 and NameCheap.


Sign up for an AWS Free Tier Account

If you already have an AWS account you can skip this part.

In order to host your website on AWS, you will be needing to sign up for an AWS Account. If you are a beginner, you do not have to worry since an AWS Free Tier account will be free for 12 months. Just do not go over the limits.


Sign in to AWS Console

Go to https://aws.amazon.com/console/ and sign in.

Here are the list of AWS services that we will need:

  1. AWS Elastic Beanstalk
  2. Route 53
  3. Certificate Manager

Buy a domain from NameCheap

I bought EasyAsPy.org from NameCheap.com for $15. You can buy cheaper ones for less than $1. To get a boost, familiarize yourself with the NameCheap dashboard. You are required later to update the Nameserver to link your domain to Route 53


Request a certificate from AWS Certificate Manager

From AWS Console click Services on the top-left corner and click Certificate Manager under Security, Identity, & Compliance.

Certificate Manager

From Certificate Manager Console, click Request a certificate button. Select Request a public certificate and click Request a certificate button.

Under Add domain names, register your domains. I registered easyaspy.org and *.easyaspy.org to match any subdomains that I will set up later. This is called a wildcard certificate. Click Next button.

Add domain names

Under Select validation method, choose Email Validation and click Review button. 2 validation emails will be sent to your NameCheap-registered email.

Set validation method

Under Review, Click Confirm and request button and press Continue button. Wait for the approval emails and approve your certificates to be able to use it on the next section.


Create a new AWS Elastic Beanstalk Application

From AWS Console, click Services on the top left corner and click Elastic Beanstalk under Compute.

Elastic Beanstalk

On Elastic Beanstalk Console, click Create New Application on the top right corner. Enter your preferred application details on the Create New Application dialog and click Create button.

Create New Application

You will then be redirected to your application page. Create a new environment by clicking Create one now.

Elastic Beanstalk Application

Select Web server environment and click Select button.

Select Environment Tier

Leave the details Environment Information as-is. Under Base Configuration, Select Platform > Preconfigured platform and choose Python. Leave Application code > Sample application selected. Click Configure more options button.

Select Python

Here are the additional configurations:

Configuration

  1. Software
    1. Click Modify
    2. Check Enable in S3 Log Storage > Rotate logs
    3. Click Save button
  2. Capacity
    1. Click Modify
    2. Select Load Balanced on Auto Scaling Group > Environment type
    3. Click Save button
  3. Load Balancer
    1. Click Modify
    2. Select Classic Load Balancer
    3. Under Classic Load Balancer section do the following:
      1. click Add Listener
      2. Set Listener port to 443
      3. Set Listener protocol to HTTPS
      4. Set Instance port to 80
      5. Set Instance protocol to HTTP
      6. Choose your SSL Certificate that you set up from the previous section
      7. Click Save button
    4. Click Save button

Click Create environment button.

After your Elastic Beanstalk has been setup, you should be able to check if it is running by clicking the URL on your application dashboard.

Environment URL

If your Elastic application is working properly, you will be able to access the sample page.

Sample application


DNS Management with Route 53 and NameCheap

From AWS Console, click Services on the top left corner and click Route 53 under Networking & Content Delivery.

Route 53

Select Hosted zones and click Create Hosted Zone button. Enter your Domain Name and click Create.

Create Hosted Zone

From the Hosted Zones table, click your domain name and click Create Record Set. Leave Name empty or enter www.  Set Yes to Alias and select your Elastic Beanstalk load balancer in Alias Target.

I prefer non-WWW but I create a record set for each to redirect WWW to non-WWW. For further reading about WWW and non-WWW click here.

Create Record Set

From the record set table, find the entry with the Type of NS. These are nameservers for this route. Take note of the values as you will use these on your NameCheap dashboard.

NS Ruleset

Login to your NameCheap dashboard and select Domain List. Find your domain and click Manage button.

Domain List

Find the NAMESERVERS section, select Custom DNS and enter the nameservers that you obtained from Route 53.

NAMESERVERS


Writing your Flask application

If you are a beginner or do not have any idea about Python programming language and Flask, you can check Beginner's Guide to Python and learn more about Flask.

Write your main application code in application.py. I added HTTP to HTTPS redirection for added security.

from flask import Flask, request, redirect

application = Flask(__name__)


@application.before_request
def before_request():
    """
    HTTP to HTTPS redirect when deployed in Elastic Beanstalk
    """
    url = request.url
    protocol = request.headers.get('X-Forwarded-Proto')
    if protocol == 'http' or url.startswith('http://www.'):
        url = url.replace('http://', 'https://', 1).replace('www.', '', 1)
        return redirect(url, code=301)


@application.route('/')
def index():
    return "Hello world"

Create a file name requirements.txt containing your Flask version.

Flask==1.0.2

Deploying you application code

I use EB CLI in deploying EasyAsPy.org since this is easier to do from command line but requires knowledge of Amazon IAM (Identity and Access Management). It will not be included on this article.

The easiest way to deploy your Flask application is by packaging application.py and requirements.txt together in a ZIP file.

From AWS Elastic Beanstalk console, select your application and click Upload and Deploy button. Click Browse button and select your application ZIP file, enter your Version label and click Deploy.

Upload and Deploy


That's it!

You will spend virtually zero hosting your application on AWS using a Free Tier account.

Route 53 is not part of the AWS Free Tier and costs $0.5 per month.

You can buy a domain from NameCheap for as low as $0.88 and you can use it for a year. You will then again need to renew it after.

Python programming language and Flask are easy to learn. EasyAsPy.org was built using these technologies.

python flask aws elastic beanstalk route 53 namecheap certificate manager


Share