
Build a Serverless Alert Pipeline for High-Risk AWS Account Activity
Use CloudTrail events, Amazon EventBridge, and Amazon SNS to get notified within seconds of root usage, IAM persistence attempts, and logging tampering, with a deployable CloudFormation template.
Most AWS account compromises follow a familiar pattern: an attacker gets credentials, creates a way to keep access, and then tries to hide. The good news is that each of these steps leaves a distinct API call in AWS CloudTrail. In this article, you'll build a small, serverless pipeline that alerts you when those calls happen, with no servers and no extra services to maintain.
What you'll build
Management events from CloudTrail are delivered to the default EventBridge event bus automatically, so you don't need to configure a trail for this pipeline to work. (You should still run an organization-wide trail for retention and investigation. This pipeline is for fast alerting, not for evidence.)
What we'll detect
# | Activity | Why it matters |
|---|---|---|
1 | Root user sign-in or API use | Root should almost never be used day to day |
2 | IAM persistence changes | New keys, users, or policies are a common attacker foothold |
3 | CloudTrail tampering | Stopping or altering logging is a classic defense-evasion step |
Prerequisites
- An AWS account and permission to create CloudFormation stacks, SNS topics, and EventBridge rules
- An email address you can confirm a subscription from
- AWS CLI configured (optional, for deployment from the terminal)
Step 1: Understand the event patterns
EventBridge rules match JSON events. Here are the three patterns.
Root activity
{ "detail-type": [ "AWS Console Sign In via CloudTrail", "AWS API Call via CloudTrail" ], "detail": { "userIdentity": { "type": ["Root"] } }}IAM persistence changes
{ "source": ["aws.iam"], "detail-type": ["AWS API Call via CloudTrail"], "detail": { "eventSource": ["iam.amazonaws.com"], "eventName": [ "CreateUser", "CreateAccessKey", "CreateLoginProfile", "UpdateLoginProfile", "AttachUserPolicy", "AttachRolePolicy", "PutUserPolicy", "PutRolePolicy", "UpdateAssumeRolePolicy" ] }}CloudTrail tampering
{ "source": ["aws.cloudtrail"], "detail-type": ["AWS API Call via CloudTrail"], "detail": { "eventSource": ["cloudtrail.amazonaws.com"], "eventName": [ "StopLogging", "DeleteTrail", "UpdateTrail", "PutEventSelectors" ] }}Region note: IAM and console sign-in are global services, and their events are recorded in US East (N. Virginia)us-east-1. Deploy this stack there so those rules see those events. For tampering detection on regional trails, deploy the same stack in each region you use.
Step 2: Deploy with CloudFormation
Save this as
alert-pipeline.yaml .AWSTemplateFormatVersion: "2010-09-09"Description: Alerts for root usage, IAM persistence, and CloudTrail tampering
Parameters: AlertEmail: Type: String Description: Email address that receives alerts
Resources: AlertTopic: Type: AWS::SNS::Topic Properties: DisplayName: high-risk-activity-alerts Subscription: - Protocol: email Endpoint: !Ref AlertEmail
AlertTopicPolicy: Type: AWS::SNS::TopicPolicy Properties: Topics: [!Ref AlertTopic] PolicyDocument: Version: "2012-10-17" Statement: - Sid: AllowEventBridgePublish Effect: Allow Principal: Service: events.amazonaws.com Action: sns:Publish Resource: !Ref AlertTopic
RootActivityRule: Type: AWS::Events::Rule Properties: Description: Root user sign-in or API activity EventPattern: detail-type: - AWS Console Sign In via CloudTrail - AWS API Call via CloudTrail detail: userIdentity: type: [Root] Targets: - Id: sns Arn: !Ref AlertTopic
IamPersistenceRule: Type: AWS::Events::Rule Properties: Description: IAM changes commonly used for persistence EventPattern: source: [aws.iam] detail-type: [AWS API Call via CloudTrail] detail: eventSource: [iam.amazonaws.com] eventName: - CreateUser - CreateAccessKey - CreateLoginProfile - UpdateLoginProfile - AttachUserPolicy - AttachRolePolicy - PutUserPolicy - PutRolePolicy - UpdateAssumeRolePolicy Targets: - Id: sns Arn: !Ref AlertTopic
CloudTrailTamperRule: Type: AWS::Events::Rule Properties: Description: Attempts to stop or alter CloudTrail logging EventPattern: source: [aws.cloudtrail] detail-type: [AWS API Call via CloudTrail] detail: eventSource: [cloudtrail.amazonaws.com] eventName: - StopLogging - DeleteTrail - UpdateTrail - PutEventSelectors Targets: - Id: sns Arn: !Ref AlertTopic
Outputs: TopicArn: Value: !Ref AlertTopicDeploy it:
aws cloudformation deploy \ --region us-east-1 \ --stack-name high-risk-activity-alerts \ --template-file alert-pipeline.yaml \ --parameter-overrides AlertEmail=you@example.comThen open the confirmation email from SNS and confirm the subscription. Alerts won't arrive until you do.
Step 3: Test it safely
Pick a low-impact action in a non-production account and watch for the email:
aws iam create-user --user-name alert-test-useraws iam delete-user --user-name alert-test-userThe
CreateUser call should trigger the IAM persistence rule within moments. You can also use the EventBridge console's sandbox to test a pattern against a sample event before relying on it.Step 4: Make alerts readable
Raw SNS notifications from EventBridge contain the full JSON event, which is hard to scan at 3 a.m. Use an input transformer on each rule target to extract the useful fields:
Targets: - Id: sns Arn: !Ref AlertTopic InputTransformer: InputPathsMap: who: "$.detail.userIdentity.arn" what: "$.detail.eventName" ip: "$.detail.sourceIPAddress" region: "$.detail.awsRegion" time: "$.detail.eventTime" InputTemplate: | "High-risk activity: <what> by <who> from <ip> in <region> at <time>"Now each alert is one readable line: the action, the actor, the source IP, the region, and the time.
Reduce noise before it reduces trust
An alert pipeline that cries wolf gets muted. A few habits keep it useful:
- Expect legitimate changes. If your CI/CD role creates IAM roles routinely, exclude it using
anything-butmatching on the principal ARN, rather than disabling the rule.
- Separate severity. Send root usage and CloudTrail tampering to a high-priority destination, and IAM changes to a lower-priority one.
- Review quarterly. Check which rules fired, which were false positives, and tune the patterns.
What this does not cover
Be honest about the limits:
- It detects known risky API calls, not novel behavior. For anomaly and threat detection, add Amazon GuardDuty.
- It alerts, but doesn't respond. You can extend the SNS target with a Lambda function or Systems Manager Automation for containment.
- It doesn't replace centralized logging. Keep an organization trail delivering to a protected S3 bucket with log file validation enabled.
Cleanup
Remove everything when you're done testing:
aws cloudformation delete-stack --region us-east-1 --stack-name high-risk-activity-alertsConclusion
With one template and a few JSON patterns, you now get near-real-time visibility into three of the highest-signal events in an AWS account. From here, a good next step is routing alerts to a team chat channel through AWS Chatbot, and layering GuardDuty on top for behavioral detection.
Have you built something similar, or tuned these patterns differently? Share your approach in the comments.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article