AWS Security ChangesHomeSearch

AWS code-library high security documentation change

Service: code-library · 2026-05-01 · Security-related high

File: code-library/latest/ug/bash_2_cloudwatch-logs_code_examples.md

Summary

Enhanced the script with improved error handling, input validation, and security measures. Changes include: added input validation functions, improved error checking in Lambda code, secure temporary directory handling, enhanced random ID generation, non-interactive defaults, and resource cleanup automation.

Security assessment

Added input validation (validate_input function) to prevent malformed/malicious inputs in IAM roles and test events. Implemented strict type/value checking in Lambda functions to reject invalid inputs. Made TEMP_DIR read-only to prevent tampering. Improved random ID generation using cryptographic randomness. Added multiple error checks to prevent partial deployments. These changes mitigate risks of injection attacks, data tampering, and insecure resource handling.

Diff

diff --git a/code-library/latest/ug/bash_2_cloudwatch-logs_code_examples.md b/code-library/latest/ug/bash_2_cloudwatch-logs_code_examples.md
index 2caeb2556..2ba62e726 100644
--- a//code-library/latest/ug/bash_2_cloudwatch-logs_code_examples.md
+++ b//code-library/latest/ug/bash_2_cloudwatch-logs_code_examples.md
@@ -627 +627 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        read -r CLEANUP_CHOICE
+        CLEANUP_CHOICE="y"
@@ -744 +744 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    set -eE
+    set -eE -o pipefail
@@ -750 +750 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    UNIQUE_ID=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 8 | head -n 1)
+    UNIQUE_ID=$(head -c 8 /dev/urandom | od -An -tx1 | tr -d ' ')
@@ -755,0 +756 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    readonly TEMP_DIR
@@ -864,0 +866,10 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    validate_input() {
+        local input="$1"
+        local pattern="$2"
+        if ! [[ "$input" =~ $pattern ]]; then
+            echo "ERROR: Invalid input: $input"
+            return 1
+        fi
+        return 0
+    }
+    
@@ -889,2 +900,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    echo "Enter your choice (1 or 2): "
-    read -r RUNTIME_CHOICE
+    echo "Using default: Python 3.13"
+    RUNTIME_CHOICE="1"
@@ -903,2 +914,7 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        length = event['length']
-        width = event['width']
+        if not isinstance(event, dict) or 'length' not in event or 'width' not in event:
+            raise ValueError('Event must contain length and width')
+        try:
+            length = float(event['length'])
+            width = float(event['width'])
+            if length < 0 or width < 0:
+                raise ValueError('Length and width must be non-negative')
@@ -908,0 +925,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+        except (TypeError, ValueError) as e:
+            logger.error(f'Error processing input: {str(e)}')
+            raise
@@ -919,0 +939,6 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+      if (!event || typeof event.length !== 'number' || typeof event.width !== 'number') {
+        throw new Error('Event must contain numeric length and width');
+      }
+      if (event.length < 0 || event.width < 0) {
+        throw new Error('Length and width must be non-negative');
+      }
@@ -962,0 +988,6 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    
+    if ! validate_input "$ROLE_OUTPUT" "^arn:aws:iam::[0-9]+:role/"; then
+        echo "ERROR: Failed to create IAM role"
+        exit 1
+    fi
+    
@@ -992,3 +1023,11 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    cd "$TEMP_DIR"
-    zip -j function.zip "$CODE_FILE" > /dev/null 2>&1
-    cd "$ORIGINAL_DIR"
+    cd "$TEMP_DIR" || exit 1
+    zip -j function.zip "$CODE_FILE" > /dev/null 2>&1 || {
+        echo "ERROR: Failed to create deployment package"
+        exit 1
+    }
+    cd "$ORIGINAL_DIR" || exit 1
+    
+    if [ ! -f "${TEMP_DIR}/function.zip" ]; then
+        echo "ERROR: Deployment package creation failed"
+        exit 1
+    fi
@@ -1009,0 +1049,6 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    
+    if [ -z "$CREATE_OUTPUT" ]; then
+        echo "ERROR: Failed to create Lambda function"
+        exit 1
+    fi
+    
@@ -1032,0 +1078,5 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    if ! validate_input "$TEST_EVENT" '"length": [0-9]+, "width": [0-9]+'; then
+        echo "ERROR: Invalid test event format"
+        exit 1
+    fi
+    
@@ -1039,0 +1090,5 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    if [ ! -f "${TEMP_DIR}/response.json" ]; then
+        echo "ERROR: No response file generated"
+        exit 1
+    fi
+    
@@ -1113 +1168 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    echo "CLEANUP CONFIRMATION"
+    echo "CLEANUP"
@@ -1116,4 +1171 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    echo "Do you want to clean up all created resources? (y/n): "
-    read -r CLEANUP_CHOICE
-    
-    if [[ "$CLEANUP_CHOICE" =~ ^[Yy]$ ]]; then
+    echo "Cleaning up all created resources..."
@@ -1121,19 +1172,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    else
-        echo ""
-        echo "Resources were NOT deleted. To clean up manually, run:"
-        echo ""
-        echo "  # Delete the Lambda function"
-        echo "  aws lambda delete-function --function-name ${FUNCTION_NAME}"
-        echo ""
-        echo "  # Delete the CloudWatch log group"
-        echo "  aws logs delete-log-group --log-group-name ${LOG_GROUP_NAME}"
-        echo ""
-        echo "  # Detach the policy and delete the IAM role"
-        echo "  aws iam detach-role-policy --role-name ${ROLE_NAME} --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
-        echo "  aws iam delete-role --role-name ${ROLE_NAME}"
-        echo ""
-    
-        if [ -d "$TEMP_DIR" ]; then
-            rm -rf "$TEMP_DIR"
-        fi
-    fi