Your Infrastructure as Code (IaC) can drift from reality in hours. Drift means the state you defined in Terraform, CloudFormation, or Pulumi no longer matches what runs. That gap is a risk: misconfigurations, security holes, and broken deployments hide there. Detecting it early matters.
IaC Drift Detection is straightforward in theory. Compare provisioned infrastructure to the source of truth in code. In practice, it needs precision and automation. AWS CloudTrail logs every change: new resources, updated properties, deletions. Querying CloudTrail for configuration changes gives you a real-time feed of potential drift. You can match these changes against your IaC definitions to confirm drift, or dismiss intentional updates.
The core pattern is: gather CloudTrail events → filter for resource change actions → map to resource IDs in your IaC → raise alerts when a mismatch appears. SQL queries in Amazon Athena or direct CloudTrail LookupEvents API calls can extract the actions you care about. Example: UpdateSecurityGroupRule, CreateBucket, ModifyDBInstance. Run them on a schedule, pipe results into a comparison job, store history.