AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-06-28 · Documentation low

File: code-library/latest/ug/java_2_dynamodb_code_examples.md

Summary

Added documentation and code examples for managing DynamoDB global tables with replicas across regions, including creation, replica management, and verification

Security assessment

The changes demonstrate operational management of DynamoDB global tables but do not address security vulnerabilities or introduce security-specific features. The code examples focus on replication configuration and table management without mentioning security controls like encryption, access policies, or authentication mechanisms.

Diff

diff --git a/code-library/latest/ug/java_2_dynamodb_code_examples.md b/code-library/latest/ug/java_2_dynamodb_code_examples.md
index 7aed400d3..31e32f583 100644
--- a//code-library/latest/ug/java_2_dynamodb_code_examples.md
+++ b//code-library/latest/ug/java_2_dynamodb_code_examples.md
@@ -3818,0 +3819,385 @@ The following code example shows how to create an item with TTL.
+The following code example shows how to create and manage DynamoDB global tables with replicas across multiple Regions.
+
+  * Create a table with Global Secondary Index and DynamoDB Streams.
+
+  * Add replicas in different Regions to create a global table.
+
+  * Remove replicas from a global table.
+
+  * Add test items to verify replication across Regions.
+
+  * Describe global table configuration and replica status.
+
+
+
+
+**SDK for Java 2.x**
+    
+
+Create a table with Global Secondary Index and DynamoDB Streams using AWS SDK for Java 2.x.
+    
+    
+        public static CreateTableResponse createTableWithGSI(
+            final DynamoDbClient dynamoDbClient, final String tableName, final String indexName) {
+    
+            if (dynamoDbClient == null) {
+                throw new IllegalArgumentException("DynamoDB client cannot be null");
+            }
+            if (tableName == null || tableName.trim().isEmpty()) {
+                throw new IllegalArgumentException("Table name cannot be null or empty");
+            }
+            if (indexName == null || indexName.trim().isEmpty()) {
+                throw new IllegalArgumentException("Index name cannot be null or empty");
+            }
+    
+            try {
+                LOGGER.info("Creating table: " + tableName + " with GSI: " + indexName);
+    
+                CreateTableRequest createTableRequest = CreateTableRequest.builder()
+                    .tableName(tableName)
+                    .attributeDefinitions(
+                        AttributeDefinition.builder()
+                            .attributeName("Artist")
+                            .attributeType(ScalarAttributeType.S)
+                            .build(),
+                        AttributeDefinition.builder()
+                            .attributeName("SongTitle")
+                            .attributeType(ScalarAttributeType.S)
+                            .build())
+                    .keySchema(
+                        KeySchemaElement.builder()
+                            .attributeName("Artist")
+                            .keyType(KeyType.HASH)
+                            .build(),
+                        KeySchemaElement.builder()
+                            .attributeName("SongTitle")
+                            .keyType(KeyType.RANGE)
+                            .build())
+                    .billingMode(BillingMode.PAY_PER_REQUEST)
+                    .globalSecondaryIndexes(GlobalSecondaryIndex.builder()
+                        .indexName(indexName)
+                        .keySchema(KeySchemaElement.builder()
+                            .attributeName("SongTitle")
+                            .keyType(KeyType.HASH)
+                            .build())
+                        .projection(
+                            Projection.builder().projectionType(ProjectionType.ALL).build())
+                        .build())
+                    .streamSpecification(StreamSpecification.builder()
+                        .streamEnabled(true)
+                        .streamViewType(StreamViewType.NEW_AND_OLD_IMAGES)
+                        .build())
+                    .build();
+    
+                CreateTableResponse response = dynamoDbClient.createTable(createTableRequest);
+                LOGGER.info("Table creation initiated. Status: "
+                    + response.tableDescription().tableStatus());
+    
+                return response;
+    
+            } catch (DynamoDbException e) {
+                LOGGER.severe("Failed to create table: " + tableName + " - " + e.getMessage());
+                throw e;
+            }
+        }
+    
+    
+
+Wait for a table to become active using AWS SDK for Java 2.x.
+    
+    
+        public static void waitForTableActive(final DynamoDbClient dynamoDbClient, final String tableName) {
+    
+            if (dynamoDbClient == null) {
+                throw new IllegalArgumentException("DynamoDB client cannot be null");
+            }
+            if (tableName == null || tableName.trim().isEmpty()) {
+                throw new IllegalArgumentException("Table name cannot be null or empty");
+            }
+    
+            try {
+                LOGGER.info("Waiting for table to become active: " + tableName);
+    
+                try (DynamoDbWaiter waiter =
+                    DynamoDbWaiter.builder().client(dynamoDbClient).build()) {
+                    DescribeTableRequest request =
+                        DescribeTableRequest.builder().tableName(tableName).build();
+    
+                    waiter.waitUntilTableExists(request);
+                    LOGGER.info("Table is now active: " + tableName);
+                }
+    
+            } catch (DynamoDbException e) {
+                LOGGER.severe("Failed to wait for table to become active: " + tableName + " - " + e.getMessage());
+                throw e;
+            }
+        }
+    
+    
+
+Add a replica to create or extend a global table using AWS SDK for Java 2.x.
+    
+    
+        public static UpdateTableResponse addReplica(
+            final DynamoDbClient dynamoDbClient,
+            final String tableName,
+            final Region replicaRegion,
+            final String indexName,
+            final Long readCapacity) {
+    
+            if (dynamoDbClient == null) {
+                throw new IllegalArgumentException("DynamoDB client cannot be null");
+            }
+            if (tableName == null || tableName.trim().isEmpty()) {
+                throw new IllegalArgumentException("Table name cannot be null or empty");
+            }
+            if (replicaRegion == null) {
+                throw new IllegalArgumentException("Replica region cannot be null");
+            }
+            if (indexName == null || indexName.trim().isEmpty()) {
+                throw new IllegalArgumentException("Index name cannot be null or empty");
+            }
+            if (readCapacity == null || readCapacity <= 0) {
+                throw new IllegalArgumentException("Read capacity must be a positive number");
+            }
+    
+            try {
+                LOGGER.info("Adding replica in region: " + replicaRegion.id() + " for table: " + tableName);
+    
+                // Create a ReplicationGroupUpdate for adding a replica
+                ReplicationGroupUpdate replicationGroupUpdate = ReplicationGroupUpdate.builder()
+                    .create(builder -> builder.regionName(replicaRegion.id())
+                        .globalSecondaryIndexes(ReplicaGlobalSecondaryIndex.builder()
+                            .indexName(indexName)
+                            .provisionedThroughputOverride(ProvisionedThroughputOverride.builder()
+                                .readCapacityUnits(readCapacity)
+                                .build())
+                            .build())
+                        .build())
+                    .build();
+    
+                UpdateTableRequest updateTableRequest = UpdateTableRequest.builder()
+                    .tableName(tableName)
+                    .replicaUpdates(replicationGroupUpdate)
+                    .build();
+    
+                UpdateTableResponse response = dynamoDbClient.updateTable(updateTableRequest);
+                LOGGER.info("Replica addition initiated in region: " + replicaRegion.id());
+    
+                return response;
+    
+            } catch (DynamoDbException e) {
+                LOGGER.severe("Failed to add replica in region: " + replicaRegion.id() + " - " + e.getMessage());
+                throw e;
+            }
+        }
+    
+    
+
+Remove a replica from a global table using AWS SDK for Java 2.x.
+    
+    
+        public static UpdateTableResponse removeReplica(
+            final DynamoDbClient dynamoDbClient, final String tableName, final Region replicaRegion) {
+    
+            if (dynamoDbClient == null) {
+                throw new IllegalArgumentException("DynamoDB client cannot be null");
+            }
+            if (tableName == null || tableName.trim().isEmpty()) {
+                throw new IllegalArgumentException("Table name cannot be null or empty");
+            }
+            if (replicaRegion == null) {
+                throw new IllegalArgumentException("Replica region cannot be null");
+            }
+    
+            try {