Now that we know the API Gateway integration with the Lambda function works, we will deploy it and get the URL. The architecture is shown in the following diagram:
The workings of this architecture is as follows:
- Sign in to the AWS Management Console and open the API Gateway console at https://console.aws.amazon.com/apigateway/.
- In the Amazon API Gateway navigation pane, choose APIs and metrics.
- Select Resources under metrics and /Vists/{resourceId}, and choose Deploy API from the Actions drop-down menu.
- In the Deploy API pop-up window, perform the following steps:
- In Deployment stage, choose [New Stage]
- In Stage name, type prod
- In Stage description, type prod
- Select Deploy
- The Stages under metrics should be automatically selected on the left-hand menu.
- Select GET under prod/visits/{resourceId}/GET to get the invoke URL. The invoke URL should look like this: https://{restapi_id}.execute-api.{region}.amazonaws.com/prod/visits/{resourceId}.
- Open a new browser tab and paste in the invoke URL: https://{restapi_id}.execute-api.{region}.amazonaws.com/Prod/visits/{resourceId}:
- The response body will be {"message": "Internal server error"}
- This is because we validated resource_id in the URL parse_parameters() function before querying DynamoDB to make sure that it is a number
- Open a new browser tab and paste in the invoke URL: https://{restapi_id}.execute-api.{region}.amazonaws.com/prod/visits/324. As we have used the correct resourceId or EventId internally, you should see the following code in your browser tab:
[{
"EventCount": 3,
"EventDay": 20171001,
"EventId": "324"
},
{
"EventCount": 5,
"EventDay": 20171002,
"EventId": "324"
}]
- Open a new browser tab and paste in the invoke URL: https://{restapi_id}.execute-api.{region}.amazonaws.com/Prod/visits/324?startDate=20171002. As we have added the startDate=20171002 parameter, you should see the following code in your browser tab:
[{"EventCount": 5, "EventDay": 20171002, "EventId": "324"}]
That is using the other query_by_partition_and_sort_key() method in the Lambda with the startDate.
We now have a fully working serverless data API, with the ability to run different types of queries on DynamoDB.