AWS Security ChangesHomeSearch

AWS amazondynamodb documentation change

Service: amazondynamodb · 2025-04-03 · Documentation low

File: amazondynamodb/latest/developerguide/example_dynamodb_PutItemTTL_section.md

Summary

Updated Java code example for DynamoDB PutItem with TTL to improve code structure, add input validation, error handling, and documentation comments. Introduced constants, separated concerns into methods, and enhanced exception handling.

Security assessment

Changes focus on code quality improvements and error handling rather than addressing security vulnerabilities. No evidence of patching exploits, mitigating attacks, or addressing CVEs. TTL implementation is a standard data lifecycle feature, not a security control.

Diff

diff --git a/amazondynamodb/latest/developerguide/example_dynamodb_PutItemTTL_section.md b/amazondynamodb/latest/developerguide/example_dynamodb_PutItemTTL_section.md
index fbafca441..d3a2bac28 100644
--- a//amazondynamodb/latest/developerguide/example_dynamodb_PutItemTTL_section.md
+++ b//amazondynamodb/latest/developerguide/example_dynamodb_PutItemTTL_section.md
@@ -17,0 +18,6 @@ Java
+    import java.util.HashMap;
+    import java.util.Map;
+    import java.util.Optional;
+    
+    import com.amazon.samplelib.CodeSampleUtils;
+    
@@ -25,5 +30,0 @@ Java
-    import software.amazon.awssdk.utils.ImmutableMap;
-    
-    import java.io.Serializable;
-    import java.util.Map;
-    import java.util.Optional;
@@ -30,0 +32,4 @@ Java
+    /**
+     * Creates an item in a DynamoDB table with TTL attributes.
+     * This class demonstrates how to add TTL expiration timestamps to DynamoDB items.
+     */
@@ -32,2 +37,2 @@ Java
-        public static void main(String[] args) {
-            final String usage = """
+    
+        private static final String USAGE = """
@@ -42,3 +47,38 @@ Java
-            // Optional "region" parameter - if args list length is NOT 3 or 4, short-circuit exit.
-            if (!(args.length == 3 || args.length == 4)) {
-                System.out.println(usage);
+        private static final int DAYS_TO_EXPIRE = 90;
+        private static final int SECONDS_PER_DAY = 24 * 60 * 60;
+        private static final String PRIMARY_KEY_ATTR = "primaryKey";
+        private static final String SORT_KEY_ATTR = "sortKey";
+        private static final String CREATION_DATE_ATTR = "creationDate";
+        private static final String EXPIRE_AT_ATTR = "expireAt";
+        private static final String SUCCESS_MESSAGE = "%s PutItem operation with TTL successful.";
+        private static final String TABLE_NOT_FOUND_ERROR = "Error: The Amazon DynamoDB table \"%s\" can't be found.";
+    
+        private final DynamoDbClient dynamoDbClient;
+    
+        /**
+         * Constructs a CreateTTL instance with the specified DynamoDB client.
+         *
+         * @param dynamoDbClient The DynamoDB client to use
+         */
+        public CreateTTL(final DynamoDbClient dynamoDbClient) {
+            this.dynamoDbClient = dynamoDbClient;
+        }
+    
+        /**
+         * Constructs a CreateTTL with a default DynamoDB client.
+         */
+        public CreateTTL() {
+            this.dynamoDbClient = null;
+        }
+    
+        /**
+         * Main method to demonstrate creating an item with TTL.
+         *
+         * @param args Command line arguments
+         */
+        public static void main(final String[] args) {
+            try {
+                int result = new CreateTTL().processArgs(args);
+                System.exit(result);
+            } catch (Exception e) {
+                System.err.println(e.getMessage());
@@ -46,0 +87 @@ Java
+        }
@@ -48,4 +89,29 @@ Java
-            String tableName = args[0];
-            String primaryKey = args[1];
-            String sortKey = args[2];
-            Region region = Optional.ofNullable(args[3]).isEmpty() ? Region.US_EAST_1 : Region.of(args[3]);
+        /**
+         * Process command line arguments and create an item with TTL.
+         *
+         * @param args Command line arguments
+         * @return 0 if successful, non-zero otherwise
+         * @throws ResourceNotFoundException If the table doesn't exist
+         * @throws DynamoDbException If an error occurs during the operation
+         * @throws IllegalArgumentException If arguments are invalid
+         */
+        public int processArgs(final String[] args) {
+            // Argument validation (remove or replace this line when reusing this code)
+            CodeSampleUtils.validateArgs(args, new int[]{3, 4}, USAGE);
+            
+            final String tableName = args[0];
+            final String primaryKey = args[1];
+            final String sortKey = args[2];
+            final Region region = Optional.ofNullable(args.length > 3 ? args[3] : null)
+                    .map(Region::of)
+                    .orElse(Region.US_EAST_1);
+    
+            try (DynamoDbClient ddb = dynamoDbClient != null ? dynamoDbClient : 
+                    DynamoDbClient.builder().region(region).build()) {
+                final CreateTTL createTTL = new CreateTTL(ddb);
+                createTTL.createItemWithTTL(tableName, primaryKey, sortKey);
+                return 0;
+            } catch (Exception e) {
+                throw e;
+            }
+        }
@@ -52,0 +119,13 @@ Java
+        /**
+         * Creates an item in the specified table with TTL attributes.
+         *
+         * @param tableName The name of the table
+         * @param primaryKeyValue The value for the primary key
+         * @param sortKeyValue The value for the sort key
+         * @return The response from the PutItem operation
+         * @throws ResourceNotFoundException If the table doesn't exist
+         * @throws DynamoDbException If an error occurs during the operation
+         */
+        public PutItemResponse createItemWithTTL(final String tableName, 
+                                                 final String primaryKeyValue, 
+                                                 final String sortKeyValue) {
@@ -57 +136,7 @@ Java
-            final long expireDate = createDate + (90 * 24 * 60 * 60);
+            final long expireDate = createDate + (DAYS_TO_EXPIRE * SECONDS_PER_DAY);
+    
+            final Map<String, AttributeValue> itemMap = new HashMap<>();
+            itemMap.put(PRIMARY_KEY_ATTR, AttributeValue.builder().s(primaryKeyValue).build());
+            itemMap.put(SORT_KEY_ATTR, AttributeValue.builder().s(sortKeyValue).build());
+            itemMap.put(CREATION_DATE_ATTR, AttributeValue.builder().n(String.valueOf(createDate)).build());
+            itemMap.put(EXPIRE_AT_ATTR, AttributeValue.builder().n(String.valueOf(expireDate)).build());
@@ -59,5 +143,0 @@ Java
-            final ImmutableMap<String, ? extends Serializable> itemMap =
-                    ImmutableMap.of("primaryKey", primaryKey,
-                    "sortKey", sortKey,
-                    "creationDate", createDate,
-                    "expireAt", expireDate);
@@ -66 +146 @@ Java
-                    .item((Map<String, AttributeValue>) itemMap)
+                    .item(itemMap)
@@ -68,6 +148,5 @@ Java
-            try (DynamoDbClient ddb = DynamoDbClient.builder()
-                    .region(region)
-                    .build()) {
-                final PutItemResponse response = ddb.putItem(request);
-                System.out.println(tableName + " PutItem operation with TTL successful. Request id is "
-                        + response.responseMetadata().requestId());
+                    
+            try {
+                final PutItemResponse response = dynamoDbClient.putItem(request);
+                System.out.println(String.format(SUCCESS_MESSAGE, tableName));
+                return response;
@@ -75,2 +154,2 @@ Java
-                System.err.format("Error: The Amazon DynamoDB table \"%s\" can't be found.\n", tableName);
-                System.exit(1);
+                System.err.format(TABLE_NOT_FOUND_ERROR, tableName);
+                throw e;
@@ -79 +158 @@ Java
-                System.exit(1);
+                throw e;
@@ -81 +159,0 @@ Java
-            System.exit(0);