AWS amazondynamodb documentation change
Summary
Migrated code samples from AWS SDK for Java v1 to v2, updated DynamoDB Streams KCL adapter implementation to use newer Kinesis Client Library (KCL) 2.x APIs and async clients
Security assessment
Changes involve updating SDK versions and API usage patterns without introducing security controls or addressing vulnerabilities. Credential handling moved to default providers but doesn't indicate a security fix. No CVE references or vulnerability mitigations mentioned.
Diff
diff --git a/amazondynamodb/latest/developerguide/Streams.KCLAdapter.Walkthrough.CompleteProgram.md b/amazondynamodb/latest/developerguide/Streams.KCLAdapter.Walkthrough.CompleteProgram.md index fe6d103e7..62ff303a6 100644 --- a//amazondynamodb/latest/developerguide/Streams.KCLAdapter.Walkthrough.CompleteProgram.md +++ b//amazondynamodb/latest/developerguide/Streams.KCLAdapter.Walkthrough.CompleteProgram.md @@ -43,11 +42,0 @@ The source code consists of four `.java` files: - import com.amazonaws.auth.AWSCredentialsProvider; - import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; - import com.amazonaws.regions.Regions; - import com.amazonaws.services.cloudwatch.AmazonCloudWatch; - import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; - import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; - import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; - import com.amazonaws.services.dynamodbv2.AmazonDynamoDBStreams; - import com.amazonaws.services.dynamodbv2.AmazonDynamoDBStreamsClientBuilder; - import com.amazonaws.services.dynamodbv2.model.DeleteTableRequest; - import com.amazonaws.services.dynamodbv2.model.DescribeTableResult; @@ -55,5 +44,16 @@ The source code consists of four `.java` files: - import com.amazonaws.services.dynamodbv2.streamsadapter.StreamsWorkerFactory; - import com.amazonaws.services.kinesis.clientlibrary.interfaces.v2.IRecordProcessorFactory; - import com.amazonaws.services.kinesis.clientlibrary.lib.worker.InitialPositionInStream; - import com.amazonaws.services.kinesis.clientlibrary.lib.worker.KinesisClientLibConfiguration; - import com.amazonaws.services.kinesis.clientlibrary.lib.worker.Worker; + import com.amazonaws.services.dynamodbv2.streamsadapter.StreamsSchedulerFactory; + import com.amazonaws.services.dynamodbv2.streamsadapter.polling.DynamoDBStreamsPollingConfig; + import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; + import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; + import software.amazon.awssdk.regions.Region; + import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient; + import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient; + import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest; + import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse; + import software.amazon.kinesis.common.ConfigsBuilder; + import software.amazon.kinesis.common.InitialPositionInStream; + import software.amazon.kinesis.common.InitialPositionInStreamExtended; + import software.amazon.kinesis.coordinator.Scheduler; + import software.amazon.kinesis.processor.ShardRecordProcessorFactory; + import software.amazon.kinesis.processor.StreamTracker; + import software.amazon.kinesis.retrieval.RetrievalConfig; @@ -62,3 +61,0 @@ The source code consists of four `.java` files: - private static Worker worker; - private static KinesisClientLibConfiguration workerConfig; - private static IRecordProcessorFactory recordProcessorFactory; @@ -66,4 +63,3 @@ The source code consists of four `.java` files: - private static AmazonDynamoDB dynamoDBClient; - private static AmazonCloudWatch cloudWatchClient; - private static AmazonDynamoDBStreams dynamoDBStreamsClient; - private static AmazonDynamoDBStreamsAdapterClient adapterClient; + private static DynamoDbAsyncClient dynamoDbAsyncClient; + private static CloudWatchAsyncClient cloudWatchAsyncClient; + private static AmazonDynamoDBStreamsAdapterClient amazonDynamoDbStreamsAdapterClient; @@ -74 +70,2 @@ The source code consists of four `.java` files: - private static Regions awsRegion = Regions.US_EAST_2; + private static Region region = Region.US_EAST_1; + private static AwsCredentialsProvider credentialsProvider = DefaultCredentialsProvider.create(); @@ -76,5 +72,0 @@ The source code consists of four `.java` files: - private static AWSCredentialsProvider awsCredentialsProvider = DefaultAWSCredentialsProviderChain.getInstance(); - - /** - * @param args - */ @@ -83,6 +75,3 @@ The source code consists of four `.java` files: - - dynamoDBClient = AmazonDynamoDBClientBuilder.standard() - .withRegion(awsRegion) - .build(); - cloudWatchClient = AmazonCloudWatchClientBuilder.standard() - .withRegion(awsRegion) + dynamoDbAsyncClient = DynamoDbAsyncClient.builder() + .credentialsProvider(credentialsProvider) + .region(region) @@ -90,2 +79,3 @@ The source code consists of four `.java` files: - dynamoDBStreamsClient = AmazonDynamoDBStreamsClientBuilder.standard() - .withRegion(awsRegion) + cloudWatchAsyncClient = CloudWatchAsyncClient.builder() + .credentialsProvider(credentialsProvider) + .region(region) @@ -93 +83,2 @@ The source code consists of four `.java` files: - adapterClient = new AmazonDynamoDBStreamsAdapterClient(dynamoDBStreamsClient); + amazonDynamoDbStreamsAdapterClient = new AmazonDynamoDBStreamsAdapterClient(credentialsProvider, region); + @@ -96 +86,0 @@ The source code consists of four `.java` files: - recordProcessorFactory = new StreamsRecordProcessorFactory(dynamoDBClient, destTable); @@ -100,13 +90,34 @@ The source code consists of four `.java` files: - workerConfig = new KinesisClientLibConfiguration("streams-adapter-demo", - streamArn, - awsCredentialsProvider, - "streams-demo-worker") - .withMaxRecords(1000) - .withIdleTimeBetweenReadsInMillis(500) - .withInitialPositionInStream(InitialPositionInStream.TRIM_HORIZON); - - System.out.println("Creating worker for stream: " + streamArn); - worker = StreamsWorkerFactory.createDynamoDbStreamsWorker(recordProcessorFactory, workerConfig, adapterClient, - dynamoDBClient, cloudWatchClient); - System.out.println("Starting worker..."); - Thread t = new Thread(worker); + StreamTracker streamTracker = StreamsSchedulerFactory.createSingleStreamTracker(streamArn, + InitialPositionInStreamExtended.newInitialPosition(InitialPositionInStream.TRIM_HORIZON)); + + ShardRecordProcessorFactory shardRecordProcessorFactory = + new StreamsAdapterDemoProcessorFactory(dynamoDbAsyncClient, destTable); + + ConfigsBuilder configsBuilder = new ConfigsBuilder( + streamTracker, + "streams-adapter-demo", + amazonDynamoDbStreamsAdapterClient, + dynamoDbAsyncClient, + cloudWatchAsyncClient, + "streams-demo-worker", + shardRecordProcessorFactory + ); + + DynamoDBStreamsPollingConfig pollingConfig = new DynamoDBStreamsPollingConfig(amazonDynamoDbStreamsAdapterClient); + RetrievalConfig retrievalConfig = configsBuilder.retrievalConfig(); + retrievalConfig.retrievalSpecificConfig(pollingConfig); + + System.out.println("Creating scheduler for stream " + streamArn); + Scheduler scheduler = StreamsSchedulerFactory.createScheduler( + configsBuilder.checkpointConfig(), + configsBuilder.coordinatorConfig(), + configsBuilder.leaseManagementConfig(), + configsBuilder.lifecycleConfig(), + configsBuilder.metricsConfig(), + configsBuilder.processorConfig(), + retrievalConfig, + amazonDynamoDbStreamsAdapterClient + ); + + System.out.println("Starting scheduler..."); + Thread t = new Thread(scheduler); @@ -115,2 +126,4 @@ The source code consists of four `.java` files: - Thread.sleep(25000); - worker.shutdown(); + Thread.sleep(250000); + + System.out.println("Stopping scheduler..."); + scheduler.shutdown(); @@ -119,2 +132,2 @@ The source code consists of four `.java` files: - if (StreamsAdapterDemoHelper.scanTable(dynamoDBClient, srcTable).getItems() - .equals(StreamsAdapterDemoHelper.scanTable(dynamoDBClient, destTable).getItems())) { + if (StreamsAdapterDemoHelper.scanTable(dynamoDbAsyncClient, srcTable).items() + .equals(StreamsAdapterDemoHelper.scanTable(dynamoDbAsyncClient, destTable).items())) { @@ -133,2 +146,2 @@ The source code consists of four `.java` files: - streamArn = StreamsAdapterDemoHelper.createTable(dynamoDBClient, srcTable); - StreamsAdapterDemoHelper.createTable(dynamoDBClient, destTable); + streamArn = StreamsAdapterDemoHelper.createTable(dynamoDbAsyncClient, srcTable); + StreamsAdapterDemoHelper.createTable(dynamoDbAsyncClient, destTable); @@ -145,2 +158,2 @@ The source code consists of four `.java` files: - DescribeTableResult result = StreamsAdapterDemoHelper.describeTable(dynamoDBClient, tableName); - created = result.getTable().getTableStatus().equals("ACTIVE"); + DescribeTableResponse result = StreamsAdapterDemoHelper.describeTable(dynamoDbAsyncClient, tableName); + created = result.table().tableStatusAsString().equals("ACTIVE"); @@ -164,6 +177,6 @@ The source code consists of four `.java` files: - StreamsAdapterDemoHelper.putItem(dynamoDBClient, tableName, "101", "test1"); - StreamsAdapterDemoHelper.updateItem(dynamoDBClient, tableName, "101", "test2"); - StreamsAdapterDemoHelper.deleteItem(dynamoDBClient, tableName, "101"); - StreamsAdapterDemoHelper.putItem(dynamoDBClient, tableName, "102", "demo3"); - StreamsAdapterDemoHelper.updateItem(dynamoDBClient, tableName, "102", "demo4"); - StreamsAdapterDemoHelper.deleteItem(dynamoDBClient, tableName, "102"); + StreamsAdapterDemoHelper.putItem(dynamoDbAsyncClient, tableName, "101", "test1"); + StreamsAdapterDemoHelper.updateItem(dynamoDbAsyncClient, tableName, "101", "test2"); + StreamsAdapterDemoHelper.deleteItem(dynamoDbAsyncClient, tableName, "101"); + StreamsAdapterDemoHelper.putItem(dynamoDbAsyncClient, tableName, "102", "demo3"); + StreamsAdapterDemoHelper.updateItem(dynamoDbAsyncClient, tableName, "102", "demo4"); + StreamsAdapterDemoHelper.deleteItem(dynamoDbAsyncClient, tableName, "102"); @@ -175,2 +188,2 @@ The source code consists of four `.java` files: - dynamoDBClient.deleteTable(new DeleteTableRequest().withTableName(srcTable)); - dynamoDBClient.deleteTable(new DeleteTableRequest().withTableName(destTable)); + dynamoDbAsyncClient.deleteTable(DeleteTableRequest.builder().tableName(srcTable).build()); + dynamoDbAsyncClient.deleteTable(DeleteTableRequest.builder().tableName(destTable).build()); @@ -188,8 +199,11 @@ The source code consists of four `.java` files: - import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; - import com.amazonaws.services.dynamodbv2.streamsadapter.model.RecordAdapter; - import com.amazonaws.services.kinesis.clientlibrary.interfaces.v2.IRecordProcessor; - import com.amazonaws.services.kinesis.clientlibrary.lib.worker.ShutdownReason; - import com.amazonaws.services.kinesis.clientlibrary.types.InitializationInput; - import com.amazonaws.services.kinesis.clientlibrary.types.ProcessRecordsInput; - import com.amazonaws.services.kinesis.clientlibrary.types.ShutdownInput; - import com.amazonaws.services.kinesis.model.Record; + import com.amazonaws.services.dynamodbv2.streamsadapter.adapter.DynamoDBStreamsClientRecord; + import com.amazonaws.services.dynamodbv2.streamsadapter.model.DynamoDBStreamsProcessRecordsInput; + import com.amazonaws.services.dynamodbv2.streamsadapter.processor.DynamoDBStreamsShardRecordProcessor; + import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient; + import software.amazon.awssdk.services.dynamodb.model.Record; + import software.amazon.kinesis.exceptions.InvalidStateException; + import software.amazon.kinesis.exceptions.ShutdownException; + import software.amazon.kinesis.lifecycle.events.InitializationInput; + import software.amazon.kinesis.lifecycle.events.LeaseLostInput; + import software.amazon.kinesis.lifecycle.events.ShardEndedInput; + import software.amazon.kinesis.lifecycle.events.ShutdownRequestedInput; @@ -197,0 +212,3 @@ The source code consists of four `.java` files: + import java.nio.charset.StandardCharsets; + + public class StreamsRecordProcessor implements DynamoDBStreamsShardRecordProcessor { @@ -199 +215,0 @@ The source code consists of four `.java` files: - public class StreamsRecordProcessor implements IRecordProcessor { @@ -202 +218 @@ The source code consists of four `.java` files: - private final AmazonDynamoDB dynamoDBClient; + private final DynamoDbAsyncClient dynamoDbAsyncClient; @@ -205,2 +221,2 @@ The source code consists of four `.java` files: