|
| 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 |
0 commit comments