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.
- Make sure you have setup AWS Client credentials (refer: https://balatamilmani.blogspot.com/2020/10/setting-up-aws-client-credentials.html)
- Execute the following code, set variable 'enable' to false to Disable the Stream and true to enable.
- Complete source code can be checked out from https://github.com/balatamilmani/aws-repo.git (Class: DynamoDBDisableStream.java)
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
No comments:
Post a Comment