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_lightsail_code_examples.md

Summary

Updated the example bash script with security hardening improvements including strict error handling, input validation, resource cleanup reliability, and private key protection.

Security assessment

The changes implement critical security best practices: 1) Added 'set -euo pipefail' for strict error handling preventing undefined behavior 2) Set 'umask 077' to protect private key files from unauthorized access 3) Added IP address format validation to prevent command injection 4) Improved error suppression during cleanup to avoid credential leaks in error messages. These directly address potential security weaknesses in the example script.

Diff

diff --git a/code-library/latest/ug/bash_2_lightsail_code_examples.md b/code-library/latest/ug/bash_2_lightsail_code_examples.md
index 9f8ce22ca..001052cea 100644
--- a//code-library/latest/ug/bash_2_lightsail_code_examples.md
+++ b//code-library/latest/ug/bash_2_lightsail_code_examples.md
@@ -53,0 +54 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    set -euo pipefail
@@ -56 +57 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    export AWS_REGION="us-west-2"
+    export AWS_REGION="${AWS_REGION:-us-west-2}"
@@ -67 +68 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-      echo "ERROR: $1"
+      echo "ERROR: $1" >&2
@@ -69 +70 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-      cleanup_resources
+      cleanup_resources || true
@@ -98 +99 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-      for resource in "${CREATED_RESOURCES[@]}"; do
+      for resource in "${CREATED_RESOURCES[@]+"${CREATED_RESOURCES[@]}"}"; do
@@ -109 +110 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            aws lightsail delete-instance-snapshot --instance-snapshot-name "$name" --region $AWS_REGION
+            aws lightsail delete-instance-snapshot --instance-snapshot-name "$name" --region "$AWS_REGION" 2>/dev/null || true
@@ -113 +114 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            aws lightsail delete-disk-snapshot --disk-snapshot-name "$name" --region $AWS_REGION
+            aws lightsail delete-disk-snapshot --disk-snapshot-name "$name" --region "$AWS_REGION" 2>/dev/null || true
@@ -117,2 +118,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            aws lightsail detach-disk --disk-name "$name" --region $AWS_REGION
-            sleep 10 # Wait for detach to complete
+            aws lightsail detach-disk --disk-name "$name" --region "$AWS_REGION" 2>/dev/null || true
+            sleep 10
@@ -120 +121 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            aws lightsail delete-disk --disk-name "$name" --region $AWS_REGION
+            aws lightsail delete-disk --disk-name "$name" --region "$AWS_REGION" 2>/dev/null || true
@@ -124,2 +125 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            # Check instance state before attempting to delete
-            INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name "$name" --region $AWS_REGION --query 'state.name' --output text 2>/dev/null)
+            INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name "$name" --region "$AWS_REGION" --query 'state.name' --output text 2>/dev/null || echo "unknown")
@@ -133 +133 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name "$name" --region $AWS_REGION --query 'state.name' --output text 2>/dev/null)
+                INSTANCE_STATE=$(aws lightsail get-instance-state --instance-name "$name" --region "$AWS_REGION" --query 'state.name' --output text 2>/dev/null || echo "unknown")
@@ -137 +137 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            aws lightsail delete-instance --instance-name "$name" --region $AWS_REGION
+            aws lightsail delete-instance --instance-name "$name" --region "$AWS_REGION" 2>/dev/null || true
@@ -153 +153 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    aws lightsail get-blueprints --region $AWS_REGION --query 'blueprints[0:5].[blueprintId,name]' --output table
+    aws lightsail get-blueprints --region "$AWS_REGION" --query 'blueprints[0:5].[blueprintId,name]' --output table
@@ -157 +157 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    aws lightsail get-bundles --region $AWS_REGION --query 'bundles[0:5].[bundleId,name,price]' --output table
+    aws lightsail get-bundles --region "$AWS_REGION" --query 'bundles[0:5].[bundleId,name,price]' --output table
@@ -173 +173 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-      --region $AWS_REGION
+      --region "$AWS_REGION"
@@ -179 +178,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # Wait for the instance to be ready (polling approach)
@@ -183 +182 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-      STATUS=$(aws lightsail get-instance-state --instance-name "$INSTANCE_NAME" --region $AWS_REGION --query 'state.name' --output text)
+      STATUS=$(aws lightsail get-instance-state --instance-name "$INSTANCE_NAME" --region "$AWS_REGION" --query 'state.name' --output text 2>/dev/null || echo "unknown")
@@ -199 +198 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    INSTANCE_IP=$(aws lightsail get-instance --instance-name "$INSTANCE_NAME" --region $AWS_REGION --query 'instance.publicIpAddress' --output text)
+    INSTANCE_IP=$(aws lightsail get-instance --instance-name "$INSTANCE_NAME" --region "$AWS_REGION" --query 'instance.publicIpAddress' --output text)
@@ -202,0 +202,5 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    # Validate IP address format
+    if ! [[ "$INSTANCE_IP" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
+      handle_error "Invalid IP address format: $INSTANCE_IP"
+    fi
+    
@@ -206 +210,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    aws lightsail download-default-key-pair --region $AWS_REGION --output text > "$KEY_FILE"
+    umask 077
+    aws lightsail download-default-key-pair --region "$AWS_REGION" --output text > "$KEY_FILE"
@@ -207,0 +213,5 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    
+    if [ ! -f "$KEY_FILE" ] || [ ! -s "$KEY_FILE" ]; then
+      handle_error "Key pair file was not created or is empty"
+    fi
+    
@@ -221 +231 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-      --region $AWS_REGION
+      --region "$AWS_REGION"
@@ -225 +235 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # FIX: Wait for the disk to be available using polling instead of fixed sleep
+    # Wait for the disk to be available using polling
@@ -230 +240 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-      DISK_STATE=$(aws lightsail get-disk --disk-name "$DISK_NAME" --region $AWS_REGION --query 'disk.state' --output text 2>/dev/null)
+      DISK_STATE=$(aws lightsail get-disk --disk-name "$DISK_NAME" --region "$AWS_REGION" --query 'disk.state' --output text 2>/dev/null || echo "unknown")
@@ -250 +260 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-      --region $AWS_REGION
+      --region "$AWS_REGION"
@@ -264 +274 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-      --region $AWS_REGION
+      --region "$AWS_REGION"
@@ -268 +278 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    # FIX: Wait for the snapshot to complete using polling instead of fixed sleep
+    # Wait for the snapshot to complete using polling
@@ -270 +280 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    MAX_ATTEMPTS=60  # Increased timeout for snapshot creation
+    MAX_ATTEMPTS=60
@@ -273,2 +283,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-      SNAPSHOT_STATE=$(aws lightsail get-instance-snapshot --instance-snapshot-name "$SNAPSHOT_NAME" --region $AWS_REGION --query 'instanceSnapshot.state' --output text 2>/dev/null)
-      if [ "$SNAPSHOT_STATE" == "completed" ]; then
+      SNAPSHOT_STATE=$(aws lightsail get-instance-snapshot --instance-snapshot-name "$SNAPSHOT_NAME" --region "$AWS_REGION" --query 'instanceSnapshot.state' --output text 2>/dev/null || echo "unknown")
+      if [ "$SNAPSHOT_STATE" == "available" ]; then
@@ -291 +301 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    for resource in "${CREATED_RESOURCES[@]}"; do
+    for resource in "${CREATED_RESOURCES[@]+"${CREATED_RESOURCES[@]}"}"; do
@@ -295,2 +305 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    read -p "Do you want to clean up these resources? (y/n): " CLEANUP_CONFIRM
-    if [[ "$CLEANUP_CONFIRM" == "y" || "$CLEANUP_CONFIRM" == "Y" ]]; then
+    echo "Cleaning up these resources automatically..."
@@ -298,8 +306,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    else
-      echo "Resources will not be cleaned up. You can manually delete them later."
-      echo "To clean up manually, use the following commands:"
-      echo "aws lightsail delete-instance-snapshot --instance-snapshot-name $SNAPSHOT_NAME --region $AWS_REGION"
-      echo "aws lightsail detach-disk --disk-name $DISK_NAME --region $AWS_REGION"
-      echo "aws lightsail delete-disk --disk-name $DISK_NAME --region $AWS_REGION"
-      echo "aws lightsail delete-instance --instance-name $INSTANCE_NAME --region $AWS_REGION"
-    fi