How to Build a Serverless REST API in 15 Minutes on AWS

Use AWS Lambda to build a Serverless REST API, storing data in S3 and querying it with Athena

Quentin Febvre
Sicara's blog
3 min readApr 25, 2018

--

Read the full article on Sicara’s website here.

We’re going to build a Serverless REST API and deploy it on AWS without setting up any server!

Why use serverless?

The great thing about going serverless is to be able to deploy your code instantly without bothering to setup a server.

But after a while, you quickly realize that having stateless bits of codes in the cloud has its limits. For example, how do you persist your data?

In this article, We’ll build a REST API using AWS Lambda (python 3.6), that stores data on an S3 Bucket and then queries it using AWS Athena.

We’ll create the following API:

  • POST /user: Create a user
  • GET /user/{user_id}: Fetch the data matching the user_id
  • PUT /user/{user_id}: Update the data matching the user_id
  • DELETE /user/{user_id}: delete the data matching the user_id
  • GET /user/list: returns a list of all the users

TL;DR: To jump to the full working example, you can go there:

We will take the following steps:

  • Install the necessary toolkits and creating a serverless project
  • Write the serverless deployment configuration
  • Define a helper class S3Model to write and read data from the S3 Bucket created in the config file
  • Define a helper class S3ApiRaw to generate the API handlers
  • Create the user.py module with the User schema and generate the API handlers
  • Configure AWS Athena to be able to query our data using SQL statements

Let’s get started!

Step 0: The requirements

The serverless-python-requirements plugin is used to ship the python dependency with the lambda, to do this all you need is to create a requirements.txt file

Step 1: Writing the deployment config

By creating a serverless project, a serverless.yml file was created. This config file will contain multiple key aspects of your project:

  • What Lambda function execute what piece of code (A function called a handler)
  • What route / parameters / HTTP method will trigger which Lambda function
  • What other AWS resources you want to create (one S3 Buckets in our case)
  • The permissions in order for the Lambda functions to interact with other AWS resources

In our case we will need to specify the following:

  • One S3 Bucket to store our data
  • Five Lambda functions: user_get, user_post, user_put, user_delete, user_list
  • The Lambda needs to be able to read and write on the S3 Bucket

Let’s take a look at what our serverless.yml will look like:

Given this configuration file, We now have to provide a python module user.py with 5 functions: get, post, put, delete and list.

Step 2: The S3Model class

An S3Model class is characterized by two class attributes:

  • a SCHEMA that will define the fields and field types of our object
  • a name that will specify an S3 folder in which the data will be stored

The class will have 5 methods:

… Read the full article on Sicara’s website here.

--

--