Skip to content

Commit 4cd466d

Browse files
committed
Add messaging tutorials (batch 14)
1 parent 49f07d9 commit 4cd466d

9 files changed

Lines changed: 710 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# EventBridge: Schedule a Lambda function
2+
3+
Create an EventBridge scheduled rule that invokes a Lambda function every minute, verify execution in CloudWatch Logs, and clean up.
4+
5+
## Source
6+
7+
https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-get-started.html
8+
9+
## Use case
10+
11+
- ID: eventbridge/getting-started
12+
- Phase: create
13+
- Complexity: beginner
14+
- Core actions: events:PutRule, events:PutTargets
15+
16+
## What it does
17+
18+
1. Creates an IAM execution role for Lambda
19+
2. Creates a Node.js Lambda function that logs EventBridge events
20+
3. Creates an EventBridge rule with a `rate(1 minute)` schedule
21+
4. Grants EventBridge permission to invoke the function
22+
5. Adds the Lambda function as the rule target
23+
6. Waits for the rule to fire and verifies output in CloudWatch Logs
24+
25+
## Running
26+
27+
```bash
28+
bash amazon-eventbridge-gs.sh
29+
```
30+
31+
To auto-run with cleanup:
32+
33+
```bash
34+
echo 'y' | bash amazon-eventbridge-gs.sh
35+
```
36+
37+
## Resources created
38+
39+
- EventBridge rule (scheduled, rate 1 minute)
40+
- Lambda function (Node.js 22)
41+
- IAM role (with AWSLambdaBasicExecutionRole policy)
42+
- CloudWatch log group (created automatically by Lambda)
43+
44+
## Estimated time
45+
46+
- Run: ~90 seconds (includes 65s wait for rule to fire)
47+
- Cleanup: ~5 seconds
48+
49+
## Cost
50+
51+
Free tier eligible. Lambda and EventBridge invocations stay well within free tier limits for this tutorial.
52+
53+
## Related docs
54+
55+
- [Getting started with Amazon EventBridge](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-get-started.html)
56+
- [Creating a rule that runs on a schedule](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html)
57+
- [Tutorial: Use EventBridge to relay events to a Lambda function](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-log-ec2-instance-state.html)
58+
- [Schedule expressions using rate or cron](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-scheduled-rule-pattern.html)
59+
60+
---
61+
62+
## Appendix: Generation details
63+
64+
| Field | Value |
65+
|-------|-------|
66+
| Generation date | 2026-04-14 |
67+
| Script lines | 130 |
68+
| Script test result | EXIT 0, 91s, 6 steps, clean teardown |
69+
| Issues encountered | None |
70+
| Iterations | v1 direct to publish |
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Create an EventBridge rule that triggers a Lambda function on a schedule
2+
3+
This tutorial shows you how to create an Amazon EventBridge scheduled rule that invokes an AWS Lambda function every minute. You create the Lambda function, set up the rule, verify the function runs by checking CloudWatch Logs, and then clean up.
4+
5+
## Prerequisites
6+
7+
- AWS CLI configured with credentials and a default region
8+
- Permissions to create EventBridge rules, Lambda functions, and IAM roles
9+
10+
## Step 1: Create an execution role
11+
12+
Create an IAM role that grants the Lambda function permission to write logs.
13+
14+
```bash
15+
ROLE_ARN=$(aws iam create-role --role-name eb-tut-role \
16+
--assume-role-policy-document '{
17+
"Version":"2012-10-17",
18+
"Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]
19+
}' --query 'Role.Arn' --output text)
20+
21+
aws iam attach-role-policy --role-name eb-tut-role \
22+
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
23+
```
24+
25+
Wait about 10 seconds for the role to propagate before creating the function.
26+
27+
## Step 2: Create the Lambda function
28+
29+
Create a Node.js function that logs each EventBridge event it receives.
30+
31+
```javascript
32+
// index.mjs
33+
export const handler = async (event) => {
34+
console.log('EventBridge event received:', JSON.stringify(event, null, 2));
35+
return { statusCode: 200, body: 'Event processed' };
36+
};
37+
```
38+
39+
Package and deploy:
40+
41+
```bash
42+
zip function.zip index.mjs
43+
44+
aws lambda create-function --function-name eb-tut-handler \
45+
--zip-file fileb://function.zip \
46+
--handler index.handler --runtime nodejs22.x \
47+
--role $ROLE_ARN --timeout 30 \
48+
--architectures x86_64
49+
```
50+
51+
Wait for the function to become active:
52+
53+
```bash
54+
aws lambda wait function-active-v2 --function-name eb-tut-handler
55+
```
56+
57+
## Step 3: Create an EventBridge scheduled rule
58+
59+
Create a rule that fires every minute.
60+
61+
```bash
62+
RULE_ARN=$(aws events put-rule --name eb-tut-rule \
63+
--schedule-expression "rate(1 minute)" \
64+
--state ENABLED \
65+
--query 'RuleArn' --output text)
66+
```
67+
68+
## Step 4: Grant EventBridge permission to invoke Lambda
69+
70+
Add a resource-based policy that allows EventBridge to call the function.
71+
72+
```bash
73+
aws lambda add-permission --function-name eb-tut-handler \
74+
--statement-id eb-invoke --action lambda:InvokeFunction \
75+
--principal events.amazonaws.com --source-arn $RULE_ARN
76+
```
77+
78+
## Step 5: Add the Lambda function as a target
79+
80+
Attach the function to the rule so EventBridge invokes it on each trigger.
81+
82+
```bash
83+
FUNCTION_ARN=$(aws lambda get-function --function-name eb-tut-handler \
84+
--query 'Configuration.FunctionArn' --output text)
85+
86+
aws events put-targets --rule eb-tut-rule \
87+
--targets "Id=lambda-target,Arn=$FUNCTION_ARN"
88+
```
89+
90+
## Step 6: Verify in CloudWatch Logs
91+
92+
Wait about 60 seconds for the rule to fire, then check the function's log output:
93+
94+
```bash
95+
aws logs describe-log-streams \
96+
--log-group-name /aws/lambda/eb-tut-handler \
97+
--order-by LastEventTime --descending --limit 1
98+
99+
aws logs get-log-events \
100+
--log-group-name /aws/lambda/eb-tut-handler \
101+
--log-stream-name <log-stream-name> \
102+
--query 'events[].message' --output text
103+
```
104+
105+
You should see `EventBridge event received:` followed by the scheduled event JSON, which includes `"source": "aws.events"` and `"detail-type": "Scheduled Event"`.
106+
107+
## Cleanup
108+
109+
Delete all resources in reverse order:
110+
111+
```bash
112+
aws events remove-targets --rule eb-tut-rule --ids lambda-target
113+
aws events delete-rule --name eb-tut-rule
114+
aws lambda delete-function --function-name eb-tut-handler
115+
aws iam detach-role-policy --role-name eb-tut-role \
116+
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
117+
aws iam delete-role --role-name eb-tut-role
118+
aws logs delete-log-group --log-group-name /aws/lambda/eb-tut-handler
119+
```
120+
121+
The script automates all steps including cleanup. Run it with:
122+
123+
```bash
124+
bash amazon-eventbridge-gs.sh
125+
```
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/bin/bash
2+
# Tutorial: Create an EventBridge rule that triggers a Lambda function
3+
# Source: https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-get-started.html
4+
5+
WORK_DIR=$(mktemp -d)
6+
LOG_FILE="$WORK_DIR/eventbridge-$(date +%Y%m%d-%H%M%S).log"
7+
exec > >(tee -a "$LOG_FILE") 2>&1
8+
9+
REGION=${AWS_DEFAULT_REGION:-${AWS_REGION:-$(aws configure get region 2>/dev/null)}}
10+
if [ -z "$REGION" ]; then
11+
echo "ERROR: No AWS region configured. Set one with: export AWS_DEFAULT_REGION=us-east-1"
12+
exit 1
13+
fi
14+
export AWS_DEFAULT_REGION="$REGION"
15+
echo "Region: $REGION"
16+
17+
RANDOM_ID=$(openssl rand -hex 4)
18+
RULE_NAME="eb-tut-rule-${RANDOM_ID}"
19+
FUNCTION_NAME="eb-tut-handler-${RANDOM_ID}"
20+
ROLE_NAME="eb-tut-role-${RANDOM_ID}"
21+
22+
handle_error() { echo "ERROR on line $1"; trap - ERR; cleanup; exit 1; }
23+
trap 'handle_error $LINENO' ERR
24+
25+
cleanup() {
26+
echo ""
27+
echo "Cleaning up resources..."
28+
aws events remove-targets --rule "$RULE_NAME" --ids lambda-target > /dev/null 2>&1 && echo " Removed rule target"
29+
aws events delete-rule --name "$RULE_NAME" 2>/dev/null && echo " Deleted rule $RULE_NAME"
30+
aws lambda delete-function --function-name "$FUNCTION_NAME" 2>/dev/null && echo " Deleted function $FUNCTION_NAME"
31+
aws iam detach-role-policy --role-name "$ROLE_NAME" \
32+
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole 2>/dev/null
33+
aws iam delete-role --role-name "$ROLE_NAME" 2>/dev/null && echo " Deleted role $ROLE_NAME"
34+
aws logs delete-log-group --log-group-name "/aws/lambda/$FUNCTION_NAME" 2>/dev/null && echo " Deleted log group"
35+
rm -rf "$WORK_DIR"
36+
echo "Cleanup complete."
37+
}
38+
39+
# Step 1: Create IAM role
40+
echo "Step 1: Creating IAM role: $ROLE_NAME"
41+
ROLE_ARN=$(aws iam create-role --role-name "$ROLE_NAME" \
42+
--assume-role-policy-document '{
43+
"Version":"2012-10-17",
44+
"Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]
45+
}' --query 'Role.Arn' --output text)
46+
aws iam attach-role-policy --role-name "$ROLE_NAME" \
47+
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
48+
echo " Role ARN: $ROLE_ARN"
49+
echo " Waiting for role propagation..."
50+
sleep 10
51+
52+
# Step 2: Create Lambda function
53+
echo "Step 2: Creating Lambda function: $FUNCTION_NAME"
54+
cat > "$WORK_DIR/index.mjs" << 'EOF'
55+
export const handler = async (event) => {
56+
console.log('EventBridge event received:', JSON.stringify(event, null, 2));
57+
return { statusCode: 200, body: 'Event processed' };
58+
};
59+
EOF
60+
(cd "$WORK_DIR" && zip function.zip index.mjs > /dev/null)
61+
62+
aws lambda create-function --function-name "$FUNCTION_NAME" \
63+
--zip-file "fileb://$WORK_DIR/function.zip" \
64+
--handler index.handler --runtime nodejs22.x \
65+
--role "$ROLE_ARN" --timeout 30 \
66+
--architectures x86_64 \
67+
--query 'FunctionArn' --output text
68+
aws lambda wait function-active-v2 --function-name "$FUNCTION_NAME"
69+
FUNCTION_ARN=$(aws lambda get-function --function-name "$FUNCTION_NAME" --query 'Configuration.FunctionArn' --output text)
70+
71+
# Step 3: Create EventBridge rule (runs every minute)
72+
echo "Step 3: Creating EventBridge rule: $RULE_NAME (every 1 minute)"
73+
RULE_ARN=$(aws events put-rule --name "$RULE_NAME" \
74+
--schedule-expression "rate(1 minute)" \
75+
--state ENABLED \
76+
--query 'RuleArn' --output text)
77+
echo " Rule ARN: $RULE_ARN"
78+
79+
# Step 4: Grant EventBridge permission to invoke Lambda
80+
echo "Step 4: Granting EventBridge permission to invoke Lambda"
81+
aws lambda add-permission --function-name "$FUNCTION_NAME" \
82+
--statement-id eb-invoke --action lambda:InvokeFunction \
83+
--principal events.amazonaws.com --source-arn "$RULE_ARN" > /dev/null
84+
85+
# Step 5: Add Lambda as target
86+
echo "Step 5: Adding Lambda as rule target"
87+
aws events put-targets --rule "$RULE_NAME" \
88+
--targets "Id=lambda-target,Arn=$FUNCTION_ARN" > /dev/null
89+
echo " Target added"
90+
91+
# Step 6: Wait for the rule to fire and check logs
92+
echo "Step 6: Waiting for EventBridge to trigger Lambda (~60s)..."
93+
sleep 65
94+
95+
LOG_GROUP="/aws/lambda/$FUNCTION_NAME"
96+
FOUND_LOGS=false
97+
for i in $(seq 1 10); do
98+
LOG_STREAM=$(aws logs describe-log-streams --log-group-name "$LOG_GROUP" \
99+
--order-by LastEventTime --descending --limit 1 \
100+
--query 'logStreams[0].logStreamName' --output text 2>/dev/null || true)
101+
if [ -n "$LOG_STREAM" ] && [ "$LOG_STREAM" != "None" ]; then
102+
echo " Log stream: $LOG_STREAM"
103+
echo " Recent events:"
104+
aws logs get-log-events --log-group-name "$LOG_GROUP" \
105+
--log-stream-name "$LOG_STREAM" --limit 5 \
106+
--query 'events[].message' --output text
107+
FOUND_LOGS=true
108+
break
109+
fi
110+
sleep 5
111+
done
112+
if [ "$FOUND_LOGS" = false ]; then
113+
echo " Logs not available yet — the rule may not have fired yet"
114+
fi
115+
116+
echo ""
117+
echo "Tutorial complete."
118+
echo "Do you want to clean up all resources? (y/n): "
119+
read -r CHOICE
120+
if [[ "$CHOICE" =~ ^[Yy]$ ]]; then
121+
cleanup
122+
else
123+
echo "Resources left running. The rule fires every minute."
124+
echo "Manual cleanup:"
125+
echo " aws events remove-targets --rule $RULE_NAME --ids lambda-target"
126+
echo " aws events delete-rule --name $RULE_NAME"
127+
echo " aws lambda delete-function --function-name $FUNCTION_NAME"
128+
echo " aws iam detach-role-policy --role-name $ROLE_NAME --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
129+
echo " aws iam delete-role --role-name $ROLE_NAME"
130+
fi

tuts/110-amazon-sqs-gs/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# SQS: Create queues and send messages
2+
3+
## Source
4+
5+
https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-getting-started.html
6+
7+
## Use case
8+
9+
- **ID**: sqs/getting-started
10+
- **Level**: beginner
11+
- **Core actions**: `sqs:CreateQueue`, `sqs:SendMessage`
12+
13+
## Steps
14+
15+
1. Create a standard queue
16+
2. Create a dead-letter queue and configure redrive policy
17+
3. Send messages (individual and batch)
18+
4. Receive and process messages
19+
5. Delete processed messages
20+
6. Check queue attributes
21+
7. Create a FIFO queue and send a message
22+
23+
## Resources created
24+
25+
| Resource | Type |
26+
|----------|------|
27+
| `tut-queue-<random>` | Standard queue |
28+
| `tut-dlq-<random>` | Dead-letter queue |
29+
| `tut-fifo-<random>.fifo` | FIFO queue |
30+
31+
## Cost
32+
33+
Free tier includes 1 million requests/month. This tutorial sends fewer than 10 messages.
34+
35+
## Duration
36+
37+
~14 seconds
38+
39+
## Related docs
40+
41+
- [Getting started with Amazon SQS](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-getting-started.html)
42+
- [Amazon SQS dead-letter queues](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html)
43+
- [Amazon SQS FIFO queues](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/FIFO-queues.html)
44+
- [Sending messages in batches](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-batch-api-actions.html)
45+
46+
---
47+
48+
## Appendix
49+
50+
| Field | Value |
51+
|-------|-------|
52+
| Date | 2026-04-14 |
53+
| Script lines | 110 |
54+
| Exit code | 0 |
55+
| Runtime | 14s |
56+
| Steps | 7 |
57+
| Issues | Fixed f-string quoting |
58+
| Version | v1 |

0 commit comments

Comments
 (0)