Developing RESTful APIs with AWS API Gateway

If you followed the previous post, you now have a functioning AWS Lambda function. But how do we expose or trigger this function, say for a web application/client?

AWS API Gateway is an AWS service that allows developers to create, publish, monitors and secures APIs. These APIs could be something that access another AWS Service, in this case AWS Lambda functions, or other web services and could even be data stored in the cloud. We could create RESTful APIs to enable applications to access AWS Cloud services.

Let’s start building

To start, let’s build a basic web API to invoke our Lambda function using an HTTP GET query. Go to Application services section or search for API Gateway on the AWS Services search box.

It’s a good idea to choose the same region you used previously for your Lambda function. Click Get Started in the API Gateway home page.

In the next page, give your API a name. I’m calling this API manageBooksAPI. Click Create API.

Leave the default resource (/) and create one a new one by clicking Create Resource from the Actions menu.

In the New Child Resource page, give it a name. AS shown below, I’m calling this resource books. Leave the Resource Path as is. Make sure Enable API Gateway CORS is checked. You can proceed by clicking Create Resource.

The books resource will now appear under the default resource. We can now create a method. Choose Create Method under the Action menu.

Select the Get HTTP verb.

In the Integration Type page, select Lambda Function. And in the Lambda Function text box, type the Lambda function name you created and select it from the list. Click Save.

In the next page, just click OK. This is just providing permission for API Gateway to invoke our Lambda function.

Once created, you should have something similar to the one below.

Click TEST at the top of the Client section on the books GET Method execution and click Test in the next page.

You should see something similar to the one below.

We can now see the output of our Lambda function. Take note the Response Headers which shows that the Content Type is in json format.

Deploy our API

We are now ready to deploy our API. Under the Action menu, click Deploy API.

We can have the option to create multiple stage environment where we deploy our API. Let’s create a Production deployment stage by selecting New Stage and giving it Production as it’s Stage Name. Click Deploy.

Note:  Whenever we update our API, we need to re-deploy them.

Once created, you should see the Invoke URL for the newly created  stage environment.

Open your web browser. Using the URL provided and appending the books resource, you should see the JSON values provided by our Lambda function.

We’ve successfully created an API endpoint for our Lambda function. By creating an HTML file stored in Amazon S3 and with the help of Jquery, we can now use the same endpoint in our web application and process the returned JSON data.

 $.getJSON("", function(result){ 
    for (i = 0; i < result['catalogue'].length; i++) { 
      $("#deck").append('<div class="col-md-3"><div class="card-block"><h4 class="card-title">'+ result['catalogue'][i].title +'</h4><p class="card-text">' + result['catalogue'][i].author + '</p><a href="card/'+ result['catalogue'][i].id + '" class="btn btn-primary">Learn More</a></div></div></div>');


With AWS Lambda and API Gateway ( + S3 ), we can create a Serverless application. We can create a method to handle passing parameters using HTTP or formatting the response of a function in our web API. Imagine running applications which scales without the complexity of managing compute nodes. Remember we didn’t even have to setup a single web server instance for this project!.

Yet another AWS Lambda tutorial

I briefly discussed AWS Lambda months ago but I feel that example is too simple. Let’s create a slightly more complex task, a function that list books and which we will use in our API Gateway endpoint on the next post.

To create an AWS Lambda function, login to your AWS console and select Lambda from the Compute Section or select Lambda in the AWS Services search box.

Click Create a function on the AWS Lambda home page.

To simplify the creation of Lambda function, AWS provides sample blueprints which we could use. For this session, we will be creating a function from scratch so click Author from scratch.

On the next screen, we can add a trigger for this Lambda function. We will discuss creating trigger and association a Lambda function to it at a later part of this tutorial. For now just click Next.

In Step 3, give your function a distinct name. I’m calling it manageBooks. For this example, the runtime I will be using is Python 2.7.

It is possible to develop your serverless functions locally thru the Serverless framework and upload it as an archive file. For this session, we are just going to type our code in-line. In the Lambda function code section, copy the code here and paste it in the code area.

What we did here is we have a method (get_all_lesson) which returns an array of books in json format. Take note of the name of the method as we will be using that same name in the section below for the Handler name (lambda_function.get_all_lesson).

Specifying other settings

Everything that’s executed by AWS Lambda needs to have permission to do what it’s supposed to do. This is managed by AWS Access and Identity Management thru roles and policies. We need to create a new basic execution role using the Role menu. Choose create a new role. I am using myBasicRole for the role name. Don’t select a policy template.

You need to configure two important aspects of a Lambda function. How much memory to use affects the quantity of CPU power and the cost of executing the function. For this simple function, 128MB is more than enough. The timeout after which the function is automatically terminated setting is used to avoid mistakes that could start long-running function. Three seconds is fine for this simple function.

You can select Next to review all the configurations, and then select Create function.

In the next screen, after successfully creating the function, select Test to check our function.

Since we are not passing any arguments in our function, we can just use the Hellow World event template. To test, click Save and test.

We should see the result of the test execution from the web console with the summary of the execution and the log output.

In the next post, we will create an API Gateway Endpoint to consume this AWS Lambda Function. And using that AWS Gateway Endpoint in a page hosted on S3 bucket, we will display the list of books.