: Terraform + DriftCTL + GitHub Actions 통합 운영
1. 왜 상태 드리프트 감지가 중요한가?
**State Drifting(상태 드리프트)**란, 코드(*.tf)로 관리 중인 클라우드 리소스가
외부 콘솔 수동 수정이나 다른 시스템에 의해 실제 상태가 코드와 어긋나는 현상을 말합니다.
실무에서 발생하는 예시
• 누군가 AWS 콘솔에서 EC2 보안 그룹 수동 변경
• Terraform 외 Ansible, SDK 등으로 동일 리소스를 수정
• 기존 인프라 일부를 수동 삭제
이런 경우, terraform apply가 예상치 못한 삭제/생성을 유발할 수 있어
서비스 장애로 직결될 수 있습니다.
⸻
2. DriftCTL 소개 및 원리
DriftCTL은 terraform.tfstate 파일과 실제 클라우드 리소스 상태를 비교하여
**드리프트(불일치 항목)**를 자동 탐지해주는 오픈소스 도구입니다.
driftctl scan --from tfstate+s3://my-tf-state-bucket/state.tfstate
• Terraform의 상태 파일을 기준으로 AWS 실제 리소스를 스캔
• 추가됨 / 누락됨 / 변경됨 리소스를 구분하여 상세 출력
⸻
3. GitHub Actions 기반 자동화: 매일 드리프트 체크 워크플로우
name: Detect Drift
on:
schedule:
- cron: "0 3 * * *" # 매일 새벽 3시
jobs:
driftctl:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install DriftCTL
run: |
curl -sL https://github.com/snyk/driftctl/releases/latest/download/driftctl_linux_amd64.zip -o driftctl.zip
unzip driftctl.zip
chmod +x driftctl
mv driftctl /usr/local/bin/
- name: Run DriftCTL
run: |
export AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}
export AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}
driftctl scan --from tfstate+s3://my-terraform/state.tfstate --output json > drift_report.json
- name: Archive report
uses: actions/upload-artifact@v2
with:
name: drift-report
path: drift_report.json
• GitHub Secrets에 AWS 자격증명 설정
• 드리프트가 발생하면 Slack/Email/Issue로 자동 알림 가능
⸻
4. Slack 알림 연동 예시 (drift 감지 시만)
- name: Check for Drift
id: drift
run: |
if grep '"changes":' drift_report.json | grep -vq '[]'; then
echo "drift_detected=true" >> $GITHUB_OUTPUT
else
echo "drift_detected=false" >> $GITHUB_OUTPUT
fi
- name: Notify Slack
if: steps.drift.outputs.drift_detected == 'true'
uses: slackapi/slack-github-action@v1.23.0
with:
payload: |
{
"text": "Terraform Drift Detected! :warning:",
"attachments": [
{
"text": "See the report: https://github.com/org/repo/actions/runs/${{ github.run_id }}"
}
]
}
⸻
5. 자동 복구 전략 (옵션)
• 신중해야 함: 드리프트를 무조건 덮어쓰는 방식은 위험
• 그러나 반복적으로 발생하는 리소스(S3 bucket policy, IAM user, etc)에 한해 auto-apply 가능
- name: Terraform Apply (on known drift)
if: steps.drift.outputs.drift_detected == 'true'
run: |
terraform init
terraform apply -auto-approve
※ 단, plan에서 변경 내역을 항상 검토하는 것이 원칙이며, 실시간 Slack 승인 워크플로우 연동 권장
⸻
6. 확장: 조직 전체 멀티 스택 감시
• 모든 팀/서비스별 Terraform 스택의 상태 파일을 S3에 중앙 저장
• 각 스택별로 driftctl scan 실행하여 멀티 환경 점검
for STACK in user-service order-service frontend; do
driftctl scan --from tfstate+s3://tf-states/${STACK}.tfstate --output json > ${STACK}-drift.json
done
• GitHub Actions 매트릭스 전략으로 병렬 실행 가능
⸻
결론
IaC는 코드로 관리되기 때문에 **‘코드 ≠ 실제 상태’**가 되는 순간 취약해집니다.
DriftCTL + GitHub Actions를 통해 상태 일탈을 조기에 감지하고,
Slack 알림 + 승인 기반 복구 자동화로 진정한 IaC 일관성을 유지할 수 있습니다.
이 방식은 AWS뿐 아니라 GCP, Azure, Kubernetes 등으로도 확장 가능하며,
실제 프로덕션에서는 필수로 자리잡아야 할 IaC 운영 인프라 구성의 핵심 자동화 기술입니다.
카테고리 없음