In this lesson, we will download a flask application using git from github and deploy it to AWS lambda using zappa!
Lesson 2 Overview
In this lesson, we will download a flask application using git from github and deploy it to AWS lambda using zappa!
- What is Flask?
-
-
Flask is a micro framework for building web application in Python. It is based on Werkzeug WSGI toolkit and the Jinja2 templating engine. It is similar, but lighter weight than app frameworks like Django for Python or Drupal for php.
-
We are using Flask for this lab because its lightweight, easy to understand and deploy-able to lambda using Zappa.
-
-
Why AWS Lambda?
-
Web applications usually don’t have constant workloads. Sometimes they are busy serving traffic, sometimes they sit idle. Deploying web applications into virtual machines (EC2) or containers (ECS) means that our app is almost always either under, or over utilized.
-
It’s almost impossible to size a web application server to run at 99% utilized at all times. As a result, we are paying too much for idle resources, or our server is slammed and the users have a bad experience.
-
The usual fix is to use smaller web/app servers, with DNS load balancing, Elastic Load Balancers and Autoscaling groups. But this all comes with a price. Complexity. (and still a little waste)
-
Lambda is a solution to this problem. Deploy dynamic portions of your web application AWS lambda just once. If no users hit your application, there is no cost. If your application gets slammed with millions of users, AWS scales your app to millions of instances. This happens with zero administration of ELBs, Autoscaling groups or operating systems. So it’s cost effective and easy!
-
-
What is Zappa?
-
Zappa makes it super easy to build and deploy all Python WSGI applications (like Flask) on AWS Lambda + API Gateway. Think of it as “serverless” web hosting for your Python apps. It’s great for deploying serverless micro-services with frameworks like Flask and Bottle, and for hosting larger web apps like Django. Or, you can use any WSGI-compatible app you like! You probably don’t need to change your existing applications to use it, and you’re not locked into using it.
-
Zappa also lets you build hybrid event-driven applications that can scale to trillions of events a year with no additional effort on your part! You also get free SSL certificates, global app deployment, API access management, automatic security policy generation, precompiled C-extensions, auto keep-warms, oversized Lambda packages, and many other exclusive features!
-
In this lab, we will use zappa to deploy our flask application to lambda.
-
-
-
As Git is a distributed version control system, it can be used as a server out of the box. Dedicated Git server software helps, amongst other features, to add access control, display the contents of a Git repository via the web, and help managing multiple repositories.
-
GitHub is a web-based Git or version control repository and Internet hosting service. It is mostly used for code. It offers all of the distributed version control and source code management functionality of Git as well as adding its own features
-
In this lab, we will use git to pull Flask application code down from github.
-
Lesson
-
Clone flask app from github master branch (a very basic hello world)
cd ~/ git clone https://github.com/cloudshiftstrategies/flask_lambda_lab cd ~/flask_lambda_lab
-
Initialize Zappa config file
-
Initialize zappa config file: `zappa_settings.json` (Take the defaults)
-
NOTE: If more than one person is using the same AWS account for this lab concurrently, they should choose a unique names for the environment when running `zappa init`. If both users use the default environment name (dev), the resource names will conflict
zappa init
-
EXAMPLE:
$ zappa init ... FULL OUTPUT TRUNCATED... What do you want to call this environment (default 'dev'): What do you want call your bucket? (default 'zappa-ip7znwymz'): Where is your app's function? (default 'app.__init__.app'): Would you like to deploy this application globally? (default 'n') [y/n/(p)primary]: Does this look okay? (default 'y') [y/n]:
-
to take a look at the zappa configuration file that was created
cat zappa_settings.json
-
-
Deploy the zappa application to AWS Lambda
-
This will provision your flask application to AWS lambda using zappa_setting.json config file as recipe
-
NOTE: If you used an environment name other than dev, substitute it in below
Test access to web application via API gatewayzappa deploy dev
-
-
Hello World
-
Copy the URL of the API gateway from the zappa deploy command into a browser window
-
Open browser: https://izg1p4sbd7.execute-api.us-west-2.amazonaws.com/dev
-
Congratulations! You are looking at your application running in AWS Lambda. To make the application HA and scalable to infinity you dont have to do anything. It’s already done! Cool huh?
-
-
The ‘zappa deploy dev’ command deployed several components in AWS. Login to AWS Console to see parts:
-
S3: zappa-71examplelf is the bucket zappa uses to deploy code
-
IAM: flask-lambda-la-dev-ZappaLamddaExecutionRole is the role that defines the permissions our lambda function uses
-
Lambda: flask-lambda-la-dev the the function running our Flask code
-
API Gateway flask-lambda-la-dev is the API gateway connecting http requests to the function
-
-
Change “Hello World” code and update
-
Let’s make a change to our boring little application an update lambda.
nano app/__init__.py
- Change existing file: app/__init__.py from flask import Flask
app = Flask(__name__) @app.route("/") def hello(): return "**Hello World!**"
- To something more interesting
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "**Hello from the land of Lambda!**"
-
-
deploy updates code update to lambda
-
To send the updated code to lambda, just run the `zappa update` command
zappa update dev
-
Test updates with Browser
-
-
Undeploy code from lambda
-
When we are ready to take our application down, we can undeploy all those resources using the `zappa undeploy` command
zappa undeploy dev
-
Look in AWS console, resources are gone
-
s3: zappa-skexample09jk bucket will remain, but is empty
-
IAM: role should will remain
-
Lambda: function will be gone
-
API Gateway: gateway has been removed
-
-
Test API Gateway URL in browser, should get error
-
Lesson 2 Summary
In this lesson, we completed the following:
-
Downloaded a hello world flask app from git
-
Deployed that application on an EC2 vm and tested traditional access to the app
-
Initialize zappa and deployed the app to lambda
-
Modified the application and updated it in lambda using zappa
-
Removed the lambda resources using zappa
In the lesson 2, we’ll make the app pretty and start doing some work
Recent Comments