AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-03-10 · Documentation low

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

Summary

Added Java example for configuring SQS server-side encryption with KMS

Security assessment

Demonstrates security feature implementation (SSE with KMS) but does not address a specific security issue.

Diff

diff --git a/code-library/latest/ug/sqs_example_sqs_SetQueueAttributes_section.md
index e67179181..0e1ad6edc 100644
--- a/code-library/latest/ug/sqs_example_sqs_SetQueueAttributes_section.md
+++ b/code-library/latest/ug/sqs_example_sqs_SetQueueAttributes_section.md
@@ -367,0 +368,57 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+Java
+    
+
+**SDK for Java 2.x**
+    
+
+###### Note
+
+There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/sqs#code-examples). 
+
+Configure an Amazon SQS to use server-side encryption (SSE) using a custom KMS key.
+    
+    
+        public static void addEncryption(String queueName, String kmsMasterKeyAlias) {
+            SqsClient sqsClient = SqsClient.create();
+    
+            GetQueueUrlRequest urlRequest = GetQueueUrlRequest.builder()
+                    .queueName(queueName)
+                    .build();
+    
+            GetQueueUrlResponse getQueueUrlResponse;
+            try {
+                getQueueUrlResponse = sqsClient.getQueueUrl(urlRequest);
+            } catch (QueueDoesNotExistException e) {
+                LOGGER.error(e.getMessage(), e);
+                throw new RuntimeException(e);
+            }
+            String queueUrl = getQueueUrlResponse.queueUrl();
+    
+    
+            Map<QueueAttributeName, String> attributes = Map.of(
+                    QueueAttributeName.KMS_MASTER_KEY_ID, kmsMasterKeyAlias,
+                    QueueAttributeName.KMS_DATA_KEY_REUSE_PERIOD_SECONDS, "140" // Set the data key reuse period to 140 seconds.
+            );                                                                  // This is how long SQS can reuse the data key before requesting a new one from KMS.
+    
+            SetQueueAttributesRequest attRequest = SetQueueAttributesRequest.builder()
+                    .queueUrl(queueUrl)
+                    .attributes(attributes)
+                    .build();
+            try {
+                sqsClient.setQueueAttributes(attRequest);
+                LOGGER.info("The attributes have been applied to {}", queueName);
+            } catch (InvalidAttributeNameException | InvalidAttributeValueException e) {
+                LOGGER.error(e.getMessage(), e);
+                throw new RuntimeException(e);
+            } finally {
+                sqsClient.close();
+            }
+        }
+    
+    
+
+  * For API details, see [SetQueueAttributes](https://docs.aws.amazon.com/goto/SdkForJavaV2/sqs-2012-11-05/SetQueueAttributes) in _AWS SDK for Java 2.x API Reference_. 
+
+
+
+