Part 4 - Create and Configure API Gateway API

With your Web API deployed on a t2.micro instance, the server could obviously be overwhelmed by heavy traffic. You could set up horizontal scaling to deal with the traffic, choose a larger instance type, or put your APIs behind an API Gateway facade and implement throttling.

For this lab, we’ll implement the third option: throttling. Follow the steps below to configure and deploy an API Gateway API in front of your REST API application.

1. In the AWS Management Console, type “api gateway” into the search box to navigate to the API Gateway console. See the figure below.

MngtConsoleApig

2. Create a new API, assigning a meaningful name and description. This API will be regional, rather than edge optimized or private.

When you first create the API, there are no resources (URLs) or methods (GET/POST/PUT/etc) created for it. API Gateway allows you to have different resources (ULRs or paths) and methods assigned to different backend APIs. Since all of the REST APIs are running on one server, we’ll configure API Gateway to proxy all requests to the same backend, using the proxy resource and ANY method pattern

4. In the center pane, in the Actions dropdown, select “Create resource”, and then check the box to configure the new resource as a proxy resource, which will automatically populate the resource name and path (see figure below). Click the create button to create the resource and display the method setup view.

ApigCreateResource

5. In the method setup, choose HTTP Proxy as the integration type, Passthough as the content-handling type, and enter the domain name for the REST API backend, appending “{proxy}” to the end (no quotes) for the endpoint URL. Your URL should look something like http://yourappname-prod.us-west-2.elasticbeanstalk.com/{proxy} See the figure below. Be sure to include the “http://” part of the URL.

ApigConfigMethod

7. In the method test view, select “GET” as the method, /api/values as the path, then click the test button. The response body (shown at upper-right of the pane) should show the same json response as we saw in the browser earlier. If you test the POST method, ensure you add “Content-type:application/json” to the headers box (no quotes).

8. Now we’ll deploy our API. In the center pane, click the Actions dropdown and select, Deploy API.

9. In the Deploy API dialog window that appears, select “[New Stage]” in the Deployment stage dropdown, enter “prod” (no quotes) as the stage name, a description for the stage (something like, “My API production stage”) and a deployment description (something like, “initial deployment”). Then click the deploy button.

*Note: The stage editor view will show in the content pane, and the URL to access your API Gateway API will be shown, linked, at the top. A stage is the actual deployed API endpoint(s) that will be exposed for clients to access.

10. Review the API Gateway throttling documentation for more information about how throttling works. You can access the Default Throttling through the 1) Stages, 2) click on the stage (eg ‘prod’), 3) Enable Throttling.

11. Change the throttling values to 1 request/second, with a burst rate of 2 requests. We’re choosing this low value to make it easier to test without load-testing software. Save your changes.

12. Copy the Invoke URL, and create a new GET request in Postman for that URL plus “/api/values” (no quotes). Verify that you get back the same json response as earlier. The URL will look something like: https://abck123xy.execute-api.us-west-2.amazonaws.com/prod/api/values.

You have now deployed an API Gateway API to proxy requests to your Web API, implemented throttling, and verified the API Gateway API is accessible.