Prerequisite Concepts, Skills and/or Values

Introduction

Building a scalable and reliable RESTful API from scratch can be challenging with hardware and software in mind. Using AWS Lambda allows the developer to build a RESTful API that are scalable, reliable and inexpensive without worrying too much about the compute resources, server and operating system maintenance, capacity provisioning and automatic scaling, code and security path deployment, code monitoring and logging. All you need to do is supply the code and that is it. It does sound very appearling as the developers get to focus on the business logics and shipping their product for their client. In addition, many software developer job requirements require some knowledge of RESTful API. Learning what a RESTful API is and build one from scratch can really help the learner to stands out in a pile of applicants. As there are more and more technology companies started to adapt AWS ecosystem, knowing how to use AWS is also a big plus on their resume.

Audience Needs & Motivation

Learning Objectives

Learn to build a RESTful API using AWS Lambda

In this section, I am going to show you guys how to build a RESTful API using AWS Lambda.

AWS Lambda is a compute service that lets you run code without provisioning or managing servers. AWS Lambda executes your code only when needed and scales automatically, from a few requests per day to thousands per second. You pay only for the compute time you consume - there is no charge when your code is not running. With AWS Lambda, you can run code for virtually any type of application or backend service - all with zero administration. AWS Lambda runs your code on a high-availability compute infrastructure and performs all of the administration of the compute resources, including server and operating system maintenance, capacity provisioning and automatic scaling, code monitoring and logging. All you need to do is supply your code in one of the languages that AWS Lambda supports (currently Node.js, Java, C# and Python).

To simplify the problem, we are simply going to build a Hello World version of RESTful API using AWS Lambda. The code for doing the Roman Numerals Conversion will come later in the blog. Also, as I am more familiar with Python. The example here is going to be in Python. Okay, let’s head over to your AWS account Dashboard and search “Lambda”. Select the first one and you should see a page that briefly teaches you how it works. Click the “Create a function” button and it should take you to this page.

Create a Lambda function

Here, you can simply set it up exactly as I suggested in the figure above. Next, you would need to write a Lambda function that will print the “Hello World” message. Here is a quick example of what it might look like.

  def lambda_handler(event, context):
    return {
      "message": "Hello World"
    }

Since we are building a RESTful API, we need to use API Gateway to trigger the Lambda function.

API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. With a few clicks in the AWS Management Console, you can create an API that acts as a “front door” for applications to access data, business logic, or functionality from your back-end services, such as workloads running on Amazon Elastic Compute Cloud (Amazon EC2), code running on AWS Lambda, or any Web application. Amazon API Gateway handles all the tasks involved in accepting and processing up to hundreds of thousands of concurrent API calls, including traffic management, authorization and access control, monitoring, and API version management. Amazon API Gateway has no minimum fees or startup costs. You pay only for the API calls you receive and the amount of data transferred out.

Thanks to Amazon. API Gateway is fairly easy to set up and configure. However, its compatibility could be the problem but still fairly easy to solve. We know that the API Gateway expects Lambda functions to return certain output. You must code your function accordingly. This is the structure it expects:

  {
    "statusCode": 200, 
    "headers": {"Content-Type": "application/json"},
    "body": "body_text_goes_here"
  }

In the case of the Eratosthenes function, we actually return json as the body so we have to use backslashes to escape our json-within-json:

  {
    "statusCode": 200,
    "headers": {"Content-Type": "application/json"},
    "body": "{\"message\": \"Hello World\"}"
  }

API Gateway will read the “body” element and return it as the body of the response: backslashes to escape our json-within-json:

  {
    "message": "Hello World"
  }

Now, to accommodate that (for Lambda to work with API Gateway). Here is what the Lambda function might look like.

  def lambda_handler(event, context):
    return {
      "statusCode": 200,
      "headers": {"Content-Type": "application/json"},
      "body": "{\"message\": \"Hello World\"}"
    }

Simply copy that and paste it into the Function code like this.

Sample function code

Next, you should head over to the top and add the API Gateway as your Lambda triggers. Just like what I did here.

add trigger

Make sure you hit the save button to save all your works. Next, find out what your Invoke URL is for your API Gateway for Lambda. Open up Postman or Restlet Client to check out the Hello World version RESTful API that you just did using API Gateway and Lambda. You should get something like this in return response.

return response

Tada! You just built yourself a Hello World version of RESTful API using API Gateway and Lambda.

Learn to do a Roman Numerals Conversion in hand

Roman Numerals Conversion is actually pretty simple to learn. All you have to do is to get familiar with the table below.

Number Roman numeral Calculation
0 not defined  
1 I 1
2 II 1+1
3 III 1+1+1
4 IV 5-1
5 V 5
6 VI 5+1
7 VII 5+1+1
8 VIII 5+1+1+1
9 IX 10-1
10 X 10
11 XI 10+1
12 XII 10+1+1
13 XIII 10+1+1+1
14 XIV 10-1+5
15 XV 10+5
16 XVI 10+5+1
17 XVII 10+5+1+1
18 XVIII 10+5+1+1+1
19 XIX 10-1+10
20 XX 10+10
21 XXI 10+10+1
22 XXII 10+10+1+1
23 XXIII 10+10+1+1+1
24 XXIV 10+10-1+5
25 XXV 10+10+5
26 XXVI 10+10+5+1
27 XXVII 10+10+5+1+1
28 XXVIII 10+10+5+1+1+1
29 XXIX 10+10-1+10
30 XXX 10+10+10
31 XXXI 10+10+10+1
32 XXXII 10+10+10+1+1
33 XXXIII 10+10+10+1+1+1
34 XXXIV 10+10+10-1+5
35 XXXV 10+10+10+5
36 XXXVI 10+10+10+5+1
37 XXXVII 10+10+10+5+1+1
38 XXXVIII 10+10+10+5+1+1+1
39 XXXIX 10+10+10-1+10
40 XL -10+50
41 XLI -10+50+1
42 XLII -10+50+1+1
43 XLIII -10+50+1+1+1
44 XLIV -10+50-1+5
45 XLV -10+50+5
46 XLVI -10+50+5+1
47 XLVII -10+50+5+5+1
48 XLVIII -10+50+5+1+1+1
49 XLIX -10+50-1+10
50 L 50
51 LI 50+1
52 LII 50+1+1
53 LIII 50+1+1+1
54 LIV 50-1+5
55 LV 50+5
56 LVI 50+5+1
57 LVII 50+5+1+1
58 LVIII 50+5+1+1+1
59 LIX 50-1+10
60 LX 50+10
61 LXI 50+10+1
62 LXII 50+10+1+1
63 LXIII 50+10+1+1+1
64 LXIV 50+10-1+5
65 LXV 50+10+5
66 LXVI 50+10+5+1
67 LXVII 50+10+5+1+1
68 LXVIII 50+10+5+1+1+1
69 LXIX 50+10-1+10
70 LXX 50+10+10
71 LXXI 50+10+10+1
72 LXXII 50+10+10+1+1
73 LXXIII 50+10+10+1+1+1
74 LXXIV 50+10+10-1+5
75 LXXV 50+10+10+5
76 LXXVI 50+10+10+5+1
77 LXXVII 50+10+10+5+1+1
78 LXXVIII 50+10+10+5+1+1+1
79 LXXIX 50+10+10-1+5
80 LXXX 50+10+10+10
81 LXXXI 50+10+10+10+1
82 LXXXII 50+10+10+10+1+1
83 LXXXIII 50+10+10+10+1+1+1
84 LXXXIV 50+10+10+10-1+5
85 LXXXV 50+10+10+10+5
86 LXXXVI 50+10+10+10+5+1
87 LXXXVII 50+10+10+10+5+1+1
88 LXXXVIII 50+10+10+10+5+1+1+1
89 LXXXIX 50+10+10+10-1+10
90 XC 100-10
91 XCI 100-10+1
92 XCII 100-10+1+1
93 XCIII 100-10+1+1+1
94 XCIV 100-10-1+5
95 XCV 100-10+5
96 XCVI 100-10+5+1
97 XCVII 100-10+5+1+1
98 XCVIII 100-10+5+1+1+1
99 XCIX 100-10-1+10
100 C 100
200 CC 100+100
300 CCC 100+100+100
400 CD 500-100
500 D 500
600 DC 500+100
700 DCC 500+100+100
800 DCCC 500+100+100+100
900 CM 1000-100
1000 M 1000

For simplicity, we are going to ignore number that is larger than 1000. By following the table, you should be able to do the Roman Numerals Conversion.

Implementing the Roman Numerals Conversion RESTful API

Thanks to Roman library. It allows us to easily convert digital numbers to Roman Numerals with two lines of code. For example,

  import roman
  print(roman.toRoman(10)) # you should get 'X'

Now, we simply need to make sure we can pass in data so that we can do the conversion. Here is how you can do it with Lambda.

  def handler(event, context):
    e = event.get('e')
    pi = event.get('pi')
    return {
        "statusCode": 200,
        "headers": { "Content-Type": "application/json"},
        "body": e + pi
    }

Let’s use that in our Lambda functions.

  def lambda_handler(event, context):
    import roman
    return {
      "statusCode": 200,
      "headers": {"Content-Type": "application/json"},
      "body": "{\"result\": %d}" % (roman.fromRoman(event.get('d')))
    }

Simply update your Lambda function to the one above. The Roman Numerals Conversion should now works. Here is what it might looks like,

sample roman numerals conversion response

Tada! You now have a fully functional Roman Numerals Conversion RESTful API that will convert any digital number to Roman Numerals.

Extensions

In case you found the tutorial too easy, here are some of the pointers that will make this more challenging.

Wrapping Up

Hopefully this guide has help you to build a scalable, reliable, and inexpensive RESTful API using AWS Lambda that will do the Roman Numerals Conversion for you. Thank you for reading!

Resources

I’ll try to keep this list current and up to date. If you know of a great resource you’d like to share or notice a broken link, please let us know.

Getting started