Create a simple microservice using Lambda and API Gateway
In this tutorial you will use the Lambda console to create a Lambda function, and
an Amazon API Gateway endpoint to trigger
that function. You will be able to call the endpoint with any method (GET
, POST
,
PATCH
, etc.) to trigger your Lambda function. When the endpoint is called, the entire request
will be
passed through to your Lambda function. Your function action will depend on the method
you call your endpoint with:
-
DELETE: delete an item from a DynamoDB table
-
GET: scan table and return all items
-
POST: Create an item
-
PUT: Update an item
Create an API using Amazon API Gateway
Follow the steps in this section to create a new Lambda function and an API Gateway endpoint to trigger it:
To create an API
-
Sign in to the AWS Management Console and open the AWS Lambda console.
-
Choose Create Lambda function.
-
Choose Blueprint.
-
Enter
microservice
in the search bar. Choose the microservice-http-endpoint blueprint and then choose Configure. -
Configure the following settings.
-
Name –
lambda-microservice
. -
Role – Create a new role from AWS policy templates.
-
Role name –
lambda-apigateway-role
. -
Policy templates – Simple microservice permissions.
-
API – Create a new API.
-
Security – Open.
Choose Create function.
-
When you complete the wizard and create your function, Lambda creates a proxy resource
named
lambda-microservice
under the API name you selected. For more information about proxy resources,
see Configure proxy integration for a proxy
resource.
A proxy resource has an AWS_PROXY
integration type and a catch-all method ANY
. The
AWS_PROXY
integration type applies a default mapping template to pass through the entire request
to
the Lambda function and transforms the output from the Lambda function to HTTP responses.
The ANY
method defines the same integration setup for all the supported methods, including
GET
,
POST
, PATCH
, DELETE
and others.
Test sending an HTTP request
In this step, you will use the console to test the Lambda function. In addition, you
can run a
curl
command to test the end-to-end experience. That is, send an HTTP request to your API method
and have Amazon API Gateway invoke your Lambda function. In order to complete the
steps, make sure you have created a DynamoDB
table and named it "MyTable". For more information, see Create a DynamoDB table with a stream enabled
To test the API
-
With your
lambda-microservice
function still open in the console, in the upper right hand corner of the console, choose Test. -
In Event name, enter a name for the test event.
-
In the text entry panel, replace the existing text with the following:
{ "httpMethod": "GET", "queryStringParameters": { "TableName": "MyTable" } }
-
After entering the text above choose Create.
-
Choose the event that you created and choose Test.