Developing a Serverless Backend API using Flask
By: Sourabh Mokhasi
Tags: Flask, AWS, Serverless, Python, Lambda

API-Architecture

In this post, I will guide you through the process of setting up a simple Flask application with data persisted in AWS DynamoDB ,and deploying the application using the popular Serverless Framework.

Part 1 — Setting up DynamoDB

Step 1: Creating DynamoDB table

Create a DynamoDB table with a partition key of your choice[1].

For this example, I am using an application created for senior citizens and my table is called ‘citizens’.

create_table.py

Step 2: Load data into Dynamodb

The table partition key is a string called ‘citizenId’ and for optimum performance , I am using the uuid module to generate a unique uuid values for each item. You can read more about optimizing DynamoDB performance here.

dynamodb_loader.py

Item example

DynamoDB item

Part 2 — Prepare Flask for DynamoDB CRUD operations

I am creating a single GET API for the purposes of this post and I am returning all the items present in the table using the scan operation. This operation may not be the best choice if you have a large number of items in the table.

Setup the DynamoDB resource with the table name

dynamodb = boto3.resource('dynamodb', region_name='us-east-1', endpoint_url="https://dynamodb.us-east-1.amazonaws.com")

table = dynamodb.Table('table_name')

Write a method for GET API. Note, I am also using the Flask cross_origin module to generate a CORS response.

app.py

Part 3 — Serverless deployment

Step 1: Installing the Serverless Framework

The serverless framework has a couple of dependencies which need to be installed first.

Install nodeJS Install docker desktop

Once the above dependencies are installed, verify by running the following commands in your command prompt.

3. Install serverless framework by following the instructions presented here

Step 2: Setting up serverless framework

Navigate into your project directory and run the following commands.

npm init -f

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

The above commands create the package.json file and, installs the ‘serverless-wsgi’ and ‘serverless-python-requirements’ dependencies.

The ‘serverless-wsgi’ plugin is required for negotiating the API Gateway event type into the WSGI format that Flask expects.

The ‘serverless-python-requirements’ plugin is required for handling our python packages on deployment.[2]

Step 3: Creating the serverless.yml file

The serverless.yml file is a configuration file instructing the serverless framework on the various parameters required for creating the Lambda function and API gateway used in the deployment of this application.[2]

serverless.yml

The above configuration,

Creates a Lambda function Creates a Lambda execution role with permissions to perform certain actions on DynamoDB. Creates a catch-all API gateway. Routing logic is handled by Flask.

Step 4: Create a requirements.txt file

Create the requirements.txt file using,

pip freeze > requirements.txt

Depending on your workflow, you can choose to create this file at the start of your development workflow and update as you install additional dependencies.

This file is used by ‘serverless-python-requirements’ plugin to build the deployment package.

Step 5: Deploy

Deploy the application using,

sls deploy

The above command returns the API endpoint of your application.

Step 6: Troubleshooting

Serverless framework provides an easy way to troubleshoot your Lambda application by providing an option to run the function locally. However, for this post, I am using Cloudwatch logs instead.

Use the logging module to create verbose and user-friendly logs.[3]

import logging

logger = logging.getLogger()

logger.setLevel(logging.INFO)

Lambda Cloudwatch logs

The code used in this article is present on Github.

That’s it. This post aims to provide a basic overview of setting up a Flask serverless application.Feel free to reach out to me via comments/LinkedIn in case of any questions or comments.