Schedule icon
Script icon
If icon
SlackIncomingWebhook icon
Log icon
Labels icon

AWS CloudTrail Unauthorized API Call Sentinel

Audits AWS CloudTrail logs for unauthorized API access attempts, alerting SecOps teams on Slack when AccessDenied events exceed safety thresholds.

Categories
CloudInfrastructure

This blueprint acts as an automated cloud security sentinel for AWS environments. Scheduled hourly, it interrogates AWS CloudTrail event history via Boto3 to detect spikes in AccessDenied and UnauthorizedOperation API responses across IAM users and roles, alerting SecOps teams on Slack before credential compromise or privilege escalation attempts go unnoticed.

How it works

  1. Recurring Hourly Audit: The hourly_security_audit schedule trigger (io.kestra.plugin.core.trigger.Schedule) initiates the audit at the start of each hour.
  2. CloudTrail Event Lookup: The scan_unauthorized_api_calls task (io.kestra.plugin.scripts.python.Script) queries CloudTrail lookup_events, inspecting management calls within the configured lookback window and filtering for permission denial error codes.
  3. Threat Threshold Gate: The check_unauthorized_activity flowable task (io.kestra.plugin.core.flow.If) evaluates whether total unauthorized events meet or exceed the security threshold.
  4. Slack Incident Alert: When unauthorized API spikes occur, notify_slack_secops (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) dispatches an immediate incident notification with target region and count.
  5. Nominal Confirmation: When no anomalous access violations occur, log_security_nominal logs nominal security posture.
  6. Manifest Export: The export_security_manifest task tags the execution with searchable status labels.

Architecture diagram

flowchart TD
    A[Schedule: Hourly] --> B[scan_unauthorized_api_calls: Interrogate CloudTrail Events]
    B --> C{Unauthorized Events >= Threshold?}
    C -- Yes --> D[notify_slack_secops: Slack Incident Alert]
    C -- No --> E[log_security_nominal: Log Nominal State]
    D --> F[export_security_manifest: Execution Labels]
    E --> F

Use cases

  • Detect Leaked IAM Credentials: Identify compromised developer or CI/CD access keys being probed by external attackers testing available API permissions.
  • Privilege Escalation Discovery: Alert security teams when internal service accounts attempt unauthorized actions on sensitive resources like KMS or Secrets Manager.
  • Compliance and Audit Readiness: Maintain automated audit records proving continuous monitoring of cloud administrative access controls for SOC2 and ISO 27001.

Inputs

Name Type Default Description
aws_region STRING us-east-1 Target AWS region for auditing CloudTrail event history.
lookback_hours INT 1 Number of hours in the lookback window for security event auditing.
max_unauthorized_events_threshold INT 5 Number of AccessDenied or UnauthorizedOperation events before triggering an alert.
slack_channel STRING #secops-security-alerts Slack channel destination for security operations and privilege escalation alerts.

Expected outputs

  • {{ outputs.scan_unauthorized_api_calls.vars.total_unauthorized_events }}: Count of unauthorized API calls detected during the audit window.
  • {{ outputs.scan_unauthorized_api_calls.vars.has_security_incident }}: Boolean flag indicating if events exceeded safety threshold.
  • {{ outputs.scan_unauthorized_api_calls.outputFiles['cloudtrail_unauthorized_report.json'] }}: Structured JSON security report detailing offending IAM users and event sources.

Prerequisites

  • AWS CloudTrail enabled in target region or across AWS Organizations.
  • AWS IAM credentials with cloudtrail:LookupEvents permissions.
  • Slack Incoming Webhook endpoint URL.

Secrets

  • AWS_ACCESS_KEY_ID: AWS IAM access key ID.
  • AWS_SECRET_ACCESS_KEY: AWS IAM secret access key.
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook endpoint URL.

Quick start

  1. Configure AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and SLACK_WEBHOOK_URL in your Kestra namespace secrets.
  2. Import this flow YAML into your Kestra workspace.
  3. Specify your primary AWS region in aws_region.
  4. Click Execute in the UI to perform an immediate security audit.

Common pitfalls and troubleshooting

  • CloudTrail Delivery Delay: CloudTrail events typically appear within 15 minutes of occurrence; a 1-hour lookback window guarantees events are indexed.
  • Service Account Noise: Certain automated tools frequently experience harmless AccessDenied events on read-only probes; calibrate max_unauthorized_events_threshold to prevent alert fatigue.
  • Cross-Account Role Delegation: If auditing multi-account setups via AWS Organizations, ensure the execution role possesses sts:AssumeRole grants across member accounts.

How to extend

  • Add automated IAM user deactivation via Boto3 update_access_key(Status='Inactive') when critical thresholds are breached.
  • Stream security audit manifests directly to Amazon Security Lake or Elasticsearch.
  • Fan-out audits across all organizational AWS regions using Kestra ForEach task.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.