Files
2nd/10_Wiki/Topics/Coding/DevOps_IaC_Drift_Detection.md
T
2026-05-09 21:08:02 +09:00

4.2 KiB

id, title, category, status, source_trust_level, verification_status, created_at, updated_at, tags, tech_stack, applied_in, aliases
id title category status source_trust_level verification_status created_at updated_at tags tech_stack applied_in aliases
devops-iac-drift-detection IaC Drift Detection — 수동 변경 감지 Coding draft B conceptual 2026-05-09 2026-05-09
devops
iac
terraform
drift
vibe-coding
language applicable_to
Terraform / AWS
DevOps
drift
manual change
terraform plan exit code
driftctl
AWS Config

IaC Drift Detection

Console 에서 누가 손댐 = drift. 다음 apply 가 모르는 사이 덮음. 정기 plan + 알람 으로 검출. driftctl / AWS Config / Terraform Cloud Health Assessment.

📖 핵심 개념

  • Drift: state 와 실제 인프라 차이.
  • 원인: 콘솔 변경, 다른 도구 (kubectl), 만료 / 오토 변경.
  • Prevention: SCP / IAM 으로 console write 차단.
  • Detection: 자동 plan + diff 알림.

💻 코드 패턴

Terraform plan exit code

terraform plan -detailed-exitcode -lock=false -refresh-only
# 0 = no change
# 1 = error
# 2 = drift / changes pending

CI cron (GitHub Actions)

name: drift-check
on:
  schedule: [{ cron: '0 6 * * *' }] # 매일 오전 6시
  workflow_dispatch:
jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: hashicorp/setup-terraform@v3
      - run: terraform init
      - id: plan
        run: terraform plan -detailed-exitcode -refresh-only -no-color | tee plan.txt
        continue-on-error: true
      - if: steps.plan.outcome == 'failure'
        run: |
          curl -X POST $SLACK_WEBHOOK \
            -d "{\"text\":\"Drift detected in prod: $(head -200 plan.txt)\"}"

Terraform Cloud Health Assessment

  • TFC enterprise: 자동 drift detection daily.
  • UI 에서 check + Slack notification.

driftctl

# 모든 리소스 (TF state 안 / 밖 무관) 비교
driftctl scan --from tfstate+s3://tf-state/main.tfstate

# unmanaged 리소스 (TF 외부 생성) 도 발견

AWS Config (cloud native)

resource "aws_config_configuration_recorder" "main" {
  name     = "main"
  role_arn = aws_iam_role.config.arn
  recording_group { all_supported = true }
}

resource "aws_config_config_rule" "no_unencrypted_volumes" {
  name = "encrypted-volumes"
  source { owner = "AWS"; source_identifier = "ENCRYPTED_VOLUMES" }
}

위반 시 EventBridge 로 알림 / 자동 remediation.

Lifecycle ignore_changes (의도적 drift 허용)

resource "aws_autoscaling_group" "app" {
  desired_capacity = 3
  lifecycle {
    ignore_changes = [desired_capacity] # HPA 가 관리, TF 가 안 덮음
  }
}

Prevent destroy (중요한 자원)

resource "aws_db_instance" "main" {
  lifecycle {
    prevent_destroy = true
  }
}

terraform destroy 시 에러.

콘솔 write 차단 (IAM / SCP)

// 모든 사람 read-only, terraform IAM role 만 write
{
  "Effect": "Deny",
  "Action": ["ec2:*", "rds:*", "s3:CreateBucket"],
  "Resource": "*",
  "Condition": {
    "StringNotEquals": { "aws:PrincipalArn": "arn:aws:iam::123:role/terraform" }
  }
}

Drift 발견 → 흡수 vs 회복

  • 흡수: 의도했으면 TF code 에 반영, terraform apply 가 안 바꿈.
  • 회복: 의도 X, terraform apply 가 다시 원래대로.

🤔 의사결정 기준

도구 사용
Open-source driftctl + cron
Terraform Cloud Health Assessment
AWS only + 자동 fix AWS Config + remediation
Multi-cloud driftctl
변경 감사 CloudTrail / GCP Audit
사용자 수 적음 SCP 로 prevent

안티패턴

  • Plan / drift 검사 안 함: 다음 apply 가 무릎 꿇림.
  • 변경 알림 없음: 콘솔 변경 모름.
  • 모든 변경 무조건 회복: HPA / autoscaling 영구 충돌. ignore_changes.
  • TF state 와 실제 차이 무시: refresh.
  • 콘솔 access 누구나: IAM 분리.
  • Production destroy 가능: prevent_destroy.
  • Drift 만 검사 — 외부 생성 리소스 모름: driftctl 가 unmanaged 도 발견.

🤖 LLM 활용 힌트

  • Daily plan + slack on exit 2.
  • driftctl 또는 TFC Health Assessment.
  • ignore_changes 로 의도적 drift 허용.

🔗 관련 문서