AWS Builder Center

Automating AWS Security Alerts with AWS Lambda and Amazon EventBridge

Imagine an AWS environment where an administrator accidentally disables AWS CloudTrail logging. Without automated monitoring, the security team might not notice the change immediately. During this period, suspicious activities could occur without proper audit visibility.

Cloud security is not only about preventing attacks. It is also about detecting suspicious activities and responding to them quickly.

This is where event-driven security automation becomes valuable.
By combining Amazon EventBridge, AWS Lambda, and Amazon Simple Notification Service (Amazon SNS), we can build a serverless security response system that detects important events, executes predefined actions, and notifies administrators.
In this article, we will explore how to design a simple security automation workflow on AWS.

The Problem: Manual Security Monitoring

Traditional security monitoring often depends on administrators reviewing logs, dashboards, and alerts.
This approach introduces several challenges:
  • Delayed detection: Security events may remain unnoticed for extended periods.
  • Manual response: Administrators must investigate and respond to every alert.
  • Operational overhead: Monitoring multiple AWS resources becomes difficult as infrastructure grows.
  • Inconsistent response: Different administrators may respond to similar incidents differently.
A better approach is to automate repetitive security responses while keeping human approval for sensitive operations.

Architecture Overview

The proposed architecture uses an event-driven approach.
<escape>```text AWS CloudTrail | v Amazon EventBridge | v Event Rule | v AWS Lambda | +------> Amazon SNS | | | v | Email Notification | v Security Response

### Components

**1. AWS CloudTrail**

CloudTrail records API activity within an AWS account. These events provide visibility into actions performed by users, roles, and AWS services.

**2. Amazon EventBridge**

EventBridge receives events and evaluates them against configured rules. When an event matches a rule, EventBridge can invoke a target such as Lambda.

**3. AWS Lambda**

Lambda executes the response logic without requiring server management. It can inspect the event, evaluate predefined conditions, and initiate an appropriate response.

**4. Amazon SNS**

SNS delivers notifications to subscribed endpoints, such as email addresses, allowing administrators to investigate security events.

## Use Case: Detecting Changes to Security Groups

Consider a scenario where someone modifies an EC2 security group to allow inbound SSH access from the entire internet.

An unrestricted SSH rule can increase the attack surface of an EC2 instance.

We can use CloudTrail events, EventBridge, and Lambda to detect this configuration change and notify the security team.

### Workflow

1. An administrator or user modifies an EC2 security group.
2. CloudTrail records the API activity.
3. EventBridge evaluates the event against a security monitoring rule.
4. Lambda receives the matching event.
5. Lambda extracts the relevant information, such as the security group ID and actor.
6. SNS sends an alert to the security administrator.

This workflow reduces the time required to identify potentially risky configuration changes.

## Implementation

### Step 1: Create an EventBridge Rule

Create a rule in Amazon EventBridge to monitor security group modification events.

A sample event pattern is:

<escape>```json
{
"source": ["aws.ec2"],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": ["ec2.amazonaws.com"],
"eventName": [
"AuthorizeSecurityGroupIngress"
]
}
}
```</escape>

This pattern matches events associated with the `AuthorizeSecurityGroupIngress` API operation.

**Note:** This event pattern detects the API operation, not necessarily an unrestricted SSH rule. The Lambda function must inspect the event parameters to determine whether the change introduces a risky configuration.

### Step 2: Create an SNS Notification Topic

Create an SNS topic named `SecurityAlerts`.

Configure an email subscription for the security administrator and confirm the subscription.

The topic will be used to deliver notifications whenever a matching event is detected.

### Step 3: Create the Lambda Function

The following Python example demonstrates the alerting logic.

<escape>```python
import json
import os
import boto3

sns = boto3.client("sns")

TOPIC_ARN = os.environ["SNS_TOPIC_ARN"]

def lambda_handler(event, context):
detail = event.get("detail", {})

group_id = detail.get(
"requestParameters", {}
).get("groupId", "Unknown")

actor = detail.get(
"userIdentity", {}
).get("arn", "Unknown")

message = {
"event": detail.get("eventName"),
"security_group": group_id,
"actor": actor,
"region": event.get("region")
}

sns.publish(
TopicArn=TOPIC_ARN,
Subject="AWS Security Alert",
Message=json.dumps(message, indent=2)
)

return {
"statusCode": 200,
"body": "Security alert sent"
}
```</escape>

Configure the `SNS_TOPIC_ARN` environment variable with the ARN of the SNS topic.

The Lambda execution role should have permission to publish to the designated SNS topic. EventBridge also needs permission to invoke the Lambda function.

This example is an alerting demonstration. Production implementations should validate event parameters, handle malformed events, and apply least-privilege IAM permissions.

### Step 4: Test the Workflow

Perform a controlled test in a non-production AWS environment.

1. Modify a test security group.
2. Verify that CloudTrail records the API activity.
3. Confirm that EventBridge matches the event.
4. Check the Lambda execution logs in Amazon CloudWatch.
5. Verify that the SNS notification reaches the subscribed email address.

The expected outcome is an alert containing the event name, security group identifier, actor, and AWS Region.

## Security Best Practices

Security automation itself must be designed securely.

### Least-Privilege IAM

Avoid granting Lambda broad permissions such as `AdministratorAccess`.

Grant only the permissions required for the intended response.

### Human Approval for High-Risk Actions

Automatically notifying administrators is generally safer than immediately modifying production resources.

For actions such as revoking access keys, changing network configurations, or isolating workloads, consider an approval-based workflow.

### Monitoring and Logging

Use Amazon CloudWatch Logs and metrics to monitor Lambda execution, failures, and processing latency.

### Idempotency

A security event may be delivered more than once. Design response logic so repeated processing does not cause unintended consequences.

### Error Handling

Configure appropriate retries and a dead-letter queue where applicable. Ensure failed security events can be investigated rather than silently discarded.

## Benefits of Event-Driven Security Automation

This architecture offers several advantages:

- **Faster detection:** Security events can trigger responses without manual log inspection.
- **Reduced operational effort:** Repetitive monitoring tasks can be automated.
- **Scalability:** Serverless components can handle varying event volumes.
- **Improved visibility:** Notifications provide actionable information to security teams.
- **Cost efficiency:** Lambda and EventBridge can reduce the need for continuously running monitoring infrastructure.

## Conclusion

Cloud security requires more than collecting logs. Organizations need mechanisms that transform security events into timely and actionable responses.

Amazon EventBridge, AWS Lambda, and Amazon SNS provide a flexible foundation for building event-driven security automation.

By starting with simple alerting workflows and gradually introducing controlled remediation, organizations can improve their security posture while maintaining operational control.

This architecture also provides a foundation for more advanced cloud security systems involving AWS Security Hub, Amazon GuardDuty, AWS Config, and multi-account monitoring.

**The key takeaway:** Automate the detection of security events, make responses predictable, and keep cri
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article