この記事では、クラウドリソースのデプロイに使うIaC(Infrastructure as Code)ツールとして、HashiCorp Terraformを取り上げます。Terraformでは、stateファイルとクラウドプロバイダーに適用した内容が異なる状態を、インフラのドリフト、つまりインフラの逸脱と呼びます。
1$ terraform plan23aws_s3_bucket.example: Refreshing state... [id=drift-example-managed-resource]45Note: Objects have changed outside of Terraform67Terraform detected the following changes made outside of Terraform since the last "terraform apply":89 # aws_s3_bucket.example has changed10 ~ resource "aws_s3_bucket" "example" {11 id = "drift-example-managed-resource"12 # (10 unchanged attributes hidden)1314 ~ versioning {15 ~ enabled = true -> false16 # (1 unchanged attribute hidden)17 }18 }1920Unless you have made equivalent changes to your configuration, or ignored the relevant attributes using ignore_changes, the following plan may include actions to undo or respond to these changes.2122────────────────────────────────────────────────────────────23Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:24 ~ update in-place2526Terraform will perform the following actions:2728 # aws_s3_bucket.example will be updated in-place29 ~ resource "aws_s3_bucket" "example" {30 id = "drift-example-managed-resource"31 # (10 unchanged attributes hidden)3233 ~ versioning {34 ~ enabled = false -> true35 # (1 unchanged attribute hidden)36 }37 }3839Plan: 0 to add, 1 to change, 0 to destroy.4041────────────────────────────────────────────────────────────4243Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
1$ driftctl scan --deep23Scanned states (1)4Found resources not covered by IaC:5 aws_s3_bucket:6 - drift-example-un-managed-resource7Found changed resources:8 From tfstate://terraform.tfstate9 - drift-example-managed-resource (aws_s3_bucket.example):10 ~ versioning.0.enabled: true => false11Found 2 resource(s)12 - 50% coverage13 - 1 resource(s) managed by Terraform14 - 1/1 resource(s) out of sync with Terraform state15 - 1 resource(s) not managed by Terraform16 - 0 resource(s) found in a Terraform state but missing on the cloud provider17Scan duration: 17s18Provider version used to scan: 3.74.3. Use --tf-provider-version to use another version.