Connecting API Gateway, Lambda, and DynamoDB

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:

  1. Sign in to the AWS Management Console and open the API Gateway console at https://console.aws.amazon.com/apigateway/.
  2. In the Amazon API Gateway navigation pane, choose APIs and metrics.
  3. Select Resources under metrics and /Vists/{resourceId}, and choose Deploy API from the Actions drop-down menu.
  4. In the Deploy API pop-up window, perform the following steps:
    1. In Deployment stage, choose [New Stage]
    2. In Stage name, type prod
    3. In Stage description, type prod
    4. Select Deploy
  5. The Stages under metrics should be automatically selected on the left-hand menu.
  6. 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}.
  7. 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
  1. 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"
}]
  1. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset