StackLeader Blog
Getting Started with AWS Lambda REST Services Part 2 of 3
Overview
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.
https://console.aws.amazon.com/apigateway/home
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
Resource /contacts Configuration
Select Actions->Create Resource. Create a resource with the Resource Name contacts and Resource Path contacts
Resource /contacts/{id} Configuration
Select the new contacts resource and then select Actions->Create 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.
GET Method Configuration
Select /contacts
under resources
Select Actions->Create Method and select GET. Click the check mark to save the new method.
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
.
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.
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.
Configure the method with same configuration as before.
Mapping Templates
Now the path id parameter must be mapped to a json payload for the Lambda function. Select Integration Request.
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.
Create a new mapping template for Content-Type application/json
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)
Save and go back to the Method Execution. Now you are ready to select Test, add a path id, and select Test again.
If all is well, you should get a response containing one contact with the id you specified.
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.
Configure the POST method to integrate with Lambda functions, use the appropriate region, and call the addContact
Lambda function.
This method will require no additional configuration for now. Test the method with the following request body
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.