AWS Builder Center
Weekend Agent Challenge: CloudTrail Geo-Alert Agent

Weekend Agent Challenge: CloudTrail Geo-Alert Agent

An always-on security agent that monitors AWS API calls across an organization using CloudTrail, resolves source IPs to countries with MaxMind GeoLite2, and generates AI-powered threat reports via Amazon Bedrock Nova — all triggered automatically by EventBridge, with no human interaction required.

AWS Solutions Architect

Vision & What the Agent Does

In a multi-account AWS environment, API calls happen constantly — day and night, from automation pipelines, team members, and occasionally from unexpected locations. If someone's credentials are compromised, the first sign is often an API call from a country where your team has no presence.
The CloudTrail Geo-Alert Agent is an always-on security agent that monitors every AWS API call across an organization and autonomously generates AI-powered security reports when activity originates from outside allowed countries. It runs 24/7 without any human interaction — no buttons, no dashboards to check. When something suspicious happens, the agent investigates on its own, assesses the risk using Amazon Bedrock, and delivers a clear, actionable report straight to your inbox.
What triggers it: New CloudTrail log files arriving in an S3 bucket trigger an EventBridge event, which invokes the agent automatically.
What it does on its own: The agent reads the log file, resolves each source IP to a country using MaxMind GeoLite2 (the same database AWS WAF uses internally), accumulates suspicious events in DynamoDB, and when the notification interval elapses, sends all accumulated data to Amazon Bedrock Nova to generate a natural language security assessment.
How it reports back: You receive an email (via SNS) with an AI-generated security report that includes a threat summary, key findings organized by account, a risk assessment (high/medium/low), and recommended immediate actions — all in plain language, ready to act on.
The best part? I wake up in the morning and the report is already in my inbox. No log diving, no dashboard clicking. The agent did the analysis while I was away.
Screenshot of an email containing a security alert report generated by Amazon Bedrock Nova, showing threat summary, key findings by AWS account, risk assessment, and recommended actions for API calls detected from outside allowed countries.




How You Built It

This agent builds on a series of projects I published earlier this year:
  1.  Centralizing CloudTrail Logs Across an AWS Organization  — the foundation that collects all API activity into a single S3 bucket
  1.  Querying Centralized CloudTrail Logs with Amazon Athena  — making those logs searchable with SQL
For this challenge, the key evolution was replacing the static template-based alert with an AI-powered analysis agent using Amazon Bedrock. Instead of a simple table of IPs and countries, the agent now produces an intelligent security assessment.

Development Process

The original system already had the event-driven pipeline working: CloudTrail → S3 → EventBridge → Lambda → DynamoDB → SNS. The notification message was a fixed-format summary — useful but robotic.
The upgrade involved:
  1. Restructuring the notification flow — Instead of building a text template directly, the Lambda now constructs a structured data summary (JSON) with events grouped by account, service, action, country, and IP.
  1. Integrating Amazon Bedrock — The structured data is sent to Amazon Bedrock (Nova Micro model) with a carefully crafted prompt that instructs the model to act as a cloud security analyst. The prompt asks for a threat summary, key findings, risk assessment, and recommended actions.
  1. Adding a fallback mechanism — If Bedrock is unavailable for any reason, the system falls back to the original template format. Security alerts should never be lost due to a downstream service issue.
  1. Updating IAM — Added a scoped  bedrock:InvokeModel  permission targeting only the specific foundation model ARN.

Key Decisions

  • Nova Micro over larger models: The security data is structured and concise. Nova Micro provides fast, cost-effective responses for this type of summarization task — typically responding in under 2 seconds.
  • Prompt engineering for consistency: The prompt explicitly requests plain text (no markdown), a character limit for email readability, and a professional security analyst tone.
  • Graceful degradation: The fallback ensures that a Bedrock outage never prevents a security alert from being delivered.

Challenges

The main challenge was an infrastructure issue: after reorganizing my AWS Organization (migrating accounts to a new org), the EventBridge rule was filtering on the old organization ID in the S3 key prefix. Logs were arriving under  AWSLogs/o-neworgid/...  but the rule was looking for  AWSLogs/o-oldorgid/... . The fix was simply updating the  org_id  variable in Terraform — but diagnosing it required tracing the full event path from S3 delivery to EventBridge pattern matching.



AWS Services Used / Architecture Overview

Architecture

AWS architecture diagram showing the event-driven flow from CloudTrail through S3, EventBridge, Lambda, DynamoDB, Amazon Bedrock, and SNS to deliver AI-generated security reports via email

The flow works as follows:
  1. AWS CloudTrail (Organization Trail) records all management API events across 5 accounts and delivers compressed JSON log files to a centralized S3 bucket every 5-15 minutes.
  1. Amazon S3 with EventBridge notifications enabled emits an "Object Created" event whenever a new log file lands.
  1. Amazon EventBridge matches the event against a rule that filters by bucket name and key prefix (organization ID + monitored account IDs). Only logs from monitored accounts trigger the agent.
  1. AWS Lambda (Python 3.12, arm64/Graviton2) is the agent's brain:
  • Downloads and decompresses the log file from S3
  • Resolves each source IP to a country using an embedded MaxMind GeoLite2 database
  • Filters out internal AWS IPs and allowed countries
  • Stores suspicious events in DynamoDB with a TTL
  • Checks if the notification interval has elapsed
  • If yes: queries DynamoDB for accumulated events, sends them to Bedrock for analysis, and publishes the AI report via SNS
  1. Amazon DynamoDB (on-demand, pay-per-request) serves two purposes:
  • Accumulates alert events between notification intervals (partition key:  EVT )
  • Controls notification timing to prevent email flooding (partition key:  CONTROL )
  • TTL automatically cleans up old events
  1. Amazon Bedrock (Nova Micro model) receives the structured event data and generates a natural language security report with threat assessment and recommendations.
  1. Amazon SNS delivers the final AI-generated report to the subscribed email address.

Infrastructure as Code

The entire infrastructure is managed with Terraform, organized as a reusable module ( infrastructure/ ) called from environment-specific configurations ( environments/log/ ). One file per AWS service, consistent naming with project prefix, and all secrets gitignored.



What You Learned

Amazon Bedrock Integration is Simpler Than Expected

Calling Bedrock from Lambda requires just a few lines of code — create a  bedrock-runtime  client, format the message in the Converse API structure, and parse the response. The hardest part is writing a good prompt, not the integration itself.

Prompt Design Matters for Operational Tools

For a security report delivered via email, the prompt needs to be very specific: no markdown (email clients render it as plain text), character limits (SNS has a 256KB limit, but emails should be scannable), and a consistent structure so the recipient knows exactly where to look. Adding "Keep the report under 2000 characters for email readability" to the prompt made a measurable difference.

Event-Driven Architectures Compose Naturally

The original system was already event-driven (S3 → EventBridge → Lambda → SNS). Adding AI was just inserting one more step in the pipeline — the architecture did not need to change, only the Lambda's notification function. This composability is a core strength of serverless event-driven patterns.

Graceful Degradation is Non-Negotiable for Security Tools

A security alert system cannot have a single point of failure. If Bedrock is down, the alert still needs to go out. The fallback to template-based reporting ensures that the worst case is a less readable email — never a missing alert.

Cost Profile

The entire system runs on serverless, pay-per-use services. For a small organization with 5 accounts, the monthly cost is under $5. The Bedrock calls (Nova Micro) are especially cheap — fractions of a cent per invocation for the small payloads involved.



Link to Repo

The full source code, Terraform infrastructure, and documentation are available on GitHub:



Stay Connected

For more content on AWS security, serverless architectures, and cloud infrastructure:
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