Skip to content

Commit 115c146

Browse files
committed
Add compute tutorials (batch 4)
1 parent 49f07d9 commit 115c146

5 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/conc.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); FUNC="tut-conc-${RANDOM_ID}"; ROLE="lambda-conc-role-${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 lambda delete-function-concurrency --function-name "$FUNC" 2>/dev/null; aws lambda delete-function --function-name "$FUNC" 2>/dev/null && echo " Deleted function"; aws iam detach-role-policy --role-name "$ROLE" --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole 2>/dev/null; aws iam delete-role --role-name "$ROLE" 2>/dev/null && echo " Deleted role"; rm -rf "$WORK_DIR"; echo "Done."; }
7+
ROLE_ARN=$(aws iam create-role --role-name "$ROLE" --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}' --query 'Role.Arn' --output text)
8+
aws iam attach-role-policy --role-name "$ROLE" --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole; sleep 10
9+
cat > "$WORK_DIR/index.py" << 'EOF'
10+
import time
11+
def handler(event, context):
12+
time.sleep(1)
13+
return {"remaining_ms": context.get_remaining_time_in_millis()}
14+
EOF
15+
(cd "$WORK_DIR" && zip func.zip index.py > /dev/null)
16+
aws lambda create-function --function-name "$FUNC" --zip-file "fileb://$WORK_DIR/func.zip" --handler index.handler --runtime python3.12 --role "$ROLE_ARN" --architectures x86_64 > /dev/null
17+
aws lambda wait function-active-v2 --function-name "$FUNC"
18+
echo "Step 1: Getting account concurrency limits"
19+
aws lambda get-account-settings --query 'AccountLimit.{Total:TotalCodeSize,Concurrent:ConcurrentExecutions,Unreserved:UnreservedConcurrentExecutions}' --output table
20+
echo "Step 2: Setting reserved concurrency"
21+
aws lambda put-function-concurrency --function-name "$FUNC" --reserved-concurrent-executions 5 --query 'ReservedConcurrentExecutions' --output text > /dev/null
22+
echo " Reserved: 5 concurrent executions"
23+
echo "Step 3: Getting function concurrency"
24+
aws lambda get-function-concurrency --function-name "$FUNC" --query 'ReservedConcurrentExecutions' --output text
25+
echo "Step 4: Removing reserved concurrency"
26+
aws lambda delete-function-concurrency --function-name "$FUNC"
27+
echo " Reserved concurrency removed"
28+
echo ""; echo "Tutorial complete."
29+
echo "Do you want to clean up? (y/n): "; read -r CHOICE; [[ "$CHOICE" =~ ^[Yy]$ ]] && cleanup

tuts/175-ec2-tags/ec2-tags.sh

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/tags.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)
5+
VPC_ID=$(aws ec2 describe-vpcs --filters "Name=isDefault,Values=true" --query 'Vpcs[0].VpcId' --output text)
6+
SG_ID=$(aws ec2 create-security-group --group-name "tut-tags-${RANDOM_ID}" --description "Tag tutorial" --vpc-id "$VPC_ID" --query 'GroupId' --output text)
7+
handle_error() { echo "ERROR on line $1"; trap - ERR; cleanup; exit 1; }; trap 'handle_error $LINENO' ERR
8+
cleanup() { echo ""; echo "Cleaning up..."; aws ec2 delete-security-group --group-id "$SG_ID" 2>/dev/null && echo " Deleted SG"; rm -rf "$WORK_DIR"; echo "Done."; }
9+
echo "Step 1: Adding tags"
10+
aws ec2 create-tags --resources "$SG_ID" --tags Key=Environment,Value=tutorial Key=Project,Value=tag-demo Key=Owner,Value=tutorial-user Key=CostCenter,Value=12345
11+
echo " Added 4 tags to $SG_ID"
12+
echo "Step 2: Describing tags"
13+
aws ec2 describe-tags --filters "Name=resource-id,Values=$SG_ID" --query 'Tags[].{Key:Key,Value:Value}' --output table
14+
echo "Step 3: Finding resources by tag"
15+
aws ec2 describe-security-groups --filters "Name=tag:Project,Values=tag-demo" --query 'SecurityGroups[].{Id:GroupId,Name:GroupName}' --output table
16+
echo "Step 4: Removing a tag"
17+
aws ec2 delete-tags --resources "$SG_ID" --tags Key=CostCenter
18+
echo " Removed CostCenter tag"
19+
aws ec2 describe-tags --filters "Name=resource-id,Values=$SG_ID" --query 'Tags[].{Key:Key,Value:Value}' --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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/lambda-destinations.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: Creating roles and functions"; RID=$(openssl rand -hex 4); R="dest-role-$RID"; Q="dest-queue-$RID"; F="dest-func-$RID"; ROLE_ARN=$(aws iam create-role --role-name "$R" --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}' --query Role.Arn --output text); aws iam attach-role-policy --role-name "$R" --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole; aws iam attach-role-policy --role-name "$R" --policy-arn arn:aws:iam::aws:policy/AmazonSQSFullAccess; sleep 10; QU=$(aws sqs create-queue --queue-name "$Q" --query QueueUrl --output text); QA=$(aws sqs get-queue-attributes --queue-url "$QU" --attribute-names QueueArn --query Attributes.QueueArn --output text); D=$(mktemp -d); echo "def handler(e,c): return {\"result\":\"success\"}" > "$D/i.py"; (cd "$D" && zip f.zip i.py > /dev/null); aws lambda create-function --function-name "$F" --zip-file "fileb://$D/f.zip" --handler i.handler --runtime python3.12 --role "$ROLE_ARN" --architectures x86_64 > /dev/null; aws lambda wait function-active-v2 --function-name "$F"; echo "Step 2: Configuring on-success destination"; aws lambda put-function-event-invoke-config --function-name "$F" --destination-config "{\"OnSuccess\":{\"Destination\":\"$QA\"}}" > /dev/null; echo " On-success -> SQS queue"; echo "Step 3: Invoking async"; aws lambda invoke --function-name "$F" --invocation-type Event --cli-binary-format raw-in-base64-out --payload '{}' "$D/out.json" > /dev/null; echo " Invoked async"; sleep 10; echo "Step 4: Checking SQS for result"; aws sqs receive-message --queue-url "$QU" --max-number-of-messages 1 --wait-time-seconds 5 --query "Messages[0].Body" --output text 2>/dev/null | python3 -c "import sys,json;d=json.loads(sys.stdin.read());print(f\" Result: {d.get('requestPayload',{})}\n Response: {d.get('responsePayload',{})}\")" 2>/dev/null || echo " No message yet"; echo "Do you want to clean up? (y/n): "; read -r C; [[ "$C" =~ ^[Yy]$ ]] && { aws lambda delete-function --function-name "$F" 2>/dev/null; aws sqs delete-queue --queue-url "$QU" 2>/dev/null; aws iam detach-role-policy --role-name "$R" --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole 2>/dev/null; aws iam detach-role-policy --role-name "$R" --policy-arn arn:aws:iam::aws:policy/AmazonSQSFullAccess 2>/dev/null; aws iam delete-role --role-name "$R" 2>/dev/null; rm -rf "$D"; echo Done; }

tuts/178-ec2-amis/ec2-amis.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
exec > >(tee -a "$(mktemp -d)/amis.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 Amazon Linux 2023 AMIs"
5+
aws ec2 describe-images --owners amazon --filters "Name=name,Values=al2023-ami-2023*-x86_64" "Name=state,Values=available" --query 'sort_by(Images, &CreationDate)[-3:].{Id:ImageId,Name:Name,Created:CreationDate}' --output table
6+
echo "Step 2: Listing Ubuntu AMIs"
7+
aws ec2 describe-images --owners 099720109477 --filters "Name=name,Values=ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04*" "Name=architecture,Values=x86_64" --query 'sort_by(Images, &CreationDate)[-3:].{Id:ImageId,Name:Name}' --output table
8+
echo "Step 3: Describing a specific AMI"
9+
AMI=$(aws ec2 describe-images --owners amazon --filters "Name=name,Values=al2023-ami-2023*-x86_64" --query 'sort_by(Images, &CreationDate)[-1].ImageId' --output text)
10+
aws ec2 describe-images --image-ids "$AMI" --query 'Images[0].{Id:ImageId,Arch:Architecture,Root:RootDeviceType,Virt:VirtualizationType}' --output table
11+
echo "Step 4: Listing your own AMIs"
12+
aws ec2 describe-images --owners self --query 'Images[:5].{Id:ImageId,Name:Name,State:State}' --output table 2>/dev/null || echo " No custom AMIs"
13+
echo ""; echo "Tutorial complete. No resources created — read-only."
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
WORK_DIR=$(mktemp -d); exec > >(tee -a "$WORK_DIR/pg.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); PG1="tut-cluster-${RANDOM_ID}"; PG2="tut-spread-${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 ec2 delete-placement-group --group-name "$PG1" 2>/dev/null && echo " Deleted $PG1"; aws ec2 delete-placement-group --group-name "$PG2" 2>/dev/null && echo " Deleted $PG2"; rm -rf "$WORK_DIR"; echo "Done."; }
7+
echo "Step 1: Creating cluster placement group"
8+
aws ec2 create-placement-group --group-name "$PG1" --strategy cluster --query 'PlacementGroup.{Name:GroupName,Strategy:Strategy,State:State}' --output table
9+
echo "Step 2: Creating spread placement group"
10+
aws ec2 create-placement-group --group-name "$PG2" --strategy spread --query 'PlacementGroup.{Name:GroupName,Strategy:Strategy,State:State}' --output table
11+
echo "Step 3: Describing placement groups"
12+
aws ec2 describe-placement-groups --group-names "$PG1" "$PG2" --query 'PlacementGroups[].{Name:GroupName,Strategy:Strategy,State:State}' --output table
13+
echo ""; echo "Tutorial complete."
14+
echo "Do you want to clean up? (y/n): "; read -r CHOICE; [[ "$CHOICE" =~ ^[Yy]$ ]] && cleanup

0 commit comments

Comments
 (0)