Detect drift in Terraform using CodeBuild

Detect drift in Terraform using CodeBuild

This project was developed to save time identifying the resources changed via the console (click-ops) which were deployed using Terraform.

AWS Cloud Engineer

Introduction

Have you ever updated Terraform code only to discover that the actual AWS environment no longer matches what Terraform expects to deploy?

This is a common challenge many Cloud and DevOps engineers face.

Infrastructure drift occurs when AWS resources that are meant to be managed by Terraform are modified directly through the AWS Console or CLI instead of through code.

This often happens when someone is in a rush and makes a quick change (like increasing an EBS volume size) without updating the corresponding Terraform configuration.

This becomes a problem later when Terraform plan detects unexpected changes. If the same engineer made the manual modification, resolving the drift is simple. But if another engineer encounters it, they must first investigate who changed what, often digging through the console or CloudTrail logs.

This slows down deployments, introduces uncertainty, and can even create production issues.

To help prevent these situations, I built an automated Terraform drift detection system using AWS services. It detects configuration drift daily and notifies engineers before it becomes a blocker in their workflow.

The Usual Terraform Deployment Workflow

Before diving into the drift detection system, it’s important to understand how Terraform infrastructure is typically deployed in an automated CI/CD pipeline.
Below is an example pipeline that runs the common stages:



In a normal situation, if no changes were made in the Terraform code and no manual modifications were made in AWS, the pipeline should apply no changes. After passing code analysis, the Dev and Prod stages usually proceed automatically because the plans return empty diffs.

This behavior is the foundation of reliable infrastructure deployment.

However, this also creates a hidden weakness: If someone manually changes AWS resources outside Terraform, the pipeline won’t know until the next plan run, which might not happen for days or weeks.

This gap is exactly what the drift detection system aims to solve.

How the drift detection system works




Every day, EventBridge triggers a CodeBuild job that runs Terraform init and plan to detect any changes between the desired state and the real AWS environment. If there are no changes detected, there’s no output, but if there are, then the drift is sent in a message via Slack or Email.

Implementation details

  1. EventBridge — I’ve created an EventBridge schedule which will run daily at 7am. The policy created for the role used by EventBridge follow the principle of least privilege to make sure the role doesn’t have more permissions than required, so we targeted only the specific action and CodeBuild project.
  1. CodeBuild — I’ve created a CodeBuild which uses a buildspec file that install Terraform and runs terraform init and plan. In order to trigger all of the environments, I’ve created a loop, to check all of the environments from the pipeline.
for ENV in dev prd; do
echo "===== Processing environment: $ENV ====="
terraform init -reconfigure -upgrade \
-backend-config="bucket=$BACKEND_BUCKET" \
-backend-config="key=environments/$ENV/terraform.tfstate" \
-backend-config="region=eu-west-2" \
-backend-config="dynamodb_table=$DYNAMODB_TABLE" \
-backend-config="encrypt=true"
terraform plan -var="env=$ENV" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" > plan_output_$ENV.txt
echo "Terraform plan for $ENV completed"
# Check if plan output contains "No changes." and only publish if changes detected
if ! grep -q "No changes. Your infrastructure matches the configuration." plan_output_$ENV.txt; then
aws sns publish \
--topic-arn "$SNS_TOPIC_ARN" \
--subject "Terraform Drift Check ($ENV) - Build $CODEBUILD_BUILD_NUMBER" \
--message "$(cat plan_output_$ENV.txt)"
else
echo "No changes detected for $ENV — skipping SNS notification."
fi
done

3. SNS — If there are any changes, send the changes to an email address or slack channel.

Drift Example:

An example of output when there is no drift:



And an example where there’s drift:



The above is a snippet from the email that I’ve received after the build run. To understand what are the changes we have to think the other way around. Because Terraform want’s to change it from “Jon” →“Ciprian” and from “Thomas” → “Paul”, that means in terraform the values are “Ciprian” and “Paul”.
But because the changes were made only in dev, prod’s output is as follow:



Lessons Learned

Building this drift detection system highlighted a few key insights:
  1. Drift is more common than expected, even small console changes accumulate quickly.
  1. Pipelines alone don’t catch drift, if no one deploys, issues stay hidden.
  1. CodeBuild provides a clean Terraform environment, making drift detection consistent and reliable.
  1. Clear notifications save time, formatted output helps engineers understand the drift immediately.
  1. The architecture is simple and scalable, easy to reuse across accounts and teams.
  1. These lessons reinforced how important proactive drift detection is for healthy Terraform workflows.

Conclusion

Terraform drift can cause unexpected plan results, failed deployments, and slow investigations. By combining EventBridge and CodeBuild, we can automatically detect drift on a schedule and notify engineers before it becomes a problem.

This AWS-native approach is lightweight, scalable, and easy to integrate into existing IaC workflows.
It keeps infrastructure aligned with code and reduces surprises in production.
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