Using a REST API with AWS, Part 3

In our previous blog posts (Part 1, Part 2), we have created some Lambda functions that query against a DynamoDB table. Now we will put an API gateway in from our services.

One complication of hosting is the point of contact, between the callers and the service. In many systems, the callers call the services directly. This is problematic because it means the callers are directly tied to the service. If you inject an API gateway between the caller and your service, it gives you a lot more options.

First off, the routing can be handled by the API gateway, making your callers less dependent upon your services. Second, API gateway can do some translation as it routes the request. Third, using a gateway allows us to make larger changes on the backend but keep a stable API for the callers. All those things sound good, right?

Open up a Lambda function and add an API Gateway trigger.

Creating an API Gateway starting with the Lambda is okay, but you will usually need to get into the API Gateway and play with some settings before you will find it useful.

Navigate the AWS UI menu and go into the API Gateway section. Find your API Gateway and add a POST method. We will add a POST method that provides the JSON data we need for our Lambda to work.

Click the “Action” button and create a resource for “contacts”. Then let’s create a subresource for “all” using the Action menu. Then create a method “POST” on the “all” resource, again using the “Action” button.

You should now have a resource that looks something like below.

Enter the name of your Lambda function “ContactsFindAllForGroup” and click “Save”.

Now that the Lambda is created, we should test it.

Paste in the JSON that was used to test the API in our first blog post of the series.

{
"groupid": "TEST"
}

After running the test, we should get our data.

Now it is basically a repetitive behavior to set up the other three.

If you have any questions or comments, hit me up on Twitter or sound off in the comments below.


Related posts