command_directives/synchronous_remediation/skills/drift-detector/SKILL.md
Detect infrastructure drift between Terraform state and actual cloud resources. Identifies unmanaged resources, manual changes, and configuration drift. Use when: - User asks to check for infrastructure drift - User wants to find unmanaged cloud resources - User mentions "drift detection" or "Terraform drift" - User asks to compare cloud state to IaC - User wants to audit infrastructure changes
npx skillsauth add snyk/studio-recipes drift-detectorInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Detect, track, and resolve infrastructure drift between Terraform state and actual cloud resources to maintain Infrastructure as Code integrity.
Core Principle: Your cloud should match your code.
Note: This skill uses snyk iac describe CLI command (requires shell execution).
# Basic drift scan against a local Terraform state file
snyk iac describe --from=tfstate://terraform.tfstate
# Output as JSON for further analysis
snyk iac describe --from=tfstate://terraform.tfstate --json > drift-report.json
snyk CLI installed| Provider | Setup | |----------|-------| | AWS | AWS credentials (profile, env vars, or IAM role) | | Azure | Azure CLI login or service principal | | GCP | Application default credentials or service account |
For a full list of supported resource types per provider, see SERVICES.md.
Goal: Configure drift detection environment.
Check for Terraform state:
Local state:
ls terraform.tfstate
Remote state (S3 backend):
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "state/terraform.tfstate"
region = "us-east-1"
}
}
AWS:
aws sts get-caller-identity
Azure:
az account show
GCP:
gcloud auth application-default print-access-token
Goal: Identify differences between IaC and actual cloud state.
snyk iac describe --from=tfstate://terraform.tfstate
For S3 backend:
snyk iac describe --from=tfstate+s3://my-bucket/state.tfstate
For Terraform Cloud:
snyk iac describe \
--from=tfstate+tfcloud://organization/workspace \
--tfc-token=$TFC_TOKEN
To focus on specific AWS services:
snyk iac describe \
--from=tfstate://terraform.tfstate \
--service=aws_s3,aws_ec2,aws_rds
snyk iac describe \
--from=tfstate://terraform.tfstate \
--json > drift-report.json
Goal: Understand and categorize drift.
| Category | Description | Risk Level | |----------|-------------|------------| | Unmanaged | Resources not in Terraform | High - shadow IT | | Changed | Resources modified outside Terraform | Medium - config drift | | Missing | Resources in state but deleted | Low - usually intentional |
## Infrastructure Drift Report
Scan Date: 2024-01-15
Terraform State: s3://my-bucket/prod.tfstate
Cloud Provider: AWS (us-east-1)
### Summary
- Unmanaged Resources: 12 (High)
- Changed Resources: 5 (Medium)
- Missing Resources: 2 (Low)
- Total Drift: 19
### Unmanaged Resources (Not in Terraform)
- aws_s3_bucket | prod-logs-manual | High | Import or delete
- aws_security_group | sg-temp-access | Critical | Review and remove
### Changed Resources (Modified Outside Terraform)
- aws_security_group.web | ingress: [443] → ingress: [443, 22] | High
- aws_rds_instance.main | multi_az: true → multi_az: false | Critical
Prioritize Critical issues first (e.g. SSH opened to 0.0.0.0/0, production HA disabled), then High risk issues (e.g. unmanaged IAM users or security groups). Document the affected resource, the risk, and the intended remediation action for each finding.
Goal: Resolve drift and restore IaC integrity.
For resources that should be in Terraform:
# Generate import block
terraform import aws_s3_bucket.manual_bucket prod-logs-manual
# Or use import block (Terraform 1.5+)
import {
to = aws_s3_bucket.manual_bucket
id = "prod-logs-manual"
}
For resources that shouldn't exist:
# After verification, delete unmanaged resources
aws s3 rb s3://unauthorized-bucket --force
aws ec2 terminate-instances --instance-ids i-temp-server
For resources modified outside Terraform:
# Re-apply Terraform to restore intended state
terraform apply
If the manual change should be kept:
# Update Terraform to match new reality
resource "aws_security_group" "web" {
# Add the new rule
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["10.0.0.0/8"] # Restrict if keeping
}
}
Goal: Prevent future drift.
For expected drift (auto-scaling, etc.):
snyk iac update-exclude-policy \
--exclude-unmanaged \
--exclude-changed
This creates a .snyk policy file:
exclude:
iac-drift:
- aws_autoscaling_group.*
- aws_ecs_service.*:desiredCount
Add drift detection to CI/CD:
# GitHub Actions example
- name: Check for Infrastructure Drift
run: |
snyk iac describe \
--from=tfstate+s3://my-bucket/prod.tfstate \
--json > drift.json
# Fail if unmanaged resources found
if [ $(jq '.summary.total_unmanaged' drift.json) -gt 0 ]; then
echo "Drift detected!"
exit 1
fi
Schedule regular drift audits:
| Frequency | Scope | Purpose | |-----------|-------|---------| | Daily | Critical resources | Security monitoring | | Weekly | All production | Configuration audit | | Monthly | All environments | Comprehensive review |
For detailed worked examples, see EXAMPLES.md. Brief references:
Error: Could not read Terraform state
Solutions:
1. Verify state file path
2. Check S3/backend permissions
3. Ensure terraform init has been run
Error: Authentication failed
Solutions:
1. Verify cloud credentials
2. Check IAM permissions for describe/list
3. Ensure credentials not expired
Warning: Service X not supported
Solutions:
1. Check supported services list
2. Use Terraform plan comparison instead
3. Report to Snyk for feature request
development
Complete security remediation workflow. Scans code for vulnerabilities using Snyk, fixes them, validates the fix, and optionally creates a PR. Supports both single-issue and batch mode for multiple vulnerabilities. Use this skill when: - User asks to fix security vulnerabilities - User mentions "snyk fix", "security fix", or "remediate vulnerabilities" - User wants to fix a specific CVE, Snyk ID, or vulnerability type (XSS, SQL injection, path traversal, etc.) - User wants to upgrade a vulnerable dependency - User asks to "fix all" vulnerabilities or "fix all high/critical" issues (batch mode)
testing
Helps choose secure, healthy open-source packages by evaluating vulnerability status, maintenance health, popularity, community, and security posture. Use this skill when: - Agent needs to import a new dependency - User asks "which package should I use for X?" - User wants to compare packages (A vs B) - User asks "is this package safe?" - User asks for a "secure alternative" to a package - User mentions "dependency health", "package chooser", or "package security"
testing
Helps choose secure, healthy open-source packages by evaluating vulnerability status, maintenance health, popularity, community, and security posture. Use this skill when: - Agent needs to import a new dependency - User asks "which package should I use for X?" - User wants to compare packages (A vs B) - User asks "is this package safe?" - User asks for a "secure alternative" to a package - User mentions "dependency health", "package chooser", or "package security"
development
Proactive security scanning for newly generated or modified code. Intelligently detects changes, runs appropriate scans (SAST, SCA, IaC), filters to only NEW issues, and prevents vulnerabilities at the source. Use this skill when: - Agent generates new code files - Agent modifies existing code - User asks to "scan for security issues" or "check my changes" - Before committing changes - User mentions "secure at inception", "proactive scan", or "security check"