Override aws-request-id in java sdk

0

Hello i'm using dynamo db java sdk(v2.29.17) is it possible to pass custom aws-request-id?

asked a month ago79 views
3 Answers
1
Accepted Answer

RequestIds are generated by the service, not the client side SDK, this is why it is being overwritten.

Curious as to why you need to use a custom requestId?

profile pictureAWS
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
  • I dont want my record to be overridden hence i'm catching the exception, but when the request times out (After the leader gets the record) the client automatically retries and the second attempt fails with this exception. My idea is to set the request-id to know when this happens as the request-id will be different

  • You shouldn't try it like this. Try one of these methods:

    1. Use optimistic locking.
    2. Use transactions with a ClientRequestToken.
0

Hey thanks for your reply, I'm trying this but not working for me


 AwsRequestOverrideConfiguration awsRequestOverrideConfiguration = AwsRequestOverrideConfiguration.builder()
                .putHeader("aws-request-id", "123345")
                .build();
        PutItemRequest putItemRequest = PutItemRequest.builder()
                .item(stringAttributeValueMap)
                .tableName(getTableName())
                .overrideConfiguration(awsRequestOverrideConfiguration)
                //.overrideConfiguration(s -> s.putHeader("x-amz-request-id", awsRequestId))
                .returnValuesOnConditionCheckFailure(ReturnValuesOnConditionCheckFailure.ALL_OLD)
                .conditionExpression(expression.expression())
                .build();

 try {
dbclient.putItem(putItemRequest);
        } catch (ConditionalCheckFailedException exception) {
               exception.requestId() // not what i've sent
            throw new RecordAlreadyExists("value %s for field %s already exists ".formatted("uniqueKeyAttributeName", "uniqueKeyAttributeName"),
                    exception);
        }

answered a month ago
-1

Yes, it is possible to pass a custom AWS request ID when using the DynamoDB Java SDK (v2.29.17). You can achieve this by using the request override configuration feature provided by the SDK.

To set a custom AWS request ID, you can use the AwsRequestOverrideConfiguration class. This class allows you to add custom headers to your request, including the 'aws-request-id' header.

Here's how you can do it:

  1. Create an AwsRequestOverrideConfiguration object with your custom request ID.
  2. Add this configuration to your DynamoDB request using the overrideConfiguration() method.

For example, if you're using the ExecuteStatementRequest.Builder, you can do something like this:

String customRequestId = "your-custom-request-id";

AwsRequestOverrideConfiguration overrideConfig = AwsRequestOverrideConfiguration.builder()
    .putHeader("aws-request-id", customRequestId)
    .build();

ExecuteStatementRequest request = ExecuteStatementRequest.builder()
    .statement("Your PartiQL statement")
    .overrideConfiguration(overrideConfig)
    .build();

This approach allows you to set a custom AWS request ID for your DynamoDB operations. However, it's important to note that while you can set this custom ID, the service may still generate its own request ID for internal tracking purposes.

Remember that each request includes a request ID by default, which can be useful for diagnosing issues with AWS Support. If you're overriding this ID, make sure to keep track of both your custom ID and the one returned by AWS for comprehensive request tracking.
Sources
DescribeImportRequest.Builder (AWS SDK for Java - 2.21.29)
ExecuteStatementRequest.Builder (AWS SDK for Java - 2.21.29)
Programming DynamoDB with the AWS SDK for Java 2.x - Amazon DynamoDB

profile picture
answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions