AWS Builder Center
Detecting Infrastructure Drift in AWS with Terraform/OpenTofu

Detecting Infrastructure Drift in AWS with Terraform/OpenTofu

A practical introduction to infrastructure drift in AWS, showing how Terraform and OpenTofu can detect configuration changes and how AWS Config, CloudTrail, EventBridge, and CI/CD can improve visibility and governance.

Series: DevOps & Platform Engineering (1 article)

  1. 1
    Detecting Infrastructure Drift in AWS with Terraform/OpenTofu This article
Infrastructure as Code has changed how we manage cloud environments. Instead of manually creating resources, we describe our desired infrastructure in code, version it, review changes, and apply them through controlled workflows.
However, there is an important problem that Infrastructure as Code does not automatically eliminate:
infrastructure drift.

What is infrastructure drift?

Drift happens when the actual state of your AWS infrastructure becomes different from the state declared in your Infrastructure as Code.
Imagine that Terraform defines an EC2 Security Group allowing traffic only on port 443.
Later, someone manually adds port 22 through the AWS Management Console.
The Terraform code has not changed, but the infrastructure has.
We now have:
Terraform configuration
|
| expected state
v
AWS Security Group
|
| manual modification
v
Actual AWS configuration

The declared state and the real environment are no longer aligned.
This can happen for several reasons:
  • emergency changes made directly through the AWS Console;
  • manual troubleshooting;
  • scripts executed outside the IaC workflow;
  • changes made by another automation system;
  • configuration mistakes;
  • incomplete migration of resources to Infrastructure as Code.
In small environments this may be manageable.
At scale, it becomes a governance problem.

Terraform already helps detect drift

One of the simplest ways to detect drift is:
terraform plan

Terraform refreshes information about managed resources and compares the actual infrastructure with the desired configuration.
If something changed outside Terraform, the plan can expose that difference.
For automation, a particularly useful option is:
terraform plan -detailed-exitcode

This allows a CI/CD pipeline to distinguish between three situations:
0 -> No changes detected
1 -> Terraform error
2 -> Changes detected

This makes it possible to periodically execute Terraform plans without automatically modifying infrastructure.
A simplified workflow could look like this:
Git Repository
|
v
Terraform / OpenTofu
|
v
Scheduled CI Pipeline
|
v
terraform plan
|
+-----+-----+
| |
v v
No changes Drift detected
|
v
Alert / Review

The important principle is that detection and remediation should not necessarily be the same operation.
Automatically applying Terraform every time drift appears can be dangerous. A manual change may have been performed during an incident, for example.
Detecting it first gives the team an opportunity to understand why the environment changed.

Adding AWS-native visibility

Terraform or OpenTofu can detect differences between infrastructure and configuration, while AWS services can help identify changes occurring inside the AWS environment itself.
For example, a monitoring architecture could combine:
  • AWS CloudTrail to identify API operations that modified resources;
  • AWS Config to track resource configuration and configuration history;
  • Amazon EventBridge to react to relevant events;
  • Amazon SNS or another notification mechanism to alert the engineering team;
  • Terraform/OpenTofu plan to verify whether the new state matches the declared infrastructure.
This creates two complementary perspectives:
AWS perspective
"What changed inside the environment?"

+

IaC perspective
"Is the environment still aligned with the code?"

Together, these provide much better visibility than relying only on occasional manual Terraform plans.

Drift detection in CI/CD

A practical approach is to execute a read-only drift detection pipeline periodically.
For example:
terraform init

terraform plan \
-detailed-exitcode \
-out=tfplan

EXIT_CODE=$?

The pipeline can then evaluate the result.
If the exit code is  0 , infrastructure remains aligned.
If it is  2 , the pipeline can generate an alert or create an issue for investigation.
The important distinction is that the pipeline does not immediately execute:
terraform apply

Instead, the detected change becomes something that can be reviewed.
This preserves the Infrastructure as Code workflow while avoiding potentially dangerous automatic remediation.

Treat drift as a signal

Drift should not always be interpreted simply as an error.
It is also a signal about the engineering process.
Frequent drift may indicate:
  • excessive permissions;
  • missing automation;
  • operational procedures that bypass Git;
  • resources that are not completely managed by IaC;
  • emergency workflows that were never incorporated into the normal deployment process.
Therefore, drift detection is not only about Terraform.
It is also about understanding how infrastructure is being operated.

Final thoughts

Infrastructure as Code works best when code remains the source of truth for infrastructure.
Terraform and OpenTofu already provide mechanisms for identifying differences between declared and actual state, while AWS provides powerful observability and governance services around resource changes.
Combining both approaches enables teams to build a stronger drift detection strategy:
observe changes in AWS, compare them against IaC, alert engineers, investigate the cause, and then decide how the environment should be reconciled.
For DevOps and Platform Engineering teams, making infrastructure drift visible is an important step toward predictable, auditable, and reliable cloud environments.

Series: DevOps & Platform Engineering (1 article)

  1. 1
    Detecting Infrastructure Drift in AWS with Terraform/OpenTofu This article
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