Create a serverless microservice with Zappa, Flask & AWS Lambda

April 21, 2017

As you may have seen around the web lately, there is a lot of buzz about serverless architectures and what they mean to modern web development en hosting. To understand more about what serverless computing is and what it can do for you, it’s best to check out the Wikipedia page on serverless computing.

In this blog I will show you how to create a fast little microservice (very basic Hello World) which you can deploy and update easily. I am running macOS, which works, but the following also works on other machines running Linux. In my example I am running on Ubuntu 16.04, running Python 2.7.

We’re going to use http://flask.pocoo.org/ for our microservice and https://www.zappa.io/ for deploying our microservice.

Setup your local environment

A prerequisite for setting up the complete environment is that you have Python (2.7 or 3.6), usually this is 2.7. Another prerequisite is pip (for installing packages), if pip does not work on your machine, you may install it by using easy_install.

# Install setuptools, for easy_install
apt-get install pyton-setuptools
# Install pip
easy_install pip

Boto

Boto is the Amazon Python SDK for executing actions on the Amazon Web Services platform. You need Boto so that Zappa (our tool for deploying, setup below) can connect to AWS and perform the actions needed to launch your microservice. To setup Boto we first need to install boto:

pip install boto

After Boto is installed, you can create a user in AWS IAM. There is some debate about which roles the Zappa user needs to have (see: https://github.com/Miserlou/Zappa/issues/244). Other frameworks like serverless.com also tell us to create an Administrator user, so we will do so in the steps below.

I don’t particularly agree with an approach to create a user with full access to everything, but I think this is a drawback from serverless architectures being this new.

Login to your AWS account, and go to IAM. After this step, go to your users and create a new user. We’ll call this user “zappa-user” and give this user “Programmatic access”.

step 1 - serverless

After the first step, step 2 is actually giving the user permissions, go to “Attaching existing policies directly” and select AdministratorAccess.

step 2: serverless

After this, review the user and create it. You will get an Access Key ID & Secret Access Key, please save these somewhere, you will need them to setup Boto.

To setup Boto using the keys you got from the previous step, just create a ~/.aws/credentials file in your home folder and add the following content to the file:

[default]
aws_access_key_id=<aws access key>
aws_secret_access_key=<aws secret access key>

Also you need a ~/.aws/config file, to define a region you want to use, I will be using eu-west-1:

[default]
region=eu-west-1

With these files, your Boto is set up!

Virtualenv

We’ll start by installing virtualenv, so that you have an enclosed environment where you can install the packages etc.

pip install virtualenv

To create a virtual environment you must create a directory first, I will call mine “hello-world” and change directory to “hello-world”. After that I will create the virtual environment, and activate it.

# Create directory
mkdir hello-world

# Change directory to hello-world
cd hello-world

# Create virtual environment
virtualenv env

# Activate the virtual environment
source env/bin/activate

After activating you will notice that you have an (env) tag in front of your username. This means that you are in the virtual environment. Now we can start setting up the project.

Flask & Zappa

Flask (http://flask.pocoo.org/) is a microframework for Python and is incredibly easy to implement in this situation (because of it’s easy routing capabilities and super easy setup).

Zappa (https://github.com/Miserlou/Zappa) is used as a tool to easily deploy an API Gateway on AWS and also create all the corresponding Lambda functions needed to run our “hello-world” example.

To install Flask & Zappa, you can run the following command in your virtual environment:

pip install zappa flask

After installing, we can start with setting up the Flask application. This is done by creating a hello-world.py with the following content:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
  return "Hello World!"

if __name__ == "__main__":
  app.run()

To test if Flask is working you can execute:

python hello-world.py

If Flask works, this will return a url (http://127.0.0.1:5000/) which you can call and get a Hello World back!

With Flask installed we can start with setting up Zappa, this is done by creating a zappa-settings.json, create this file with the following content:

{
  "dev": {
      "app_function": "hello-world.app",
      "s3_bucket": "well-hello-there"
  }
}

The app_function corresponds to our Flask app, the S3 bucket is used to transfer our app as a zip to S3 (for some reason hello-world-app is not working, this can be any random name), which in turn will be pushed to Lambda. The cool thing is, is that Zappa takes care of these steps for you.

You can define multiple environments and settings for the different stages you have (for example: dev, test, staging, prod).

Deploying your serverless microservice

Great! Let’s do a deploy! We are deploying with dev, because this is the stage we supplied in our zappa-settings.json.

zappa deploy dev

What Zappa does for you is actually a few steps:

  • It downloads and installs all dependencies
  • Packages everything into a ZIP
  • Uploads your ZIP to the S3 bucket you provided
  • Creates a stack (API Gateway, Lambda)
  • Configures the Lambda function(s)
  • Deploys the API Gateway

If everything went correctly you will be supplied with an URL to your API, visit the URL and you will see a “Hello World!”, and you’re up-and-running!

If you change something in your app, just do the following command afterwards to update your microservice:

zappa update dev

To destroy your microservice, just call:

zappa undeploy dev

To debug your microservice, you can just call:

zappa tail

This will give you your logs, so you can check what is going on.

For questions about serverless and other hosting-related issues, you can always contact me on my email address amer@livebyt.es, or in the comment section below this article.

Have you seen the Bits vs Bytes podcast yet? Click here to listen to it.

Spread the love

Comments are closed.