Now that we know the Lambda function works with some API Gateway test data, and returns a header and body with a statusCode of 200, we just need to add the API Gateway that will invoke the Lambda function, as shown in the following diagram:
Perform the following steps:
- Sign in to the AWS Management Console and open the API Gateway console at https://console.aws.amazon.com/apigateway/.
- Choose Get Started or, in the Amazon API Gateway navigation pane, choose APIs and choose Create API.
- On the Create page, perform the following steps:
- In Choose Protocol, select REST
- In Create new API, select New API
- Under Settings, type metrics for API name
- Choose Regional for Endpoint Type
- Choose Create API
- Choose Create Resource from the Actions drop-down menu.
- In the New Child Resource window, perform the following steps:
- In Resource Name, type visits
- In Resource Path, type visits
- Select Enable API Gateway CORS
- Choose Create Resource.
- Select the /visits resource and choose Create Resource from the Actions drop-down menu.
- In the New Child Resource window, perform the following steps:
- In Resource Name, type {resourceId}
- In Resource Path, type {resourceId}, replacing the default -resourceId- value
- Check Enable API Gateway CORS
- Choose Create Resource
- Select the /Vists/{resourceId} resource and choose Create Method from the Actions drop-down menu.
- Choose GET in the dropdown and then the checkmark to its right.
- Choose the GET resource method in the /visits/{resourceId} - GET - Setup window:
- In Integration type, choose Lambda Function
- Check Use Lambda Proxy integration
- In Lambda Region, select your region from the dropdown
-
- In Lambda Function, type lambda-dynamo-data-api
- Check Use Default Timeout
- Choose Save
- Choose OK in the Add Permission to Lambda Function. This will allow API Gateway to invoke the Lambda function.
You should now have an API Gateway GET - Method Execution that looks like the following screenshot:
Finally do a quick test to make sure it works before we deploy it by performing the following steps:
- Choose /visits/{resourceId} - GET in the Resources menu on the left
- Choose Test
- Type 324 under Path {resourceId}
- Choose Test
You should see the status 200, latency, logs, and JSON response body, as shown in the following code:
[
{
"EventCount": 3,
"EventDay": 20171001,
"EventId": "324"
},
{
"EventCount": 5,
"EventDay": 20171002,
"EventId": "324"
}
]
If you don't get a 2XX status code, then look at the logs, which will help you diagnose the issue. It will probably be linked to security IAM roles.