Skip to content

Commit 3766e01

Browse files
authored
Merge pull request #80 from aws-samples/fix/set-u-array-expansion
Fix set -u empty array expansion in 9 scripts
2 parents b8bc0d1 + 7388f94 commit 3766e01

10 files changed

Lines changed: 79 additions & 77 deletions

File tree

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
*.log
2+
*.pem
3+
aws-eump-logs/
4+
dynamodb-tutorial-logs/
5+
Dockerfile
6+
document.png
7+
dashboard-body-*.json
8+
comprehend-policy.json
9+
hello-world.json
10+
input.json
11+
query-results.csv
12+
sentiment-*.json
13+
step-functions-trust-policy.json
14+
stepfunctions-policy.json
15+
textract-*.json
16+
updated-hello-world.json
17+
webserver-template-*.yaml
18+
qbusiness-*.json
19+
datazone_script_v3_fixed.log
20+
idc_setup_*.log
21+
workspaces_creation.log

tuts/001-lightsail-gs/lightsail-gs.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ track_resource() {
4848
# Function to clean up resources
4949
cleanup_resources() {
5050
echo "Resources created by this script:"
51-
for resource in "${CREATED_RESOURCES[@]}"; do
51+
for resource in "${CREATED_RESOURCES[@]+"${CREATED_RESOURCES[@]}"}"; do
5252
echo " $resource"
5353
done
5454

@@ -250,7 +250,7 @@ fi
250250
# Step 7: Clean up resources
251251
echo "Step 7: Clean up resources"
252252
echo "The script has created the following resources:"
253-
for resource in "${CREATED_RESOURCES[@]}"; do
253+
for resource in "${CREATED_RESOURCES[@]+"${CREATED_RESOURCES[@]}"}"; do
254254
echo " $resource"
255255
done
256256

tuts/015-vpc-peering/vpc-peering.sh

Lines changed: 47 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -166,54 +166,50 @@ echo "Setting up VPC peering connection..."
166166
# Validate AWS CLI
167167
validate_aws_cli
168168

169-
# Check for existing VPCs
170-
echo "Checking for existing VPCs..."
171-
EXISTING_VPCS=$(aws ec2 describe-vpcs --region "$AWS_REGION" --query 'Vpcs[?State==`available`].[VpcId,CidrBlock]' --output text 2>/dev/null || echo "")
172-
173-
if [ -z "$EXISTING_VPCS" ]; then
174-
echo "No existing VPCs found. Creating new VPCs..."
175-
CREATE_VPCS=true
176-
else
177-
echo "Found existing VPCs:"
178-
echo "$EXISTING_VPCS"
179-
echo ""
180-
echo "Using existing VPCs..."
181-
CREATE_VPCS=false
182-
# Get the first two available VPCs
183-
VPC1_INFO=$(echo "$EXISTING_VPCS" | head -n 1)
184-
VPC2_INFO=$(echo "$EXISTING_VPCS" | head -n 2 | tail -n 1)
185-
186-
if [ -z "$VPC2_INFO" ]; then
187-
echo "Only one VPC found. Creating a second VPC..."
188-
VPC1_ID=$(echo "$VPC1_INFO" | awk '{print $1}')
189-
VPC1_CIDR=$(echo "$VPC1_INFO" | awk '{print $2}')
190-
191-
# Sanitize extracted values
192-
VPC1_ID=$(sanitize_var "$VPC1_ID") || check_error 1 "Invalid VPC1_ID format"
193-
VPC1_CIDR=$(sanitize_var "$VPC1_CIDR") || check_error 1 "Invalid VPC1_CIDR format"
194-
195-
validate_cidr "$VPC1_CIDR" || check_error 1 "Invalid VPC1 CIDR"
196-
CREATE_VPC2_ONLY=true
197-
else
198-
VPC1_ID=$(echo "$VPC1_INFO" | awk '{print $1}')
199-
VPC1_CIDR=$(echo "$VPC1_INFO" | awk '{print $2}')
200-
VPC2_ID=$(echo "$VPC2_INFO" | awk '{print $1}')
201-
VPC2_CIDR=$(echo "$VPC2_INFO" | awk '{print $2}')
202-
203-
# Sanitize extracted values
204-
VPC1_ID=$(sanitize_var "$VPC1_ID") || check_error 1 "Invalid VPC1_ID format"
205-
VPC1_CIDR=$(sanitize_var "$VPC1_CIDR") || check_error 1 "Invalid VPC1_CIDR format"
206-
VPC2_ID=$(sanitize_var "$VPC2_ID") || check_error 1 "Invalid VPC2_ID format"
207-
VPC2_CIDR=$(sanitize_var "$VPC2_CIDR") || check_error 1 "Invalid VPC2_CIDR format"
208-
209-
validate_cidr "$VPC1_CIDR" || check_error 1 "Invalid VPC1 CIDR"
210-
validate_cidr "$VPC2_CIDR" || check_error 1 "Invalid VPC2 CIDR"
211-
CREATE_VPC2_ONLY=false
169+
# Check VPC quota — need room for up to 2 new VPCs
170+
VPC_COUNT=$(aws ec2 describe-vpcs --region "$AWS_REGION" --query 'length(Vpcs)' --output text 2>/dev/null || echo 99)
171+
VPC_LIMIT=5
172+
VPCS_NEEDED=2
173+
174+
# Check if prereq stack provides a VPC we can use as VPC1
175+
PREREQ_VPC_ID=""
176+
PREREQ_STACK=$(aws cloudformation describe-stacks --region "$AWS_REGION" --stack-name tutorial-prereqs-vpc-public --query 'Stacks[0].StackStatus' --output text 2>/dev/null || echo "")
177+
if [[ "$PREREQ_STACK" == "CREATE_COMPLETE" || "$PREREQ_STACK" == "UPDATE_COMPLETE" ]]; then
178+
PREREQ_VPC_ID=$(aws cloudformation describe-stacks --region "$AWS_REGION" --stack-name tutorial-prereqs-vpc-public --query 'Stacks[0].Outputs[?OutputKey==`VpcId`].OutputValue' --output text 2>/dev/null || echo "")
179+
if [ -n "$PREREQ_VPC_ID" ]; then
180+
echo "Found prereq stack VPC: $PREREQ_VPC_ID (10.0.0.0/16)"
181+
VPCS_NEEDED=1
212182
fi
213183
fi
214184

215-
# Create VPCs if needed
216-
if [ "$CREATE_VPCS" = true ]; then
185+
AVAILABLE=$((VPC_LIMIT - VPC_COUNT))
186+
if [ "$AVAILABLE" -lt "$VPCS_NEEDED" ]; then
187+
echo "ERROR: Need $VPCS_NEEDED VPC slots but only $AVAILABLE available ($VPC_COUNT/$VPC_LIMIT used in $AWS_REGION)."
188+
echo "Free up VPCs or run in a different region: AWS_REGION=<region> bash $0"
189+
exit 1
190+
fi
191+
192+
# Set up VPCs
193+
if [ -n "$PREREQ_VPC_ID" ]; then
194+
# Use prereq VPC as VPC1, create VPC2
195+
VPC1_ID="$PREREQ_VPC_ID"
196+
VPC1_CIDR="10.0.0.0/16"
197+
echo "Using prereq stack VPC as VPC1: $VPC1_ID ($VPC1_CIDR)"
198+
199+
echo "Creating VPC2..."
200+
VPC2_ID=$(log_cmd "aws ec2 create-vpc --region '$AWS_REGION' --cidr-block 10.2.0.0/16 --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=VPC2-Peering-Demo}]' --query 'Vpc.VpcId' --output text")
201+
check_error $? "Failed to create VPC2"
202+
VPC2_ID=$(sanitize_var "$VPC2_ID") || check_error 1 "Invalid VPC2_ID returned"
203+
VPC2_CIDR="10.2.0.0/16"
204+
CREATED_RESOURCES+=("VPC2: $VPC2_ID")
205+
CLEANUP_COMMANDS+=("aws ec2 delete-vpc --region '$AWS_REGION' --vpc-id '$VPC2_ID'")
206+
echo "VPC2 created with ID: $VPC2_ID"
207+
208+
echo "Waiting for VPC2 to be available..."
209+
log_cmd "aws ec2 wait vpc-available --region '$AWS_REGION' --vpc-ids '$VPC2_ID'"
210+
check_error $? "Timeout waiting for VPC2 to become available"
211+
else
212+
# Create both VPCs
217213
echo "Creating VPC1..."
218214
VPC1_ID=$(log_cmd "aws ec2 create-vpc --region '$AWS_REGION' --cidr-block 10.1.0.0/16 --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=VPC1-Peering-Demo}]' --query 'Vpc.VpcId' --output text")
219215
check_error $? "Failed to create VPC1"
@@ -222,7 +218,7 @@ if [ "$CREATE_VPCS" = true ]; then
222218
CREATED_RESOURCES+=("VPC1: $VPC1_ID")
223219
CLEANUP_COMMANDS+=("aws ec2 delete-vpc --region '$AWS_REGION' --vpc-id '$VPC1_ID'")
224220
echo "VPC1 created with ID: $VPC1_ID"
225-
221+
226222
echo "Creating VPC2..."
227223
VPC2_ID=$(log_cmd "aws ec2 create-vpc --region '$AWS_REGION' --cidr-block 10.2.0.0/16 --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=VPC2-Peering-Demo}]' --query 'Vpc.VpcId' --output text")
228224
check_error $? "Failed to create VPC2"
@@ -231,26 +227,10 @@ if [ "$CREATE_VPCS" = true ]; then
231227
CREATED_RESOURCES+=("VPC2: $VPC2_ID")
232228
CLEANUP_COMMANDS+=("aws ec2 delete-vpc --region '$AWS_REGION' --vpc-id '$VPC2_ID'")
233229
echo "VPC2 created with ID: $VPC2_ID"
234-
235-
# Wait for VPCs to be available
230+
236231
echo "Waiting for VPCs to be available..."
237232
log_cmd "aws ec2 wait vpc-available --region '$AWS_REGION' --vpc-ids '$VPC1_ID' '$VPC2_ID'"
238233
check_error $? "Timeout waiting for VPCs to become available"
239-
240-
elif [ "$CREATE_VPC2_ONLY" = true ]; then
241-
echo "Creating VPC2..."
242-
VPC2_ID=$(log_cmd "aws ec2 create-vpc --region '$AWS_REGION' --cidr-block 10.2.0.0/16 --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=VPC2-Peering-Demo}]' --query 'Vpc.VpcId' --output text")
243-
check_error $? "Failed to create VPC2"
244-
VPC2_ID=$(sanitize_var "$VPC2_ID") || check_error 1 "Invalid VPC2_ID returned"
245-
VPC2_CIDR="10.2.0.0/16"
246-
CREATED_RESOURCES+=("VPC2: $VPC2_ID")
247-
CLEANUP_COMMANDS+=("aws ec2 delete-vpc --region '$AWS_REGION' --vpc-id '$VPC2_ID'")
248-
echo "VPC2 created with ID: $VPC2_ID"
249-
250-
# Wait for VPC2 to be available
251-
echo "Waiting for VPC2 to be available..."
252-
log_cmd "aws ec2 wait vpc-available --region '$AWS_REGION' --vpc-ids '$VPC2_ID'"
253-
check_error $? "Timeout waiting for VPC2 to become available"
254234
fi
255235

256236
echo "Using the following VPCs:"
@@ -263,8 +243,9 @@ log_cmd "aws ec2 describe-vpcs --region '$AWS_REGION' --vpc-ids '$VPC1_ID' '$VPC
263243
check_error $? "Failed to verify VPCs"
264244

265245
# Determine subnet CIDR blocks based on VPC CIDR blocks
266-
VPC1_SUBNET_CIDR=$(echo "$VPC1_CIDR" | sed 's/0\.0\/16/1.0\/24/')
267-
VPC2_SUBNET_CIDR=$(echo "$VPC2_CIDR" | sed 's/0\.0\/16/1.0\/24/')
246+
# Use .100.0/24 to avoid overlap with prereq stack subnets (.1-.4)
247+
VPC1_SUBNET_CIDR=$(echo "$VPC1_CIDR" | sed 's/0\.0\/16/100.0\/24/')
248+
VPC2_SUBNET_CIDR=$(echo "$VPC2_CIDR" | sed 's/0\.0\/16/100.0\/24/')
268249

269250
# Sanitize subnet CIDR blocks
270251
VPC1_SUBNET_CIDR=$(sanitize_var "$VPC1_SUBNET_CIDR") || check_error 1 "Invalid VPC1_SUBNET_CIDR format"
@@ -382,7 +363,7 @@ echo "Route Table 2 ID: $RTB2_ID"
382363
echo "Route Table 2 Association ID: $RTB2_ASSOC_ID"
383364
echo ""
384365
echo "Created resources:"
385-
for resource in "${CREATED_RESOURCES[@]}"; do
366+
for resource in "${CREATED_RESOURCES[@]+"${CREATED_RESOURCES[@]}"}"; do
386367
echo "- $resource"
387368
done
388369
echo "=============================================="

tuts/046-aws-systems-manager-gs/aws-systems-manager-gs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ echo ""
528528
echo "==========================================="
529529
echo "CREATED RESOURCES"
530530
echo "==========================================="
531-
for resource in "${CREATED_RESOURCES[@]}"; do
531+
for resource in "${CREATED_RESOURCES[@]+"${CREATED_RESOURCES[@]}"}"; do
532532
echo "$resource"
533533
done
534534

tuts/049-aws-end-user-messaging-gs/aws-end-user-messaging-gs.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ cleanup() {
4343

4444
# Optionally delete AWS resources
4545
if [ "${DELETE_AWS_RESOURCES:-false}" = "true" ]; then
46-
for resource in "${AWS_RESOURCES[@]}"; do
46+
for resource in "${AWS_RESOURCES[@]+"${AWS_RESOURCES[@]}"}"; do
4747
echo "Deleting AWS resource: $resource"
4848
aws pinpoint delete-app --application-id "$resource" 2>/dev/null || \
4949
echo "Warning: Failed to delete application $resource"
@@ -406,7 +406,7 @@ echo "==========================================="
406406
echo "RESOURCES CREATED"
407407
echo "==========================================="
408408
echo "AWS Resources:"
409-
for resource in "${AWS_RESOURCES[@]}"; do
409+
for resource in "${AWS_RESOURCES[@]+"${AWS_RESOURCES[@]}"}"; do
410410
echo "- Application: $resource"
411411
done
412412

tuts/055-amazon-vpc-lattice-gs/amazon-vpc-lattice-getting-started.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ log_command "aws vpc-lattice list-service-network-vpc-associations --service-net
290290
# Step 10: Cleanup - Auto-confirm
291291
echo -e "\n=== Step 10: Resource Cleanup ===" | tee -a "$LOG_FILE"
292292
echo "Resources created in this tutorial:" | tee -a "$LOG_FILE"
293-
for resource in "${CREATED_RESOURCES[@]}"; do
293+
for resource in "${CREATED_RESOURCES[@]+"${CREATED_RESOURCES[@]}"}"; do
294294
echo "- $resource" | tee -a "$LOG_FILE"
295295
done
296296

tuts/062-aws-support-gs/aws-support-gs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ else
277277
if [[ ${#CREATED_RESOURCES[@]} -eq 0 ]]; then
278278
echo "No resources were created."
279279
else
280-
for resource in "${CREATED_RESOURCES[@]}"; do
280+
for resource in "${CREATED_RESOURCES[@]+"${CREATED_RESOURCES[@]}"}"; do
281281
echo "- $resource"
282282
done
283283
fi

tuts/070-amazon-dynamodb-gs/amazon-dynamodb-gs.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ cleanup() {
8989
echo "CLEANUP"
9090
echo "==========================================="
9191
echo "Resources to clean up:"
92-
for resource in "${RESOURCES[@]}"; do
92+
for resource in "${RESOURCES[@]+"${RESOURCES[@]}"}"; do
9393
echo "- $resource"
9494
done
9595
echo ""
9696

9797
if [[ ${#RESOURCES[@]} -gt 0 ]]; then
9898
echo "Proceeding with cleanup of all created resources..."
9999

100-
for resource in "${RESOURCES[@]}"; do
100+
for resource in "${RESOURCES[@]+"${RESOURCES[@]}"}"; do
101101
if [[ "$resource" == Table:* ]]; then
102102
local table_name="${resource#Table:}"
103103
echo "Deleting table: $table_name"

tuts/077-aws-account-management-gs/aws-account-management-gs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ sleep "$API_CALL_DELAY"
267267
# Security: Define valid contact types
268268
declare -a CONTACT_TYPES=("BILLING" "OPERATIONS" "SECURITY")
269269

270-
for contact_type in "${CONTACT_TYPES[@]}"; do
270+
for contact_type in "${CONTACT_TYPES[@]+"${CONTACT_TYPES[@]}"}"; do
271271
{
272272
echo ""
273273
echo "Attempting to check $contact_type contact information..."

tuts/079-aws-iot-device-defender-gs/aws-iot-device-defender-gs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ echo "==================================================="
667667
echo "AWS IoT Device Defender setup completed successfully!"
668668
echo "==================================================="
669669
echo "The following resources were created:"
670-
for resource in "${CREATED_RESOURCES[@]}"; do
670+
for resource in "${CREATED_RESOURCES[@]+"${CREATED_RESOURCES[@]}"}"; do
671671
echo "- $resource"
672672
done
673673
echo ""

0 commit comments

Comments
 (0)