Skip to content

Commit a95c3c7

Browse files
committed
Add mixed tutorials (batch 24)
1 parent 49f07d9 commit a95c3c7

5 files changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/ad.log") 2>&1
3+
REGION=${AWS_DEFAULT_REGION:-$(aws configure get region 2>/dev/null)}; [ -z "$REGION" ] && echo "ERROR: No region" && exit 1; export AWS_DEFAULT_REGION="$REGION"; echo "Region: $REGION"
4+
RANDOM_ID=$(openssl rand -hex 4); NS="Tutorial/AD-${RANDOM_ID}"; ALARM="tut-ad-alarm-${RANDOM_ID}"
5+
cleanup() { echo ""; echo "Cleaning up..."; aws cloudwatch delete-alarms --alarm-names "$ALARM" 2>/dev/null && echo " Deleted alarm"; aws cloudwatch delete-anomaly-detector --namespace "$NS" --metric-name Latency --stat Average 2>/dev/null && echo " Deleted detector"; rm -rf "$WORK_DIR"; echo "Done."; }
6+
echo "Step 1: Publishing baseline metrics"
7+
for i in $(seq 1 20); do aws cloudwatch put-metric-data --namespace "$NS" --metric-name Latency --value $((50 + RANDOM % 20)) --unit Milliseconds; done
8+
echo " Published 20 data points"
9+
echo "Step 2: Creating anomaly detector"
10+
aws cloudwatch put-anomaly-detector --namespace "$NS" --metric-name Latency --stat Average 2>/dev/null && echo " Detector created" || echo " Detector creation pending"
11+
echo "Step 3: Creating anomaly detection alarm"
12+
aws cloudwatch put-metric-alarm --alarm-name "$ALARM" --namespace "$NS" --metric-name Latency --comparison-operator LessThanLowerOrGreaterThanUpperThreshold --evaluation-periods 1 --threshold-metric-id ad1 --metrics '[{"Id":"m1","MetricStat":{"Metric":{"Namespace":"'"$NS"'","MetricName":"Latency"},"Period":60,"Stat":"Average"},"ReturnData":true},{"Id":"ad1","Expression":"ANOMALY_DETECTION_BAND(m1,2)","ReturnData":true}]' 2>/dev/null && echo " Alarm created" || echo " Alarm creation requires more data"
13+
echo "Step 4: Describing alarm"
14+
aws cloudwatch describe-alarms --alarm-names "$ALARM" --query 'MetricAlarms[0].{Name:AlarmName,State:StateValue}' --output table 2>/dev/null || echo " No alarm"
15+
echo ""; echo "Tutorial complete."
16+
echo "Do you want to clean up? (y/n): "; read -r CHOICE; [[ "$CHOICE" =~ ^[Yy]$ ]] && cleanup
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/tut.log") 2>&1
3+
REGION=${AWS_DEFAULT_REGION:-$(aws configure get region 2>/dev/null)}; [ -z "$REGION" ] && echo "ERROR: No region" && exit 1; export AWS_DEFAULT_REGION="$REGION"; echo "Region: $REGION"
4+
echo "Step 1: Listing existing rules"
5+
aws cloudwatch describe-insight-rules --query 'InsightRules[:5].{Name:Name,State:State}' --output table 2>/dev/null || echo " No insight rules"
6+
echo "Step 2: Listing log groups for analysis"
7+
aws logs describe-log-groups --limit 5 --query 'logGroups[].{Name:logGroupName,Stored:storedBytes}' --output table
8+
echo "Step 3: Getting metric widget image (base64)"
9+
aws cloudwatch get-metric-widget-image --metric-widget '{"metrics":[["AWS/EC2","CPUUtilization"]],"period":300,"stat":"Average","region":"us-east-1","title":"EC2 CPU"}' --query 'MetricWidgetImage' --output text | head -c 50
10+
echo "..."
11+
echo ""; echo "Tutorial complete. No resources created — read-only."
12+
rm -rf "$WORK_DIR"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/tut.log") 2>&1
3+
REGION=${AWS_DEFAULT_REGION:-$(aws configure get region 2>/dev/null)}; [ -z "$REGION" ] && echo "ERROR: No region" && exit 1; export AWS_DEFAULT_REGION="$REGION"; echo "Region: $REGION"
4+
RANDOM_ID=$(openssl rand -hex 4); NS="Tutorial/Comp-${RANDOM_ID}"; A1="tut-cpu-${RANDOM_ID}"; A2="tut-mem-${RANDOM_ID}"; COMP="tut-composite-${RANDOM_ID}"
5+
cleanup() { echo "Cleaning up..."; aws cloudwatch delete-alarms --alarm-names "$COMP" "$A1" "$A2" 2>/dev/null && echo " Deleted alarms"; rm -rf "$WORK_DIR"; echo "Done."; }
6+
echo "Step 1: Publishing metrics"
7+
for i in $(seq 1 5); do aws cloudwatch put-metric-data --namespace "$NS" --metric-data "[{\"MetricName\":\"CPU\",\"Value\":$((RANDOM%100)),\"Unit\":\"Percent\"},{\"MetricName\":\"Memory\",\"Value\":$((RANDOM%100)),\"Unit\":\"Percent\"}]"; done
8+
echo "Step 2: Creating metric alarms"
9+
aws cloudwatch put-metric-alarm --alarm-name "$A1" --namespace "$NS" --metric-name CPU --statistic Average --period 60 --threshold 80 --comparison-operator GreaterThanThreshold --evaluation-periods 1
10+
aws cloudwatch put-metric-alarm --alarm-name "$A2" --namespace "$NS" --metric-name Memory --statistic Average --period 60 --threshold 80 --comparison-operator GreaterThanThreshold --evaluation-periods 1
11+
echo "Step 3: Creating composite alarm"
12+
aws cloudwatch put-composite-alarm --alarm-name "$COMP" --alarm-rule "ALARM(\"$A1\") AND ALARM(\"$A2\")"
13+
echo " Composite alarm triggers when BOTH CPU and Memory are high"
14+
echo "Step 4: Describing composite alarm"
15+
aws cloudwatch describe-alarms --alarm-names "$COMP" --alarm-types CompositeAlarm --query 'CompositeAlarms[0].{Name:AlarmName,Rule:AlarmRule,State:StateValue}' --output table
16+
echo ""; echo "Tutorial complete."
17+
echo "Do you want to clean up? (y/n): "; read -r C; [[ "$C" =~ ^[Yy]$ ]] && cleanup
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/tut.log") 2>&1
3+
REGION=${AWS_DEFAULT_REGION:-$(aws configure get region 2>/dev/null)}; [ -z "$REGION" ] && echo "ERROR: No region" && exit 1; export AWS_DEFAULT_REGION="$REGION"; echo "Region: $REGION"
4+
echo "Step 1: Listing tables"; aws dynamodb list-tables --query 'TableNames' --output table
5+
echo "Step 2: Table details"
6+
T=$(aws dynamodb list-tables --query 'TableNames[0]' --output text 2>/dev/null)
7+
[ -n "$T" ] && [ "$T" != "None" ] && aws dynamodb describe-table --table-name "$T" --query 'Table.{Name:TableName,Status:TableStatus,Items:ItemCount,Size:TableSizeBytes,Billing:BillingModeSummary.BillingMode}' --output table || echo " No tables"
8+
echo ""; echo "Tutorial complete. Read-only."; rm -rf "$WORK_DIR"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/tut.log") 2>&1
3+
REGION=${AWS_DEFAULT_REGION:-$(aws configure get region 2>/dev/null)}; [ -z "$REGION" ] && echo "ERROR: No region" && exit 1; export AWS_DEFAULT_REGION="$REGION"; echo "Region: $REGION"
4+
echo "Step 1: Listing IAM users"; aws iam list-users --query 'Users[].{Name:UserName,Created:CreateDate,PasswordLastUsed:PasswordLastUsed}' --output table
5+
echo "Step 2: User count"; echo " Total: $(aws iam list-users --query 'Users | length(@)' --output text) users"
6+
echo ""; echo "Tutorial complete. Read-only."; rm -rf "$WORK_DIR"

0 commit comments

Comments
 (0)