Friday, October 23, 2020

AWS - Enabling/Disabling DynamoDB Stream programmatically

How to Enable/Disable AWS DynamoDB Stream programmatically?

It would be useful for testing purposes, through automation code, to enable or disable AWS DynamoDB Streams to Lambda. Let's see how to achieve that.



           boolean enable = false;//false-to disable, true to enable  
           String dynamoDBTable = "TableName";  
           // Create Async client  
           AmazonDynamoDBAsync ddb = AmazonDynamoDBAsyncClientBuilder.defaultClient();  
           // Create StreamSpecification  
           StreamSpecification streamSpecification = new StreamSpecification();  
           streamSpecification.setStreamEnabled(enable);  
           // StreamViewType is needed only for enabling  
           if (enable) {  
                streamSpecification.setStreamViewType(StreamViewType.NEW_AND_OLD_IMAGES);  
           }  
           // Create update request using the StreamSpecification  
           UpdateTableRequest updateTableRequest = new UpdateTableRequest();  
           updateTableRequest.setTableName(dynamoDBTable);  
           updateTableRequest.setStreamSpecification(streamSpecification);  
           // Execute the update request Asynchronously  
           Future<UpdateTableResult> updateResultFuture = ddb.updateTableAsync(updateTableRequest);  
  • Reattach if any Lambda Trigger back to the Stream otherwise any existing Lambda trigger won't work as they got removed during this process, refer to this Blog
Note: The program is not exiting after executing the request, not sure it's due to wrong implementation of the Async API of 'AmazonDynamoDBAsync' code. Please comment if you have a solution to exit from the program gracefully. I ran the program on Java (11.0.8) with AWS DynamoDB SDK (1.11.881) and also using Java 1.8.0_272 as well with no luck

No comments:

Post a Comment