How to log Amazon DynamoDB RequestIds

2 minute read
Content level: Advanced
0

Understanding how to log requestIds for troubleshooting with AWS Premium Support.

Overview

Often when troubleshooting issues with AWS Services it is necessary to provide the RequestId to AWS Premium Support. RequestId's are often associated with internal logs, allowing for Support Engineers to identify an individual log allowing them to troubleshoot effectively.

Here, we will showcase how to log RequestId's in each of the AWS SDK's.

Resolution

We will use the DynamoDB Client to showcase how to log RequestIds in each SDK. Each of the below snippets make use of the GetItem API call, however, I ensured to enter an invalid TableName which will result in a ResourceNotFoundExcepion

Java

        try{
            Table table = docClient.getTable("table1");
            GetItemOutcome outcome = table.getItemOutcome("id", "test");

            System.out.println(outcome.toString());

        }catch (Exception e){
            System.out.println(e.getMessage().split(";")[3]);
        }


Javascript / Node SDK v2

      var params = {
          TableName : 'table1',
          Key: {
            id: 'test'
          }
        };

      dynamodb.get(params)
          .promise()
          .then(res => console.log(res))
          .catch(err => console.log(err.requestId));

While there is no way to retrieve requestId from successful requests while using promises, you can modify request.js file to return requestId:

node_modules/aws-sdk/lib/request.js

/**
 * @api private
 */
AWS.Request.addPromisesToClass = function addPromisesToClass(PromiseDependency) {
  this.prototype.promise = function promise() {
    var self = this;
    // append to user agent
    this.httpRequest.appendToUserAgent('promise');
    return new PromiseDependency(function(resolve, reject) {
      self.on('complete', function(resp) {
        if (resp.error) {
          reject(resp.error);
        } else {
          // define $response property so that it is not enumerable
          // this prevents circular reference errors when stringifying the JSON object
          resolve(Object.defineProperty(
            {...resp.data,  requestId: resp.requestId},  // Mody this line (line 791 in SDK v 2.799.0)
            '$response',
            {value: resp}
          ));
        }
      });
      self.runTo();
    });
  };
};

Python / Boto3

In Boto3 we must import ClientError from botocore.exceptions: from botocore.exceptions import ClientError

      try:
          response = client.get_item(
              Key={
                  "id": {
                      "S": "test"
                  }
              },
              TableName="table1"
          )

      except ClientError as error:
          print(error.response['ResponseMetadata']['RequestId'])


Go / Golang

      // Request Format Interface
      type RequestFailure interface {
          error
          StatusCode() int
          RequestID() string
      }

      // DDB Table Name and Item PK
      tableName := "table2"
      id := "test"

      // Call GetItem
      result, err := svc.GetItem(&dynamodb.GetItemInput{
          TableName: aws.String(tableName),
          Key: map[string]*dynamodb.AttributeValue{
              "id": {
                  S: aws.String(id),
              },
          },
      })

      // Handle Errors
      if err != nil {
          if reqerr, ok := err.(RequestFailure); ok {
              fmt.Println("RequestId", reqerr.RequestID())
          } else {
              fmt.Println("Error:", err.Error())
          }
      }

profile pictureAWS
EXPERT
published 23 days ago2015 views