StackLeader Blog

Post Image
Jun 1, 2016

Getting Started with AWS Lambda REST Services Part 2 of 3


Part 1 of this post outlined how to configure the AWS Lambda functions for the HTTP REST service. For part 2 we will explore how to configure API Gateway to create REST endpoints to fire the Lambda functions created in the previous post.

Configure API Gateway

Go to the Gateway API dashboard.

Select Create API. If this is your first time using API Gateway you may need to click Get Started. Select New API and name the API Contact API

Creating a new API

Resource /contacts Configuration

Select Actions->Create Resource. Create a resource with the Resource Name contacts and Resource Path contacts

Creating a new API

Resource /contacts/{id} Configuration

Select the new contacts resource and then select Actions->Create Resource.

Select contacts resource

Create a resource with the Resource Name id and Resource Path of {id}. The curly braces creates a path variable id that will be included in the request.

Create id resource

GET Method Configuration

Select /contacts under resources

Select contacts resource

Select Actions->Create Method and select GET. Click the check mark to save the new method.

Select contacts resource

Configure the method to integrate with a Lambda Function, use the Lambda region that you created the function in, and call the Lambda function getContacts.

Configure method

If you are unsure of the regions then go to the Lambda console, select the getContacts Lambda function, and look at the ARN.


After creating the method, you should now see the flow of the GET request on the contacts resource. Select Test to test the new endpoint.


The response should return a list containing two contacts.

Test result

We will come back to this endpoint later in the post to cover security. Do not worry, the endpoint is not public until you deploy your api.

Select the {id} resource and add a GET method.

id get method

Configure the method with same configuration as before.

id get method

Mapping Templates

Now the path id parameter must be mapped to a json payload for the Lambda function. Select Integration Request.

id get method

Change the Body Mapping Templates: Request body passthrough to Never. This will prevent API gateway request body from being passed through to the Lambda function.

passthrough settings

Create a new mapping template for Content-Type application/json

mapping template

Create a mapping template that creates a json object for the Lambda function. The input.params function returns the value of a method request parameter from the path, query string, or header value (in that order). (see AWS doc)

  "id" : $input.params('id')

mapping template

Save and go back to the Method Execution. Now you are ready to select Test, add a path id, and select Test again.

mapping template

If all is well, you should get a response containing one contact with the id you specified.

mapping template

POST Method Configuration

The last method for this API Gateway will add a POST method for the addContact lambda function.

Select the /contacts resource and create a POST method.

post method

Configure the POST method to integrate with Lambda functions, use the appropriate region, and call the addContact Lambda function.

post method

This method will require no additional configuration for now. Test the method with the following request body

    "name" : "test",
    "phone" : "555-555-5555"

You should get a similar response


Congratulations! You have configured API Gateway to synchronously call Lambda functions. In part 3 of this post we will configure the IAM security policies and deploy the api so that it is publicly accessible.