Policy-As-Code with CloudTrail Query Runbooks
Policy-As-Code turns rules into executable checks. Instead of a static document, you have live, testable code. With AWS CloudTrail, every API call is recorded. By running structured queries against these logs, you can enforce policies automatically, detect violations in seconds, and automate remediation.
A CloudTrail query runbook is a repeatable workflow that parses event history and checks it against your codified rules. You can run it on demand, on a schedule, or triggered by an event. For example, you can write a query to find any RunInstances call that launches an instance without encryption. The runbook flags it, blocks it, or alerts the right team. The same approach applies to IAM changes, security group modifications, or unexpected deletions.
Building this system means combining three parts:
- Policy-As-Code – rules and guardrails expressed in a DSL or configuration format.
- CloudTrail Query – targeted searches using AWS CloudTrail Lake or Athena to extract specific events.
- Runbooks – scripted, automated steps for investigating and responding to what those queries find.
Integrating them creates a closed loop:
- CloudTrail logs every change.
- Queries extract events against the live policy set.
- Runbooks act on the results without human delay.
This design reduces manual review, increases coverage, and eliminates ambiguity. Policies are explicit. Violations are measurable. The gap between detection and action compresses to near zero.
Version-control your policies. Test queries before production. Keep runbooks idempotent and clear. Treat the whole pipeline as code so it can be deployed, rolled back, and audited.
The outcome is a system of truth where rules, data, and action align. CloudTrail query runbooks give Policy-As-Code teeth.
See how to build and run Policy-As-Code CloudTrail query runbooks in minutes with hoop.dev — and watch it work live.