This is a common headache for anyone managing infrastructure as code. You need to know if an IAM role's permissions suddenly changed, a network configuration shifted, or if it's something less impactful. A simple 'drift exists' isn't enough to act quickly or prioritize.
So, here’s a clever fix that helps you understand what's really going on. Instead of just sending a basic email, the solution involves creating a structured event with precise details. First, the process runs `terraform plan` to identify changes and saves the plan. If changes are found, `terraform show -json` converts this plan into a machine-readable JSON format. Since the full JSON can be quite large, a tool called `jq` is used to filter out only the essential bits: the resource `address`, its `type` (like `aws_iam_role`), and the `actions` taken (like `create`, `update`, or `delete`).
This condensed, valuable information is then published as a single structured event to an SNS topic. From there, it intelligently splits into two paths. One copy goes to an SQS queue, which acts as a secure audit log for all changes, ensuring you have a record. The other copy is sent to a Lambda function. This function's job is to instantly classify the change's severity – rating it as HIGH, MEDIUM, or LOW – based on the resource type and actions. Finally, these classifications are stored in CloudWatch logs, giving you clear insights into your infrastructure's integrity. This way, you get actionable intelligence, letting you respond effectively to actual infrastructure shifts without getting overwhelmed by vague alerts.