Skip to content

Commit 994217d

Browse files
committed
make the scripts look a little shiny
1 parent 10ce2c9 commit 994217d

File tree

2 files changed

+188
-80
lines changed

2 files changed

+188
-80
lines changed

solutions/dynamodb-outage.sh

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,78 @@
1-
awslocal sns create-topic --name ProductEventsTopic
1+
#!/bin/bash
22

3-
awslocal sqs create-queue --queue-name ProductEventsQueue
3+
set -e
4+
set -o pipefail
45

5-
awslocal sqs get-queue-attributes --queue-url http://localhost:4566/000000000000/ProductEventsQueue --attribute-names QueueArn
6+
AWS_ENDPOINT_URL=${AWS_ENDPOINT_URL:-"http://localhost:4566"}
67

8+
# Colors for logging
9+
GREEN='\033[0;32m'
10+
BLUE='\033[0;34m'
11+
RED='\033[0;31m'
12+
NC='\033[0m'
13+
14+
# Logging functions
15+
log() {
16+
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" >&2
17+
}
18+
19+
error_log() {
20+
echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ERROR:${NC} $1" >&2
21+
}
22+
23+
trap 'error_log "An error occurred. Exiting..."; exit 1' ERR
24+
25+
# Setup SNS Topic
26+
log "Creating SNS topic 'ProductEventsTopic'..."
27+
SNS_TOPIC_ARN=$(awslocal sns create-topic --name ProductEventsTopic --output json | jq -r '.TopicArn')
28+
log "SNS topic created. ARN: $SNS_TOPIC_ARN"
29+
30+
# Setup SQS Queue
31+
log "Creating SQS queue 'ProductEventsQueue'..."
32+
QUEUE_URL=$(awslocal sqs create-queue --queue-name ProductEventsQueue --output json | jq -r '.QueueUrl')
33+
QUEUE_ARN=$(awslocal sqs get-queue-attributes \
34+
--queue-url $QUEUE_URL \
35+
--attribute-names QueueArn \
36+
--query 'Attributes.QueueArn' --output text)
37+
log "SQS queue created. ARN: $QUEUE_ARN"
38+
39+
# Subscribe SQS Queue to SNS Topic
40+
log "Subscribing SQS queue to SNS topic..."
741
awslocal sns subscribe \
8-
--topic-arn arn:aws:sns:us-east-1:000000000000:ProductEventsTopic \
42+
--topic-arn $SNS_TOPIC_ARN \
943
--protocol sqs \
10-
--notification-endpoint arn:aws:sqs:us-east-1:000000000000:ProductEventsQueue
44+
--notification-endpoint $QUEUE_ARN >/dev/null
45+
log "SQS queue subscribed to SNS topic."
1146

47+
# Create Lambda Function
48+
log "Creating Lambda function 'process-product-events'..."
1249
awslocal lambda create-function \
1350
--function-name process-product-events \
1451
--runtime java17 \
1552
--handler lambda.DynamoDBWriterLambda::handleRequest \
1653
--memory-size 1024 \
1754
--timeout 20 \
1855
--zip-file fileb://lambda-functions/target/product-lambda.jar \
19-
--role arn:aws:iam::000000000000:role/productRole
56+
--role arn:aws:iam::000000000000:role/productRole >/dev/null
57+
log "Lambda function created."
2058

59+
# Create Event Source Mapping from SQS to Lambda
60+
log "Creating event source mapping from SQS to Lambda..."
2161
awslocal lambda create-event-source-mapping \
2262
--function-name process-product-events \
2363
--batch-size 10 \
24-
--event-source-arn arn:aws:sqs:us-east-1:000000000000:ProductEventsQueue
64+
--event-source-arn $QUEUE_ARN >/dev/null
65+
log "Event source mapping created."
2566

67+
# Set Queue Attributes
68+
log "Setting SQS queue attributes..."
2669
awslocal sqs set-queue-attributes \
27-
--queue-url http://localhost:4566/000000000000/ProductEventsQueue \
28-
--attributes VisibilityTimeout=10
70+
--queue-url $QUEUE_URL \
71+
--attributes VisibilityTimeout=10 >/dev/null
72+
log "SQS queue attributes set."
73+
74+
# Final Output
75+
echo
76+
echo -e "${BLUE}Setup completed successfully.${NC}"
77+
echo -e "${BLUE}SNS Topic ARN:${NC} $SNS_TOPIC_ARN"
78+
echo -e "${BLUE}SQS Queue ARN:${NC} $QUEUE_ARN"

solutions/route53-failover.sh

Lines changed: 129 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,165 @@
1-
echo "--- Step 1: Define Hosted Zone ---"
1+
#!/bin/bash
2+
3+
set -e
4+
set -o pipefail
5+
6+
# Colors for logging
7+
GREEN='\033[0;32m'
8+
BLUE='\033[0;34m'
9+
RED='\033[0;31m'
10+
NC='\033[0m'
11+
12+
# Logging functions
13+
log() {
14+
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" >&2
15+
}
16+
17+
error_log() {
18+
echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ERROR:${NC} $1" >&2
19+
}
20+
21+
trap 'error_log "An error occurred. Exiting..."; exit 1' ERR
22+
23+
# Step 1: Define Hosted Zone
24+
log "Defining hosted zone..."
225
HOSTED_ZONE_NAME="hello-localstack.com"
326
RAW_HOSTED_ZONE_ID=$(awslocal route53 create-hosted-zone \
427
--name "$HOSTED_ZONE_NAME" \
528
--caller-reference "zone-$(date +%s)" | jq -r .HostedZone.Id)
629
CLEANED_HOSTED_ZONE_ID="${RAW_HOSTED_ZONE_ID#/hostedzone/}"
7-
echo "Hosted Zone Name: $HOSTED_ZONE_NAME"
8-
echo "Raw Hosted Zone ID: $RAW_HOSTED_ZONE_ID"
9-
echo
1030

11-
export HOSTED_ZONE_NAME
12-
export RAW_HOSTED_ZONE_ID
31+
log "Hosted Zone Name: $HOSTED_ZONE_NAME"
32+
log "Raw Hosted Zone ID: $RAW_HOSTED_ZONE_ID"
33+
export HOSTED_ZONE_NAME RAW_HOSTED_ZONE_ID
1334

14-
echo "--- Step 2: Define API Gateway and Health Check Parameters ---"
35+
# Step 2: Define API Gateway and Health Check Parameters
36+
log "Defining API Gateway and health check parameters..."
1537
PRIMARY_API_ID="12345"
1638
SECONDARY_API_ID="67890"
17-
PRIMARY_API_REGION="us-east-1" # Define the region of your primary API Gateway
18-
39+
PRIMARY_API_REGION="us-east-1"
1940
HEALTH_CHECK_RESOURCE_PATH="/dev/healthcheck"
2041
PRIMARY_API_GATEWAY_FQDN="${PRIMARY_API_ID}.execute-api.localhost.localstack.cloud"
2142
HEALTH_CHECK_PORT=4566
2243

23-
echo "Primary API ID: $PRIMARY_API_ID in region $PRIMARY_API_REGION"
24-
echo "Primary API FQDN for Health Check: $PRIMARY_API_GATEWAY_FQDN"
25-
echo "Health Check Port: $HEALTH_CHECK_PORT"
26-
echo "Health Check Path: $HEALTH_CHECK_RESOURCE_PATH"
27-
echo
44+
log "Primary API ID: $PRIMARY_API_ID"
45+
log "Primary API FQDN: $PRIMARY_API_GATEWAY_FQDN"
46+
log "Health Check Port: $HEALTH_CHECK_PORT"
47+
log "Health Check Path: $HEALTH_CHECK_RESOURCE_PATH"
2848

29-
echo "--- Step 3: Create Health Check for the Primary API Gateway ---"
30-
# Health check can be created in any region, let's use us-west-1 as an example for the HC resource
49+
# Step 3: Create Health Check for the Primary API Gateway
50+
log "Creating Route 53 health check..."
3151
HEALTH_CHECK_RESOURCE_REGION="us-west-1"
3252
HEALTH_CHECK_ID=$(awslocal route53 create-health-check \
3353
--caller-reference "hc-app-${PRIMARY_API_ID}-$(date +%s)" \
3454
--region "$HEALTH_CHECK_RESOURCE_REGION" \
35-
--health-check-config "{
36-
\"FullyQualifiedDomainName\": \"${PRIMARY_API_GATEWAY_FQDN}\",
37-
\"Port\": ${HEALTH_CHECK_PORT},
38-
\"ResourcePath\": \"${HEALTH_CHECK_RESOURCE_PATH}\",
39-
\"Type\": \"HTTP\",
40-
\"RequestInterval\": 10,
41-
\"FailureThreshold\": 2
42-
}" | jq -r .HealthCheck.Id)
43-
echo "Health Check ID created ($HEALTH_CHECK_ID) in region $HEALTH_CHECK_RESOURCE_REGION"
55+
--health-check-config "{\"FullyQualifiedDomainName\": \"${PRIMARY_API_GATEWAY_FQDN}\", \"Port\": ${HEALTH_CHECK_PORT}, \"ResourcePath\": \"${HEALTH_CHECK_RESOURCE_PATH}\", \"Type\": \"HTTP\", \"RequestInterval\": 10, \"FailureThreshold\": 2}" | jq -r .HealthCheck.Id)
56+
57+
log "Health check created with ID: $HEALTH_CHECK_ID in region $HEALTH_CHECK_RESOURCE_REGION"
4458
export HEALTH_CHECK_ID
45-
echo
4659
sleep 5
4760

48-
echo "--- Step 4: Verify Initial Health of Primary API Gateway (No Chaos) ---"
49-
echo "Attempting to curl the primary health check endpoint directly (should be 200 OK):"
50-
curl --connect-timeout 5 -v "http://${PRIMARY_API_GATEWAY_FQDN}:${HEALTH_CHECK_PORT}${HEALTH_CHECK_RESOURCE_PATH}"
51-
echo
52-
echo
53-
echo "Fetching initial health check status from Route 53 (may take a few checks to show Success):"
54-
sleep 25 # (RequestInterval * FailureThreshold + buffer)
55-
# Query the health check status from the region it was created in
56-
awslocal route53 get-health-check-status --health-check-id "$HEALTH_CHECK_ID" --region "$HEALTH_CHECK_RESOURCE_REGION"
57-
echo
58-
echo
61+
# Step 4: Verify Initial Health
62+
log "Verifying primary health check endpoint (expect HTTP 200)..."
63+
curl --connect-timeout 5 -v "http://${PRIMARY_API_GATEWAY_FQDN}:${HEALTH_CHECK_PORT}${HEALTH_CHECK_RESOURCE_PATH}" || true
5964

60-
echo "--- Step 5: Create CNAME Records for Regional API Endpoints ---"
65+
log "Fetching health check status from Route 53 (may take a few seconds)..."
66+
sleep 25
67+
awslocal route53 get-health-check-status \
68+
--health-check-id "$HEALTH_CHECK_ID" \
69+
--region "$HEALTH_CHECK_RESOURCE_REGION" >/dev/null
70+
71+
# Step 5: Create CNAME Records
72+
log "Creating CNAME records for regional endpoints..."
6173
PRIMARY_REGIONAL_DNS_NAME="${PRIMARY_API_ID}.${HOSTED_ZONE_NAME}"
6274
SECONDARY_REGIONAL_DNS_NAME="${SECONDARY_API_ID}.${HOSTED_ZONE_NAME}"
6375
PRIMARY_API_TARGET_FQDN="${PRIMARY_API_ID}.execute-api.localhost.localstack.cloud"
6476
SECONDARY_API_TARGET_FQDN="${SECONDARY_API_ID}.execute-api.localhost.localstack.cloud"
6577

66-
CHANGE_BATCH_REGIONAL_CNAMES_JSON=$(printf '{
67-
"Comment": "Creating CNAMEs for regional API endpoints",
68-
"Changes": [
69-
{"Action": "UPSERT", "ResourceRecordSet": {"Name": "%s", "Type": "CNAME", "TTL": 60, "ResourceRecords": [{"Value": "%s"}]}},
70-
{"Action": "UPSERT", "ResourceRecordSet": {"Name": "%s", "Type": "CNAME", "TTL": 60, "ResourceRecords": [{"Value": "%s"}]}}
71-
]
72-
}' "$PRIMARY_REGIONAL_DNS_NAME" "$PRIMARY_API_TARGET_FQDN" "$SECONDARY_REGIONAL_DNS_NAME" "$SECONDARY_API_TARGET_FQDN")
78+
CHANGE_BATCH_REGIONAL_CNAMES_JSON=$(cat <<EOF
79+
{
80+
"Comment": "Creating CNAMEs for regional API endpoints",
81+
"Changes": [
82+
{
83+
"Action": "UPSERT",
84+
"ResourceRecordSet": {
85+
"Name": "$PRIMARY_REGIONAL_DNS_NAME",
86+
"Type": "CNAME",
87+
"TTL": 60,
88+
"ResourceRecords": [{ "Value": "$PRIMARY_API_TARGET_FQDN" }]
89+
}
90+
},
91+
{
92+
"Action": "UPSERT",
93+
"ResourceRecordSet": {
94+
"Name": "$SECONDARY_REGIONAL_DNS_NAME",
95+
"Type": "CNAME",
96+
"TTL": 60,
97+
"ResourceRecords": [{ "Value": "$SECONDARY_API_TARGET_FQDN" }]
98+
}
99+
}
100+
]
101+
}
102+
EOF
103+
)
73104

74-
echo "Creating/Updating CNAMEs for regional API Gateways..."
75-
awslocal route53 change-resource-record-sets --hosted-zone-id "$RAW_HOSTED_ZONE_ID" --change-batch "$CHANGE_BATCH_REGIONAL_CNAMES_JSON"
76-
echo
105+
awslocal route53 change-resource-record-sets \
106+
--hosted-zone-id "$RAW_HOSTED_ZONE_ID" \
107+
--change-batch "$CHANGE_BATCH_REGIONAL_CNAMES_JSON" >/dev/null
108+
log "CNAME records created."
77109

78-
echo "--- Step 6: Create Failover Alias Records ---"
110+
# Step 6: Create Failover Alias Records
111+
log "Creating failover alias records..."
79112
FAILOVER_RECORD_NAME="test.${HOSTED_ZONE_NAME}"
80113
PRIMARY_FAILOVER_SET_ID="primary-app-${PRIMARY_API_ID}"
81114
SECONDARY_FAILOVER_SET_ID="secondary-app-${SECONDARY_API_ID}"
82115

83-
CHANGE_BATCH_FAILOVER_ALIASES_JSON=$(printf '{
84-
"Comment": "Creating failover alias records for %s",
85-
"Changes": [
86-
{
87-
"Action": "UPSERT",
88-
"ResourceRecordSet": {
89-
"Name": "%s", "Type": "CNAME", "SetIdentifier": "%s", "Failover": "PRIMARY", "HealthCheckId": "%s",
90-
"AliasTarget": {"HostedZoneId": "%s", "DNSName": "%s", "EvaluateTargetHealth": true}
91-
}
92-
},
93-
{
94-
"Action": "UPSERT",
95-
"ResourceRecordSet": {
96-
"Name": "%s", "Type": "CNAME", "SetIdentifier": "%s", "Failover": "SECONDARY",
97-
"AliasTarget": {"HostedZoneId": "%s", "DNSName": "%s", "EvaluateTargetHealth": false}
98-
}
116+
CHANGE_BATCH_FAILOVER_ALIASES_JSON=$(cat <<EOF
117+
{
118+
"Comment": "Creating failover alias records for $FAILOVER_RECORD_NAME",
119+
"Changes": [
120+
{
121+
"Action": "UPSERT",
122+
"ResourceRecordSet": {
123+
"Name": "$FAILOVER_RECORD_NAME",
124+
"Type": "CNAME",
125+
"SetIdentifier": "$PRIMARY_FAILOVER_SET_ID",
126+
"Failover": "PRIMARY",
127+
"HealthCheckId": "$HEALTH_CHECK_ID",
128+
"AliasTarget": {
129+
"HostedZoneId": "$RAW_HOSTED_ZONE_ID",
130+
"DNSName": "$PRIMARY_REGIONAL_DNS_NAME",
131+
"EvaluateTargetHealth": true
99132
}
100-
]
101-
}' "$FAILOVER_RECORD_NAME" \
102-
"$FAILOVER_RECORD_NAME" "$PRIMARY_FAILOVER_SET_ID" "$HEALTH_CHECK_ID" "$RAW_HOSTED_ZONE_ID" "$PRIMARY_REGIONAL_DNS_NAME" \
103-
"$FAILOVER_RECORD_NAME" "$SECONDARY_FAILOVER_SET_ID" "$RAW_HOSTED_ZONE_ID" "$SECONDARY_REGIONAL_DNS_NAME")
133+
}
134+
},
135+
{
136+
"Action": "UPSERT",
137+
"ResourceRecordSet": {
138+
"Name": "$FAILOVER_RECORD_NAME",
139+
"Type": "CNAME",
140+
"SetIdentifier": "$SECONDARY_FAILOVER_SET_ID",
141+
"Failover": "SECONDARY",
142+
"AliasTarget": {
143+
"HostedZoneId": "$RAW_HOSTED_ZONE_ID",
144+
"DNSName": "$SECONDARY_REGIONAL_DNS_NAME",
145+
"EvaluateTargetHealth": false
146+
}
147+
}
148+
}
149+
]
150+
}
151+
EOF
152+
)
153+
154+
awslocal route53 change-resource-record-sets \
155+
--hosted-zone-id "$RAW_HOSTED_ZONE_ID" \
156+
--change-batch "$CHANGE_BATCH_FAILOVER_ALIASES_JSON" >/dev/null
157+
log "Failover alias records created."
104158

105-
echo "Creating/Updating failover alias records for $FAILOVER_RECORD_NAME..."
106-
awslocal route53 change-resource-record-sets --hosted-zone-id "$RAW_HOSTED_ZONE_ID" --change-batch "$CHANGE_BATCH_FAILOVER_ALIASES_JSON"
159+
# Final Output
107160
echo
161+
echo -e "${BLUE}Route 53 and failover setup completed successfully.${NC}"
162+
echo -e "${BLUE}Hosted Zone:${NC} $HOSTED_ZONE_NAME"
163+
echo -e "${BLUE}Primary API FQDN:${NC} $PRIMARY_API_GATEWAY_FQDN"
164+
echo -e "${BLUE}Health Check ID:${NC} $HEALTH_CHECK_ID"
165+
echo -e "${BLUE}Failover Domain:${NC} $FAILOVER_RECORD_NAME"

0 commit comments

Comments
 (0)