Manually testing the serverless microservice

The steps for testing are 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 | lambda-dynamo-data-apiStages.
  1. Select GET under Prod/visits/{resourceId}/GET to get the invoke URL, which should look like https://{restapi_id}.execute-api.{region}.amazonaws.com/Prod/visits/{resourceId}.
  2. Open a new browser tab and enter the https://{restapi_id}.execute-api.{region}.amazonaws.com/Prod/visits/{resourceId} URL. You will get the {"message":"resource_id not a number"} response body. This is because we validated resource_id in the parse_parameters() URL function before querying DynamoDB to make sure that it is a number.
  3. Open a new browser tab and enter the https://{restapi_id}.execute-api.{region}.amazonaws.com/Prod/visits/324 URL. As we have used the correct resourceId, you should see [ ] in your browser tab.

Why are we getting no data?

Well, no data has been loaded into the user-visits-sam DynamoDB table, that is why!

Run python3 ./aws_dynamo/dynamo_modify_items.py to load some records into the user-visits-sam DynamoDB table.

Here are the contents of dynamo_modify_items.py:

from boto3 import resource

class DynamoRepository:
    def __init__(self, target_dynamo_table, region='eu-west-1'):
        self.dynamodb = resource(service_name='dynamodb', region_name=region)
        self.target_dynamo_table = target_dynamo_table
        self.table = self.dynamodb.Table(self.target_dynamo_table)

    def update_dynamo_event_counter(self, event_name, 
event_datetime, event_count=1): return self.table.update_item( Key={ 'EventId': event_name, 'EventDay': event_datetime }, ExpressionAttributeValues={":eventCount": event_count}, UpdateExpression="ADD EventCount :eventCount") def main(): table_name = 'user-visits-sam' dynamo_repo = DynamoRepository(table_name) print(dynamo_repo.update_dynamo_event_counter('324', 20171001)) print(dynamo_repo.update_dynamo_event_counter('324', 20171001, 2)) print(dynamo_repo.update_dynamo_event_counter('324', 20171002, 5)) if __name__ == '__main__': main()

Now go to the same endpoint in the browser, and you should get the following data back:

[{"EventCount": 3, "EventDay": 20171001, "EventId": "324"}, {"EventCount": 5, "EventDay": 20171002, "EventId": "324"}]

Open a new browser tab and enter the https://{restapi_id}.execute-api.{region}.amazonaws.com/Prod/visits/324?startDate=20171002 URL. As we have added the startDate=20171002 parameter, you should see the following in your browser tab:

[{"EventCount": 5, "EventDay": 20171002, "EventId": "324"}]
..................Content has been hidden....................

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