Getting Started With AWS Lambda: A Practical Guide

Approximately remain in this minutes read.

Getting Started With AWS Lambda: A Practical Guide

Getting Started With AWS Lambda: A Practical Guide
Written by

In our previous article titled Getting Started with AWS Lambda: 4 Use Cases, you learned about AWS Lambda and got acquainted with a number of use cases for this service.

Below is an example of a solution that will allow you to use AWS Lambda to serve as a frontend for your application.

The ISBN Lookup Service

Let’s consider the following scenario.

You have a database of records that contain a list of ISBNs (International Standard Book Numbers) for a number of books in your possession. You are interested in looking up the title of a specific book based on its ISBN.

Traditionally, a two-tier application would exist. This is what it would look like inside AWS:


Amazon DynamoDB is the database of choice here. This is a simple database that has a number of records. Instead of managing a database on your own, it makes good sense to use the service provided by AWS.

It is also necessary to spin up an instance to front as a web server to query the database to retrieve the values for the records.

Running a web server 24/7 to listen to queries might make sense for an application that constantly serves massive amounts of content for a high traffic website. In this case, we will use a Lambda function to provide the same functionality as the web server, but without having to actually run a full operating system on an EC2 instance 24/7.

This is the architecture of your application using Lambda:


There is a difference in the architecture, specifically the API gateway. The Amazon API Gateway is a fully managed service that allows you to easily create a “window to the world” for your application in a secure and simple way. You can expose a number of AWS services to the outside world. In our case we will expose the Lambda function that will query our database for information. With the API gateway, you only pay for the actual API calls received and data transfer.

Discover features, limitations and few practical examples of AWS Lambda here.

Database Creation

Our application requires data,  and AWS provides a NoSQL database as a service in the form of DynamoDB.

To create the table in DynamoDB, configure the properties in the DynamoDB Create Table screen.




Database Population

A database without any data is useless,  so let’s insert some data. Tthis can be done in a number of ways,  but for the sake of this example we will do it manually.

Select the ISBN table,  click the Items tab, and then click Create Item.



Enter your data and then click Save.

image12As shown below, there are three different books in my database.


Lambda Function

Now that we have data to serve, let’s create the function that will be the brains behind our application.


Our function will be based on the microservice-http-endpoint example provided by AWS.


For the purpose of this example, we will not use any security (IAM). Nonetheless, you should incorporate security into your application–   this can be managed with IAM roles and policies. 


  1. Give your function a name and description.image19
  2. Replace the example code with the following:
'use strict';
 const doc = require('dynamodb-doc');
 const dynamo = new doc.DynamoDB();
  * @constructor
  * @inherits {Error}
 function HTTPError(code, message) {
     this.code = code;
     this.message = message;
  * A simple HTTP web service to return a book title for
  * the specified ISBN
 exports.handler = (event, context, callback) => {
     const done = (err, res) => callback(null, {
         statusCode: err ? err.code : '200',
         body: err ? err.message : res,
         headers: {
             'Content-Type': 'text/plain',
     switch (event.httpMethod) {
         case 'GET':
             if (event.pathParameters) {
                 dynamo.getItem({ TableName: 'ISBN', Key: { isbn: event.pathParameters.isbn } }, function(err, data) {
                     if (!err && data && data.Item && data.Item.title) {
                         done(null, `${data.Item.title}\n`);
                     } else {
                         done(new HTTPError(404, `Not found\n`));
             } else {
                 done(new HTTPError(404, `Not found\n`));
             done(new HTTPError(400, `Unsupported method "${event.httpMethod}"\n`));

     3. Enter a name for the role your Lambda function will use, and then click Next.


     4. Review your function information, and then click Create Function.


     5. This step creates your function and provisions an API Gateway for you (if you did not already have one).



API Gateway configuration

We need to configure the API Gateway for the function to support isbn as a path parameter.



  1. In your API Gateway, select  the LambdaMicroservice, Resources, and then isbn.
  2. Click Create Resource.
  3. Add a new resource named {isbn}  and configure it as a proxy resource.
  4. Configure your integration as a Lambda Function Proxy, choose the Lambda region where  you created the function, and enter the name of the function you created above.image01
  5. Confirm your configuration.image16
  6. As a result of the changes we made, we need to re-deploy the API with the latest configuration. From the Actions menu, select Deploy API.


Testing the Application

The URL was previously presented to us when the function was created.

By running a curl command for each of the books, we cantest if the application is functioning correctly.


The results that come back from our request are as follows:

The Phoenix Project: A Novel about IT, DevOps, and Helping Your Business Win
The DevOps Handbook: How to Create World-Class Agility, Reliability, and Security in Technology Organizations
Amazon Web Services in Action


From the dashboard of the API gateway, we can see the number of requests n served, as well as the number or  errors and latency metrics.



The graph  function is integrated by default with CloudWatch and allows you to go into even more detail regarding the number of call mades at very detailed resolutions. 


The ISBN database lookup example is just the beginning of what you can do with AWS Lambda. The possibilities and opportunities available to you when using this service are mostly limited by your software development capabilities and your imagination.

The advantage of using such a simple solution is the significant potential cost savings, as you are running only 46 lines of very simple code and pay only for the API calls that are used. You  do not need to keep an EC2 instance up and running (which has a cost), nor do you need to manage and maintain another instance in the cloud on a regular basis (which in itself has an operation cost attached to it).

Calculating the Economics of a Private Cloud Banner - Stratoscale
February 8, 2017

Simple Share Buttons
Simple Share Buttons