What is Zappa?

Zappa is a very lightweight and powerful tool which allows you to deploy and update python applications to AWS. Using Zappa you can host your WSGI app on AWS Lambda, API Gateway quickly.

Why do we need Zappa?

Generally, we prefer to use a VM to host our python application and the VM is costly compared to serverless technology because it will be up and running all the time and the cloud provider is billing us as per run time. Pricing based on the amount of computing power, storage, and time.

  • Infinite scaling
  • Zero downtime
  • Zero maintenance

Serverless

Nowadays serverless is in a buzz. Actually without any server we can’t host our application  there is a resource which is available on demand. “Serverless computing is a cloud computing execution model in which the cloud provider allocates machine resources on demand, taking care of the servers on behalf of their customers.”

EC2 vs. Lambda

AWS EC2 is a service that provides traditional cloud infrastructure (IaaS) and allows us to run EC2 instances as VMs. AWS Lambda provides us a serverless architecture and allows you to run arbitrary event-driven code in the cloud after an event trigger is activated.

Alternative

There are many alternatives available for Zappa

Flask with Zappa

In this section I am going to demonstrate how to make a Flask web application serverless using zappa. I have already installed minimal dependencies into my local machine e.g. python3 virtualenv

Step 1: Create a project folder called “myflask”.

> mkdir myflask

Step 2: Create a “requirements.txt” file inside of the myflask folder.

 Flask==2.0.1
 zappa==0.48.2 

Step 3: Create Virtual Environment

> virtualenv env

Step 4: Activate Your env

(Windows) D:\zappaTest>env\Scripts\activate
(Mac) $ source env/bin/activate

Step 5: Install Dependencies in Environment

(env)> pip3 install -r requirements.txt

Step 5: Create an index.py file for Flask.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

@app.route('/hello')
def hello_world1():
    return 'Hello, World1!'
 
if __name__ == '__main__':
    app.run(host="0.0.0.0", port=3000, debug=True) 

Step 6: Test Flask

(env)> python3 index.py

Step 7: Create a Zappa configuration file.

(env)> zappa init
(read all instructions)

after completing the instructions you will get zappa config file like below

{
     "dev": {
         "aws_region": "ap-south-1",
         "django_settings": "myproject.settings",
         "profile_name": "default",
         "project_name": "project",
         "runtime": "python3.6",
         "s3_bucket": "zappa-ga67u69nx"
     }
 } 

Step 8: Setup your aws configure

There is two ways to configure your aws credentials using AWS command or updating credentials file if you want to go with command then you must have awscli installed into your local machine if not install using 

> pip3 install awscli
> aws configure
 AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
 AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
 Default region name [None]: us-west-2
 Default output format [None]: json 

OR

> vi ~/.aws/credentials
 [default]
 aws_access_key_id = 'abc'
 aws_secret_access_key = '123’

Step 9: Deploy Flask

(env)> zappa deploy <env>
 or 
(env)> zappa deploy 

Step 9: After Deploying Check Server Log

(env)> zappa tail

Step 9: Update Codebase

(env)> zappa update dev

Step 10: If you want to remove this project from aws

(env)> zappa undeploy <env>
            OR
(env)> zappa undeploy

Leave a Reply

Your email address will not be published.