Skip to content

Commit 92488e1

Browse files
committed
Add database tutorials (batch 10)
1 parent 49f07d9 commit 92488e1

5 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/ddb-streams.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); TABLE="tut-stream-${RANDOM_ID}"
5+
handle_error() { echo "ERROR on line $1"; trap - ERR; cleanup; exit 1; }; trap 'handle_error $LINENO' ERR
6+
cleanup() { echo ""; echo "Cleaning up..."; aws dynamodb delete-table --table-name "$TABLE" > /dev/null 2>&1 && echo " Deleted table"; rm -rf "$WORK_DIR"; echo "Done."; }
7+
echo "Step 1: Creating table with streams enabled"
8+
aws dynamodb create-table --table-name "$TABLE" --key-schema AttributeName=pk,KeyType=HASH --attribute-definitions AttributeName=pk,AttributeType=S --billing-mode PAY_PER_REQUEST --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES --query 'TableDescription.{Table:TableName,Stream:LatestStreamArn}' --output table
9+
aws dynamodb wait table-exists --table-name "$TABLE"
10+
STREAM_ARN=$(aws dynamodb describe-table --table-name "$TABLE" --query 'Table.LatestStreamArn' --output text)
11+
echo "Step 2: Writing items to trigger stream events"
12+
aws dynamodb put-item --table-name "$TABLE" --item '{"pk":{"S":"user-1"},"name":{"S":"Alice"},"age":{"N":"30"}}' 2>/dev/null
13+
aws dynamodb put-item --table-name "$TABLE" --item '{"pk":{"S":"user-2"},"name":{"S":"Bob"},"age":{"N":"25"}}' 2>/dev/null
14+
aws dynamodb update-item --table-name "$TABLE" --key '{"pk":{"S":"user-1"}}' --update-expression "SET age = :a" --expression-attribute-values '{":a":{"N":"31"}}' 2>/dev/null
15+
aws dynamodb delete-item --table-name "$TABLE" --key '{"pk":{"S":"user-2"}}' 2>/dev/null
16+
echo " 4 operations: 2 puts, 1 update, 1 delete"
17+
echo "Step 3: Reading stream records"
18+
SHARD_ID=$(aws dynamodbstreams describe-stream --stream-arn "$STREAM_ARN" --query 'StreamDescription.Shards[0].ShardId' --output text)
19+
ITERATOR=$(aws dynamodbstreams get-shard-iterator --stream-arn "$STREAM_ARN" --shard-id "$SHARD_ID" --shard-iterator-type TRIM_HORIZON --query 'ShardIterator' --output text)
20+
aws dynamodbstreams get-records --shard-iterator "$ITERATOR" --query 'Records[].{Event:eventName,Keys:dynamodb.Keys}' --output table
21+
echo ""; echo "Tutorial complete."
22+
echo "Do you want to clean up? (y/n): "; read -r CHOICE; [[ "$CHOICE" =~ ^[Yy]$ ]] && cleanup
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/ddb-global.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); TABLE="tut-global-${RANDOM_ID}"
5+
handle_error() { echo "ERROR on line $1"; trap - ERR; cleanup; exit 1; }; trap 'handle_error $LINENO' ERR
6+
cleanup() { echo ""; echo "Cleaning up..."; aws dynamodb delete-table --table-name "$TABLE" > /dev/null 2>&1 && echo " Deleted table"; rm -rf "$WORK_DIR"; echo "Done."; }
7+
echo "Step 1: Creating table"
8+
aws dynamodb create-table --table-name "$TABLE" --key-schema AttributeName=pk,KeyType=HASH --attribute-definitions AttributeName=pk,AttributeType=S --billing-mode PAY_PER_REQUEST --query 'TableDescription.TableName' --output text > /dev/null
9+
aws dynamodb wait table-exists --table-name "$TABLE"
10+
echo "Step 2: Enabling point-in-time recovery"
11+
aws dynamodb update-continuous-backups --table-name "$TABLE" --point-in-time-recovery-specification PointInTimeRecoveryEnabled=true > /dev/null
12+
echo " PITR enabled"
13+
echo "Step 3: Describing continuous backups"
14+
aws dynamodb describe-continuous-backups --table-name "$TABLE" --query 'ContinuousBackupsDescription.{Status:ContinuousBackupsStatus,PITR:PointInTimeRecoveryDescription.PointInTimeRecoveryStatus}' --output table
15+
echo "Step 4: Writing and reading items"
16+
aws dynamodb put-item --table-name "$TABLE" --item '{"pk":{"S":"item-1"},"data":{"S":"Hello"}}' 2>/dev/null
17+
aws dynamodb get-item --table-name "$TABLE" --key '{"pk":{"S":"item-1"}}' --query 'Item.{pk:pk.S,data:data.S}' --output table
18+
echo "Step 5: Table details"
19+
aws dynamodb describe-table --table-name "$TABLE" --query 'Table.{Name:TableName,Status:TableStatus,Items:ItemCount,Billing:BillingModeSummary.BillingMode}' --output table
20+
echo ""; echo "Tutorial complete."
21+
echo "Do you want to clean up? (y/n): "; read -r CHOICE; [[ "$CHOICE" =~ ^[Yy]$ ]] && cleanup
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/rds-snap.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 RDS instances"
5+
aws rds describe-db-instances --query 'DBInstances[:5].{Id:DBInstanceIdentifier,Engine:Engine,Status:DBInstanceStatus,Class:DBInstanceClass}' --output table 2>/dev/null || echo " No RDS instances"
6+
echo "Step 2: Listing automated snapshots"
7+
aws rds describe-db-snapshots --snapshot-type automated --query 'DBSnapshots[:5].{Id:DBSnapshotIdentifier,Instance:DBInstanceIdentifier,Status:Status,Engine:Engine}' --output table 2>/dev/null || echo " No automated snapshots"
8+
echo "Step 3: Listing manual snapshots"
9+
aws rds describe-db-snapshots --snapshot-type manual --query 'DBSnapshots[:5].{Id:DBSnapshotIdentifier,Status:Status,Size:AllocatedStorage}' --output table 2>/dev/null || echo " No manual snapshots"
10+
echo "Step 4: Listing cluster snapshots"
11+
aws rds describe-db-cluster-snapshots --query 'DBClusterSnapshots[:3].{Id:DBClusterSnapshotIdentifier,Cluster:DBClusterIdentifier,Status:Status}' --output table 2>/dev/null || echo " No cluster snapshots"
12+
echo ""; echo "Tutorial complete. No resources created — read-only."
13+
rm -rf "$WORK_DIR"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/ddb-query.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); TABLE="tut-query-${RANDOM_ID}"
5+
handle_error() { echo "ERROR on line $1"; trap - ERR; cleanup; exit 1; }; trap 'handle_error $LINENO' ERR
6+
cleanup() { echo ""; echo "Cleaning up..."; aws dynamodb delete-table --table-name "$TABLE" > /dev/null 2>&1 && echo " Deleted table"; rm -rf "$WORK_DIR"; echo "Done."; }
7+
echo "Step 1: Creating table with GSI"
8+
aws dynamodb create-table --table-name "$TABLE" --key-schema AttributeName=pk,KeyType=HASH AttributeName=sk,KeyType=RANGE --attribute-definitions AttributeName=pk,AttributeType=S AttributeName=sk,AttributeType=S AttributeName=status,AttributeType=S --billing-mode PAY_PER_REQUEST --global-secondary-indexes '[{"IndexName":"status-index","KeySchema":[{"AttributeName":"status","KeyType":"HASH"},{"AttributeName":"sk","KeyType":"RANGE"}],"Projection":{"ProjectionType":"ALL"}}]' > /dev/null
9+
aws dynamodb wait table-exists --table-name "$TABLE"
10+
echo "Step 2: Writing items"
11+
for i in 1 2 3 4 5; do
12+
STATUS=$( [ $((i % 2)) -eq 0 ] && echo "active" || echo "inactive" )
13+
aws dynamodb put-item --table-name "$TABLE" --item "{\"pk\":{\"S\":\"user-$i\"},\"sk\":{\"S\":\"profile\"},\"name\":{\"S\":\"User $i\"},\"status\":{\"S\":\"$STATUS\"}}" 2>/dev/null
14+
done
15+
echo " Wrote 5 items"
16+
echo "Step 3: Query by partition key"
17+
aws dynamodb query --table-name "$TABLE" --key-condition-expression "pk = :pk" --expression-attribute-values '{":pk":{"S":"user-1"}}' --query 'Items[].{pk:pk.S,name:name.S,status:status.S}' --output table
18+
echo "Step 4: Query GSI (active users)"
19+
aws dynamodb query --table-name "$TABLE" --index-name status-index --key-condition-expression "#s = :s" --expression-attribute-names '{"#s":"status"}' --expression-attribute-values '{":s":{"S":"active"}}' --query 'Items[].{pk:pk.S,name:name.S}' --output table
20+
echo "Step 5: Scan with filter"
21+
aws dynamodb scan --table-name "$TABLE" --filter-expression "#s = :s" --expression-attribute-names '{"#s":"status"}' --expression-attribute-values '{":s":{"S":"inactive"}}' --query '{Count:Count,Items:Items[].{pk:pk.S,name:name.S}}' --output table
22+
echo ""; echo "Tutorial complete."
23+
echo "Do you want to clean up? (y/n): "; read -r CHOICE; [[ "$CHOICE" =~ ^[Yy]$ ]] && cleanup
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/ttl.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); TABLE="tut-ttl-${RANDOM_ID}"
5+
handle_error() { echo "ERROR on line $1"; trap - ERR; cleanup; exit 1; }; trap 'handle_error $LINENO' ERR
6+
cleanup() { echo ""; echo "Cleaning up..."; aws dynamodb delete-table --table-name "$TABLE" > /dev/null 2>&1 && echo " Deleted table"; rm -rf "$WORK_DIR"; echo "Done."; }
7+
echo "Step 1: Creating table"
8+
aws dynamodb create-table --table-name "$TABLE" --key-schema AttributeName=pk,KeyType=HASH --attribute-definitions AttributeName=pk,AttributeType=S --billing-mode PAY_PER_REQUEST > /dev/null
9+
aws dynamodb wait table-exists --table-name "$TABLE"
10+
echo "Step 2: Enabling TTL"
11+
aws dynamodb update-time-to-live --table-name "$TABLE" --time-to-live-specification Enabled=true,AttributeName=expires_at > /dev/null
12+
echo " TTL enabled on 'expires_at' attribute"
13+
echo "Step 3: Writing items with TTL"
14+
PAST=$(($(date +%s) - 3600))
15+
FUTURE=$(($(date +%s) + 86400))
16+
aws dynamodb put-item --table-name "$TABLE" --item "{\"pk\":{\"S\":\"expired-item\"},\"data\":{\"S\":\"This should expire\"},\"expires_at\":{\"N\":\"$PAST\"}}" 2>/dev/null
17+
aws dynamodb put-item --table-name "$TABLE" --item "{\"pk\":{\"S\":\"active-item\"},\"data\":{\"S\":\"This stays\"},\"expires_at\":{\"N\":\"$FUTURE\"}}" 2>/dev/null
18+
echo " Wrote 2 items (1 expired, 1 active)"
19+
echo "Step 4: Describing TTL"
20+
aws dynamodb describe-time-to-live --table-name "$TABLE" --query 'TimeToLiveDescription.{Status:TimeToLiveStatus,Attribute:AttributeName}' --output table
21+
echo "Step 5: Scanning items"
22+
aws dynamodb scan --table-name "$TABLE" --query 'Items[].{pk:pk.S,data:data.S,expires:expires_at.N}' --output table
23+
echo " Note: DynamoDB deletes expired items within 48 hours, not immediately"
24+
echo ""; echo "Tutorial complete."
25+
echo "Do you want to clean up? (y/n): "; read -r CHOICE; [[ "$CHOICE" =~ ^[Yy]$ ]] && cleanup

0 commit comments

Comments
 (0)