Terraformで管理されていないAWSリソースを検知する

参考 : https://zenn.dev/gosarami/articles/dd938001eac988e44d11

以下は、綺麗にフォーマットされた文章です。

IaC化されていないリソースをdriftctlで検知する

driftctl driftctlは、IaC化されていない野良リソース——driftctlの文脈で言うところのDrift——を検知するツールです。 主な特徴としては、IaCと実際のクラウド上のリソースを比較し、差分を分析、IaCで管理されていない、あるいは不要なリソースを警告、検知対象リソースのフィルタリング、複数の出力フォーマットに対応します。

公式github : https://github.com/snyk/driftctl

公式ドキュメント : https://github.com/snyk/driftctl

インストール方法

dockerで動かす

docker run -t --rm \
  -v ~/.aws:/root/.aws:ro \
  -v $(pwd):/app:ro \
  -v ~/.driftctl:/root/.driftctl \
  -e AWS_PROFILE=non-default-profile \
  snyk/driftctl scan

brewでインストール

brew install driftctl

サンプル

driftctl scan [OPTIONS]
driftctl scan --output json://result.json 
{
  "summary": {
    "total_resources": 3,
    "total_unmanaged": 1,
    "total_missing": 1,
    "total_managed": 1,
    "total_iac_source_count": 1
  },
  "managed": [
    // list of resources found in IaC and in sync with remote
    {
      "id": "driftctl-bucket-test-1",
      "type": "aws_s3_bucket",
      "source": { // Source encapsulates metadata explaining where the resource is coming from within an IaC
        "source": "tfstate://terraform.tfstate", // Represents the Terraform state file
        "namespace": "module", // If you use Terraform module, it will be displayed here
        "internal_name": "my_name" // Represents the internal Terraform resource's name
      }
    }
  ],
  "unmanaged": [
    // list of resources found in remote but not in IaC
    {
      "id": "driftctl-bucket-test-3",
      "type": "aws_s3_bucket"
    }
  ],
  "missing": [
    // list of resources found in IaC but not on remote
    {
      "id": "driftctl-bucket-test-2",
      "type": "aws_s3_bucket"
    },
  ],
  "coverage": 33,
  "provider_name": "AWS",
  "provider_version": "2.18.5",
  // Scan duration in seconds
  "scan_duration": 27,
  "date": "2022-04-08T10:35:00Z"
}

page:https://minegishirei.hatenablog.com/entry/2025/03/01/173843

Dear My Frends.: 個人開発宣伝ラボ - 個人開発者が間違った施策で時間を溶かさないための、心理学と実務知見のナレッジ共有コミュニティ