BrainyBeeBrainyBee
ExploreBlogStart Studying
HomeAWS Certified DevOps Engineer - Professional (DOP-C02)Lab: Automating Event-Driven Monitoring and Remediation
Hands-On Lab845 words

Lab: Automating Event-Driven Monitoring and Remediation

Automate monitoring and event management of complex environments

Lab: Automating Event-Driven Monitoring and Remediation

[!WARNING] Remember to run the teardown commands at the end of this lab to avoid ongoing charges. Most resources used here are Free Tier eligible, but costs can accumulate if left running.

Prerequisites

Before starting this lab, ensure you have:

  • An AWS Account with administrative access.
  • AWS CLI installed and configured locally with <YOUR_PROFILE>.
  • Basic knowledge of Python (for the Lambda remediation script).
  • A verified email address to receive SNS notifications.

Learning Objectives

By the end of this lab, you will be able to:

  • Configure an Amazon SNS topic for real-time alerting.
  • Develop an AWS Lambda function to programmatically remediate unauthorized security group changes.
  • Create an Amazon EventBridge rule to detect specific API calls via CloudTrail and trigger automation.
  • Implement an event-driven, asynchronous design pattern for system governance.

Architecture Overview

This lab implements a self-healing pattern. When a user creates a "wide-open" (0.0.0.0/0) security group rule, EventBridge detects the event and triggers a Lambda function to revoke the rule and notify the security team via SNS.

Loading Diagram...
Figure 1 — Mermaid diagram

Step-by-Step Instructions

Step 1: Create an SNS Topic for Alerts

We need a communication channel to notify us when a remediation event occurs.

bash
aws sns create-topic --name brainybee-security-alerts
▶Console alternative
  1. Navigate to SNS > Topics.
  2. Click Create topic.
  3. Type: Standard. Name: brainybee-security-alerts.
  4. Click Create topic.

Step 2: Subscribe to the Topic

Replace <YOUR_EMAIL> with your actual email address.

bash
aws sns subscribe \ --topic-arn arn:aws:sns:<YOUR_REGION>:<YOUR_ACCOUNT_ID>:brainybee-security-alerts \ --protocol email \ --notification-endpoint <YOUR_EMAIL>

[!IMPORTANT] You must click the "Confirm Subscription" link in the email sent by AWS before you can receive alerts.

Step 3: Create the Remediation Lambda Function

This function will parse the EventBridge event, identify the security group, and revoke any rules that allow 0.0.0.0/0.

First, create a file named lambda_function.py:

python
import boto3 ec2 = boto3.client('ec2') def lambda_handler(event, context): group_id = event['detail']['requestParameters']['groupId'] items = event['detail']['requestParameters']['ipPermissions']['items'] for item in items: for ip_range in item.get('ipRanges', {}).get('items', []): if ip_range.get('cidrIp') == '0.0.0.0/0': print(f"Remediating group {group_id} - Revoking 0.0.0.0/0") ec2.revoke_security_group_ingress( GroupId=group_id, IpPermissions=[item] ) return {'status': 'remediated'}

Zip the file and deploy via CLI (ensure your IAM role has ec2:RevokeSecurityGroupIngress permissions):

bash
zip function.zip lambda_function.py aws lambda create-function --function-name SecurityGroupRemediator \ --zip-file fileb://function.zip --handler lambda_function.lambda_handler \ --runtime python3.9 --role <YOUR_LAMBDA_EXECUTION_ROLE_ARN>

Step 4: Configure the EventBridge Rule

We will trigger our automation whenever a AuthorizeSecurityGroupIngress event is recorded.

bash
aws events put-rule --name DetectWideOpenSG \ --event-pattern '{"source":["aws.ec2"],"detail-type":["AWS API Call via CloudTrail"],"detail":{"eventSource":["ec2.amazonaws.com"],"eventName":["AuthorizeSecurityGroupIngress"]}}'

Connect the Lambda function as a target:

bash
aws events put-targets --rule DetectWideOpenSG \ --targets "Id"="1","Arn"="arn:aws:lambda:<YOUR_REGION>:<YOUR_ACCOUNT_ID>:function:SecurityGroupRemediator"

Checkpoints

  1. Verification: Go to the EC2 console and create a security group rule allowing 0.0.0.0/0 on port 22.
  2. Observation: Wait 1-2 minutes for CloudTrail to process the API call.
  3. Result: Refresh the security group page. The rule should be gone.
  4. Logs: Check CloudWatch Logs for /aws/lambda/SecurityGroupRemediator to see the execution details.

Troubleshooting

IssueLikely CauseSolution
Lambda doesn't triggerCloudTrail DelayCloudTrail can take up to 15 mins to deliver events to EventBridge in some regions.
Permission DeniedMissing IAM PolicyEnsure Lambda role has ec2:RevokeSecurityGroupIngress and logs:CreateLogGroup.
SNS No EmailUnconfirmed SubCheck your spam folder for the AWS Subscription Confirmation email.

Clean-Up / Teardown

Run these commands to remove lab resources:

bash
# Delete EventBridge Rule Targets aws events remove-targets --rule DetectWideOpenSG --ids "1" # Delete EventBridge Rule aws events delete-rule --name DetectWideOpenSG # Delete Lambda Function aws lambda delete-function --function-name SecurityGroupRemediator # Delete SNS Topic aws sns delete-topic --topic-arn arn:aws:sns:<YOUR_REGION>:<YOUR_ACCOUNT_ID>:brainybee-security-alerts

Stretch Challenge

Difficulty: Challenge Instead of a custom Lambda function, use AWS Config with a managed rule vpc-sg-open-only-to-authorized-ports and configure an SSM Automation Document as a remediation action. This is the enterprise-standard way to handle configuration drift at scale.

Cost Estimate

ServiceUsageEstimated Cost (Monthly)
EventBridge< 1,000 events$0.00 (Free Tier)
Lambda< 100 requests$0.00 (Free Tier)
SNS< 1,000 notifications$0.00 (Free Tier)
CloudTrail1 Management Trail$0.00 (First copy is free)

Concept Review

Remediation Logic Flow

Compiling TikZ diagram…
⏳
Running TeX engine…
This may take a few seconds
Figure 2 — TikZ diagram

Comparison: EventBridge vs. AWS Config

FeatureAmazon EventBridgeAWS Config
TriggerReal-time API calls / State changesConfiguration changes / Periodic snapshots
LatencyNear real-timeMinutes to hours
Use CaseAsynchronous workflows, custom appsCompliance, auditing, and governance
RemediationLambda, SSM, Step FunctionsSSM Automation, Lambda

[!TIP] In a complex environment, use EventBridge for immediate operational response and AWS Config for long-term compliance reporting.

All AWS Certified DevOps Engineer - Professional (DOP-C02) Study Resources

Related Notes

  • Automating Monitoring and Event Management in Complex Environments1,050 words
  • Mastering AWS Alerting and Automated Remediation1,050 words
  • Study Guide: Analyzing Failed Deployments in AWS940 words
  • Incident Analysis: Troubleshooting Failed Processes in AWS1,050 words
  • Mastering AWS Monitoring & Security Analytics: Logs, Metrics, and Findings1,050 words
  • AWS Log Analysis: Athena, CloudWatch Insights, and OpenSearch920 words
  • Analyzing Real-Time Log Streams with Amazon Kinesis Data Streams985 words
  • CloudWatch Anomaly Detection Alarms: Professional Study Guide820 words
  • AWS Application Storage Patterns: EBS, EFS, and S31,054 words
  • Lab: Automating Security Controls and Data Protection with AWS Secrets Manager and Config942 words
  • Master Study Guide: Automating Security Controls & Data Protection (AWS DOP-C02)1,184 words
  • Mastering AWS CloudFormation StackSets: Multi-Account & Multi-Region Orchestration895 words

Ready to study AWS Certified DevOps Engineer - Professional (DOP-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up.

Start Studying

Ready to study AWS Certified DevOps Engineer - Professional (DOP-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free
AWS Certified DevOps Engineer - Professional (DOP-C02) ResourcesExplore All HivesBlogHome

© 2026 BrainyBee. Free AI-powered exam prep.

Loading Diagram...
Flowchart, left to right. User Change connects to AWS CloudTrail ("AuthorizeSecurityGroupIngress"). B connects to Amazon EventBridge. C connects to AWS Lambda ("Match Pattern"). C connects to Amazon SNS ("Match Pattern"). D connects to EC2 Security Group ("Revoke Security Group Rule"). E connects to DevOps Engineer ("Email Alert").