Serverless With Flask

Since, some time I have been using serverless and found that it’s really awesome technology and give lot of freedom and flexibility. Specially as individual developer if you need to deploy your code without any cost and run cheaply, there is nothing better than serverless.

On this tutorial, I will try make a simple api to which fetch the current weather of London. This tutorial assume that you have basic understanding of python, can create virtualenv for python.

First Step:
Need to install serverless Framework on your system.
If you don’t have node install on your system, Install Node as follows:
brew install node
npm install -g serverless

Second Step:
Create a virtualenv with 3.6.0(OR anything which you would like to build your flask app) by following this awesome article:
Let’s assume, you have created virtualenv called weather-flask (it could be any name).

Third Step:
Now under your virtualenv create a directory maybe called weather-flask(it could be any name).

    Install following plugin:

  • serverless-python-requirements: It install the lib from requirements and make it available in serverless.
  • serverless-wsgi: Convert api handler request to wsgi and vise-versa.

npm install serverless-python-requirements --save-dev
npm install serverless-wsgi --save-dev

Fourth Step:
Now you can install pip packages inside your virtualenv. We need two packages at least:
pip install flask
pip install requests

Fifth Step:
First let’s create Flask app with an api to get recent weather of London:
Before starting writing api, you need to get the apikey from OpenWeatherMap.
You need to signup on OpenWeatherMap to get apikey, here is more information:Get Api ID

Sixth Step:
The next step is to create the api, so you can write your code something like that in file called

import requests
import flask

app = Flask(__name__)

def bay_recent_tweets():
    r = requests.get('
    response = app.response_class(
    return response

Seventh Step:
The next task is to create serverless.yml file which we configure to deploy on AWS lambda.

# Name of the service, you would see this as lambda
service: weather-testing

# Plugins which we are using to making our app serverless(as explained above)
  - serverless-python-requirements
  - serverless-wsgi

# Custom configuration. Here we are making our app wsgi app.
    packRequirements: false
    dockerizePip: non-linux

 Configuration for provider, as we are using AWS with python 3.6
  name: aws
  runtime: python3.6
  stage: dev
  region: us-west-2

# Here our handlers are wsgi handler, which handle the events(which are http events) for any kind of routes(api)
    handler: wsgi.handler
      - http: ANY /
      - http: 'ANY {proxy+}'

Last Step:
Now you can deploy this with command as:
sls deploy

Now you should see your api on AWS lambda.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s