|
| 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); F="dlq-func-${RANDOM_ID}"; R="dlq-func-role-${RANDOM_ID}"; Q="dlq-func-q-${RANDOM_ID}" |
| 5 | +handle_error() { echo "ERROR on line $1"; trap - ERR; cleanup; exit 1; }; trap 'handle_error $LINENO' ERR |
| 6 | +cleanup() { echo "Cleaning up..."; aws lambda delete-function --function-name "$F" 2>/dev/null; [ -n "$QU" ] && 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 "$WORK_DIR"; echo "Done."; } |
| 7 | +RA=$(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) |
| 8 | +aws iam attach-role-policy --role-name "$R" --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole |
| 9 | +aws iam attach-role-policy --role-name "$R" --policy-arn arn:aws:iam::aws:policy/AmazonSQSFullAccess; sleep 10 |
| 10 | +QU=$(aws sqs create-queue --queue-name "$Q" --query QueueUrl --output text) |
| 11 | +QA=$(aws sqs get-queue-attributes --queue-url "$QU" --attribute-names QueueArn --query Attributes.QueueArn --output text) |
| 12 | +echo "Step 1: Creating function that always fails" |
| 13 | +echo "def handler(e,c): raise Exception('Intentional failure')" > "$WORK_DIR/i.py" |
| 14 | +(cd "$WORK_DIR" && zip f.zip i.py > /dev/null) |
| 15 | +aws lambda create-function --function-name "$F" --zip-file "fileb://$WORK_DIR/f.zip" --handler i.handler --runtime python3.12 --role "$RA" --dead-letter-config "{\"TargetArn\":\"$QA\"}" --architectures x86_64 > /dev/null |
| 16 | +aws lambda wait function-active-v2 --function-name "$F" |
| 17 | +echo " Function with DLQ configured" |
| 18 | +echo "Step 2: Invoking async (will fail and go to DLQ)" |
| 19 | +aws lambda invoke --function-name "$F" --invocation-type Event --cli-binary-format raw-in-base64-out --payload '{}' "$WORK_DIR/out.json" > /dev/null |
| 20 | +echo " Invoked async — Lambda will retry twice then send to DLQ" |
| 21 | +echo "Step 3: Checking DLQ (after retries, ~3 min)" |
| 22 | +echo " DLQ messages: $(aws sqs get-queue-attributes --queue-url "$QU" --attribute-names ApproximateNumberOfMessages --query Attributes.ApproximateNumberOfMessages --output text)" |
| 23 | +echo " (Message will appear after Lambda exhausts retries)" |
| 24 | +echo ""; echo "Tutorial complete." |
| 25 | +echo "Do you want to clean up? (y/n): "; read -r C; [[ "$C" =~ ^[Yy]$ ]] && cleanup |
0 commit comments