Software engineer. A fan of CI/CD. Blogger.

Trying Amazon API Gateway, Lambda and DynamoDb

At the beginning of June 2016, I visited a JavaDay conference in Lviv, where I listen to a talk about serverless architecture. Today I’d like to try serverless.

According to Martin Fowler, serverless is an architecture, which uses third party services or a custom code on ephemeral containers, best known as Amazon Lambda. So, I understand it as an idea to run your backend code on demand: there won’t be any real servers like EC2, Amazon infrastructure will start a container, which will run your Lambda function. Consider reading this article for explanations. I’d like to try this infrastructure.

I decided to use Amazon API Gateway and Amazon Lambda for analyzing GitHub repositories: after each push to the repository I will examine all .md files and store them to Amazon Dynamo Database. More generally, I’ll create a backend part for a self-written blogging platform. Who knows, maybe I will write a frontend part soon.

General plan

The approach isn’t clear enough, so let’s clarify what do we need:

How it’s going to work:

Following diagram explains how blog posts will be handled by our system.

Create IAM role

Before you start, you need to create some role for lambda. I created a lambda-demo role with following policies:

I believe, there should be more restricted policies, but for demo purpose, I decided not think about that.

Here’s how it should look

This two policies will us a green light on running Amazon Lambda and working with DynamoDB.

Create Amazon DynamoDB table

Go to Amazon DynamoDB dashboard page and create a table with posts table name and title key. We don’t need to create more columns, this is just an example.

Creating Lambda functions

According to documentation, to run lambda, you need to upload a .zip file with Java classes. You’ll need to provide a full reference path, together with packages prefix.

I created a separate repository for you so don’t need to copy every piece of code from blog - github.com/ivanursul/amazon-lambda

There are only two lambda functions:

Clone this project to your local machine, build it with

gradle clean build

go to build/distributions/ and make sure archive amazon-lambda.zip is present there.

Next, go to Lambda Management Console and perform following steps:

Repeat the same steps with the second function, ArticlesRequestHandler.

Creating API Gateway

Having just a Lambda function is not enough for using it with GitHub. We need to expose it to HTTP protocol. If we are using Amazon products here, then it’s obvious to try Amazon API Gateway

We need to expose two lambda functions - ProcessRequestHandler and ArticlesRequestHandler.

You need to perform several steps for connecting lambda with api gateway:

Here’s how ArticlesRequestHandler API Gateway should look like:

Next, add Body Mappings Template: application/json with empty string content.

Do the same with ProcessRequestHandler, but add another application/json mappings template:

    "body" : $input.json('$')

Save, and Deploy your API.

Configuring WebHook

After you deploy API Gateway, you need to specify a link for ProcessRequestHandler gateway for your WebHook.

Go to your blogging repository on the Settings page, then on WebHook section and put the link.

In my case, it looks like this

Link example: https://tja9ll5tv3.execute-api.us-west-1.amazonaws.com/prod/ProcessRequestHandler You should have something similar.

If you fail with text instuction, you can watch video:

<a href=”#conclusions name=”conclusions”></a> Conclusions

What I’ve learned from Amazon Lambda?

PS - This blog post is now a part of Deployment Using Containers WIKI