Lab: Automating Event-Driven Security Notifications with Amazon EventBridge
Manage event sources to process, notify, and take action in response to events
Lab: Automating Event-Driven Security Notifications with Amazon EventBridge
In this lab, you will build a serverless event-processing workflow that detects when a new Amazon S3 bucket is created and sends an automated notification via Amazon SNS. This follows the AWS DevOps Engineer Professional domain for managing event sources to process, notify, and take action.
Prerequisites
- An active AWS Account.
- AWS CLI installed and configured with Administrator credentials.
- Basic knowledge of JSON for event patterns.
- Region: Ensure you are working in
us-east-1for consistency.
[!WARNING] This lab involves creating resources that may incur costs if not deleted. Ensure you complete the Teardown section.
Learning Objectives
- Configure an Amazon SNS Topic for event fan-out.
- Create an AWS Lambda function to process event metadata.
- Implement an Amazon EventBridge Rule to capture AWS API calls from CloudTrail.
- Grant cross-service permissions for event routing.
Architecture Overview
Step-by-Step Instructions
Step 1: Create an Amazon SNS Topic
We need a destination to "fan out" our notifications.
# Create the SNS topic
aws sns create-topic --name brainybee-event-alerts
# Subscribe your email (Replace placeholder)
aws sns subscribe --topic-arn arn:aws:sns:us-east-1:<YOUR_ACCOUNT_ID>:brainybee-event-alerts --protocol email --notification-endpoint <YOUR_EMAIL_ADDRESS>[!IMPORTANT] You must check your email inbox and click Confirm Subscription before you can receive messages.
▶Console alternative
Navigate to
. Select
, name it
brainybee-event-alerts. Once created, click
, select
, and enter your address.
Step 2: Create the Lambda Execution Role
Lambda needs permission to write logs to CloudWatch and publish to SNS.
- Create a file named
trust-policy.json:json{ "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" }] } - Create the role:
bash
aws iam create-role --role-name brainybee-lambda-event-role --assume-role-policy-document file://trust-policy.json # Attach basic execution and SNS publish policies aws iam attach-role-policy --role-name brainybee-lambda-event-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole aws iam attach-role-policy --role-name brainybee-lambda-event-role --policy-arn arn:aws:iam::aws:policy/AmazonSNSFullAccess
Step 3: Create the Lambda Processing Function
This function will parse the S3 event and send a formatted alert.
- Create
index.mjs:javascriptimport { SNSClient, PublishCommand } from "@aws-sdk/client-sns"; const sns = new SNSClient({}); export const handler = async (event) => { console.log("Event Received:", JSON.stringify(event, null, 2)); const bucketName = event.detail.requestParameters.bucketName; const region = event.detail.awsRegion; const message = `Security Alert: A new S3 bucket was created.\nBucket Name: ${bucketName}\nRegion: ${region}`; await sns.send(new PublishCommand({ TopicArn: process.env.SNS_TOPIC_ARN, Message: message, Subject: "S3 Security Notification" })); }; - Zip and deploy:
bash
zip function.zip index.mjs aws lambda create-function --function-name brainybee-s3-notifier \ --runtime nodejs18.x --role arn:aws:iam::<YOUR_ACCOUNT_ID>:role/brainybee-lambda-event-role \ --handler index.handler --zip-file fileb://function.zip \ --environment Variables={SNS_TOPIC_ARN=arn:aws:sns:us-east-1:<YOUR_ACCOUNT_ID>:brainybee-event-alerts}
Step 4: Configure the EventBridge Rule
We will create a rule that triggers whenever CloudTrail records a CreateBucket event.
# Create the rule
aws events put-rule --name "S3CreateBucketRule" --event-pattern '{
"source": ["aws.s3"],
"detail-type": ["AWS API Call via CloudTrail"],
"detail": {
"eventSource": ["s3.amazonaws.com"],
"eventName": ["CreateBucket"]
}
}'
# Add Lambda as the target
aws events put-targets --rule "S3CreateBucketRule" --targets "Id"="1","Arn"="arn:aws:lambda:us-east-1:<YOUR_ACCOUNT_ID>:function:brainybee-s3-notifier"
# Grant EventBridge permission to invoke Lambda
aws lambda add-permission --function-name brainybee-s3-notifier --statement-id EventBridgeInvoke --action lambda:InvokeFunction --principal events.amazonaws.com --source-arn arn:aws:events:us-east-1:<YOUR_ACCOUNT_ID>:rule/S3CreateBucketRuleCheckpoints
- Trigger the Event: Create a dummy S3 bucket.
bash
aws s3 mb s3://brainybee-test-event-$(date +%s) - Verify Logs: Check CloudWatch Logs to see if the Lambda executed.
bash
aws logs describe-log-streams --log-group-name /aws/lambda/brainybee-s3-notifier - Check Email: Verify you received the "S3 Security Notification" message.
Clean-Up / Teardown
[!WARNING] Failure to delete these resources will result in ongoing (though minimal) CloudWatch Log storage costs.
# Delete S3 test bucket
aws s3 rb s3://<YOUR_TEST_BUCKET_NAME>
# Delete EventBridge Rule and Target
aws events remove-targets --rule "S3CreateBucketRule" --ids "1"
aws events delete-rule --name "S3CreateBucketRule"
# Delete Lambda and IAM Role
aws lambda delete-function --function-name brainybee-s3-notifier
aws iam detach-role-policy --role-name brainybee-lambda-event-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
aws iam detach-role-policy --role-name brainybee-lambda-event-role --policy-arn arn:aws:iam::aws:policy/AmazonSNSFullAccess
aws iam delete-role --role-name brainybee-lambda-event-role
# Delete SNS Topic
aws sns delete-topic --topic-arn arn:aws:sns:us-east-1:<YOUR_ACCOUNT_ID>:brainybee-event-alertsTroubleshooting
| Error | Cause | Fix |
|---|---|---|
| No notification received | Subscription not confirmed | Check email spam folder and confirm subscription. |
| Lambda not triggered | Event pattern mismatch | Check if CloudTrail is enabled in the region. EventBridge relies on CloudTrail for S3 API events. |
| AccessDenied in Lambda | Missing SNS permission | Ensure the IAM role has AmazonSNSFullAccess or a custom policy for the topic. |
Stretch Challenge
Automated Remediation: Modify the Lambda function to check if the new bucket has a specific tag (e.g., Compliance=Internal). If the tag is missing, have the Lambda function automatically apply it or delete the bucket.
Cost Estimate
| Service | Usage | Cost |
|---|---|---|
| AWS Lambda | First 1M requests/mo free | $0.00 |
| EventBridge | Management events are free | $0.00 |
| SNS | First 1,000 emails/mo free | $0.00 |
| S3 | Storage < 5GB (Free Tier) | $0.00 |
| Total | $0.00 |
Concept Review
In this lab, we utilized an Event-Driven Architecture. This design pattern is asynchronous and decoupled, meaning the S3 service doesn't need to know about the notification logic.
Comparison: EventBridge vs. S3 Event Notifications
| Feature | S3 Event Notifications | Amazon EventBridge |
|---|---|---|
| Destinations | SNS, SQS, Lambda | 20+ AWS Services, SaaS |
| Cross-Account | Difficult | Native Support |
| Advanced Filtering | Limited (Prefix/Suffix) | Deep JSON Pattern Matching |
| Schema Registry | No | Yes |