Terraform's Drift Detection by tfaction
Tags: terraform
Published: 2023-06-05 00:28:41

In this article I describe the overview of Terraform's Drift Detection by tfaction.

https://suzuki-shunsuke.github.io/tfaction/docs/feature/drift-detection

![image](https://github.com/suzuki-shunsuke/tfaction-docs/assets/13323303/2e95f528-8c5d-410c-8dec-fe0dabd3e85a)

## What's tfaction?

https://suzuki-shunsuke.github.io/tfaction/docs/

tfaction is a framework for Monorepo to build high level Terraform Workflows by GitHub Actions. You don't have to run terraform apply in your laptop, and don't have to reinvent the wheel for Terraform Workflows anymore.

## What's drift?

In the context of IaC, `drift` means the divergence between the code and infrastructure.
Drift harms not only the reliability of the code but also the productivity.
So you should detect and resolve the drift as soon as possible.

In case of Terraform, the drift causes the unexpected changes of `terraform plan`. Unexpected changes confuse you and let you handle them.

## What's tfaction's Drift Detection?

From tfaction v0.6.0, tfaction supports Drift Detection.
tfaction enables you to detect the drift periodically and manage the drift as GitHub Issues.

This feature is disabled by default. To enable, please see [the document](https://suzuki-shunsuke.github.io/tfaction/docs/feature/drift-detection#set-up).

tfaction creates an Issue per working directory.
tfaction checks if the drift exists at the following timing.

- `apply` workflow
  - `apply` workflow is run when the pull request is merged
  - If the job succeeds, the issue is closed.
  - If the job fails, the issue is reopened.
- `schedule-detect-drifts` workflow
  - `schedule-detect-drifts` is run periodically
  - If `terraform plan` has no change, the issue is closed
  - If the job fails or `terraform plan` has change, the issue is reopened.

tfaction reopens the issue when the drift is detected, and closes the issue when the drift is resolved.
tfaction posts a comment and updates the issue description according to the result of the drift detection.

Example 1. An Issue is closed because `terraform apply` succeeds and the drift is resolved

![image](https://user-images.githubusercontent.com/13323303/232356803-e1c7298f-362c-4f00-96f0-20f2ac8720f7.png)

Example 2. An Issue is opened because `terraform apply` fails

![image](https://user-images.githubusercontent.com/13323303/233077124-4db0f8a5-1f82-4abd-b0b4-fb641fcee85e.png)

Example 3. Drift is checked by `schedule-detect-drifts` periodically

![image](https://user-images.githubusercontent.com/13323303/233079030-67bd01cc-b6bf-425a-bdeb-82447a31904a.png)

![image](https://user-images.githubusercontent.com/13323303/233079963-68765f2e-1efd-4278-b6c3-145eae9ef9c0.png)

Example 4. The latest comment is reflected to the issue description

![image](https://github.com/suzuki-shunsuke/tfaction-docs/assets/13323303/2e95f528-8c5d-410c-8dec-fe0dabd3e85a)

## Good point

If you already use GitHub Issues for your task management, you can add drift handling into your task management naturally.
You don't have to create issues yourself. You can manage issues in GitHub Projects, adjust the priority, and assign someone to issues.

Issue's comments become the history, so you can track when the drift is raised and which pull request caused the drift.
And comments tell you not only the existence of the drift but also the content of the drift.

You can adjust the frequency of the drift detection, and select workfing directories where the dirft detection is enabled.

## Conclusion

In this article I described the overview of Terraform's Drift Detection by tfaction.
About the details, please see the document.

https://suzuki-shunsuke.github.io/tfaction/docs/feature/drift-detection