This document describes the automated infrastructure validation system for Brain-Storm, including Terraform validation, security policy checking, infrastructure testing, and versioning.
The automated infrastructure validation system provides:
- Terraform Validation: Syntax and configuration validation
- Security Policy Checking: OPA-based policy enforcement
- Infrastructure Testing: Health checks and functionality tests
- Infrastructure Versioning: Version tracking and change history
- CI/CD Integration: Automated validation on every infrastructure change
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β GitHub Actions Workflow β
β (On Terraform file changes) β
ββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
ββββββββββββββ΄βββββββββββββ
β β
βΌ βΌ
ββββββββββββββββββββ ββββββββββββββββββββ
β Terraform Format β β Terraform Lint β
β & Validation β β (TFLint) β
ββββββββββ¬ββββββββββ ββββββββββ¬ββββββββββ
β β
βββββββββββββ¬ββββββββββββ
β
βΌ
ββββββββββββββββββββββββββ
β Security Scanning β
β - Checkov β
β - OPA Policies β
ββββββββββ¬ββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββ
β Terraform Plan β
β Analysis β
ββββββββββ¬ββββββββββββββββ
β
ββββββββββ΄βββββββββ¬βββββββββββββββ
βΌ βΌ βΌ
ββββββββββ ββββββββββββ ββββββββββ
β Approveβ β Reject β β Review β
β Deploy β β Changes β β Manual β
ββββββββββ ββββββββββββ ββββββββββ
Purpose: Ensures consistent code formatting
Command:
terraform fmt -check -recursiveWhat it checks:
- Consistent indentation (2 spaces)
- Proper bracket alignment
- Consistent naming conventions
Failure handling: Blocks deployment
Purpose: Validates Terraform configuration syntax and structure
Command:
terraform validateWhat it checks:
- Valid HCL syntax
- Required provider configuration
- Variable and output definitions
- Module references
Failure handling: Blocks deployment
Purpose: Checks for best practices and potential issues
Configuration: scripts/.tflint.hcl
What it checks:
- Naming conventions
- Deprecated syntax
- Module pinning
- AWS-specific best practices
- Security group rules
- Encryption settings
Failure handling: Warns but allows deployment
Purpose: Security and compliance scanning
What it checks:
- Encryption at rest and in transit
- Public access restrictions
- Backup and recovery settings
- Logging and monitoring
- IAM policies
- Network security
Failure handling: Warns but allows deployment
Purpose: Custom policy enforcement
File: infra/terraform/policies/terraform.rego
Policies:
- Deny unrestricted security group access
- Require RDS encryption
- Require S3 versioning
- Require backup retention
- Require Multi-AZ for high availability
- Require proper tagging
Failure handling: Blocks deployment
Purpose: Analyzes infrastructure changes
What it checks:
- Resource creation/modification/deletion
- Cost estimation
- Dependency analysis
- Breaking changes
Output: Detailed plan report
# Run all validation checks
./scripts/validate-infrastructure.sh
# Run specific checks
terraform fmt -check -recursive
terraform validate
tflint
checkov -d infra/terraform --framework terraformValidation runs automatically via GitHub Actions:
Trigger: Push to main or PR with changes to infra/terraform/
Steps:
- Terraform format check
- Terraform validation
- TFLint analysis
- Checkov security scan
- OPA policy validation
- Terraform plan generation
- Cost estimation
- PR comment with results
- Health endpoint (
/health) - Readiness probe (
/ready) - Liveness probe (
/live)
- PostgreSQL connection
- Migration status
- Query performance
- Redis connection
- Memory usage
- Key operations
- Course listing
- Health status
- Documentation
- X-Content-Type-Options
- X-Frame-Options
- Content-Security-Policy
- API response time
- Database query performance
- Throughput metrics
- RDS instance status
- ElastiCache status
- ECS services running
- Automated backups enabled
- Recent backup existence
- Backup retention policy
- CloudWatch logs enabled
- CloudWatch alarms configured
- Log retention settings
- Certificate validity
- TLS version
- Cipher strength
# Run all infrastructure tests
./scripts/test-infrastructure.sh
# Run with custom environment
ENVIRONMENT=production API_URL=https://api.example.com ./scripts/test-infrastructure.sh
# Run with custom database
DB_HOST=prod-db.example.com DB_PORT=5432 ./scripts/test-infrastructure.shπ§ͺ Starting Infrastructure Testing
==================================
1οΈβ£ API Health Checks
-------------------
Testing: API Health Endpoint... β
PASS
Testing: API Readiness... β
PASS
Testing: API Liveness... β
PASS
2οΈβ£ Database Connectivity
------------------------
Testing: PostgreSQL Connection... β
PASS
Testing: Database Migrations... β
PASS
...
π Test Summary
===============
Passed: 28
Failed: 0
Total: 28
Success Rate: 100%
Tracks infrastructure versions and changes over time for:
- Audit trail
- Rollback capability
- Change tracking
- Compliance reporting
Tracked Components:
- Terraform version
- AWS provider version
- Module versions
- Git commit hash
- Git branch
- Deployment timestamp
# Create version snapshot
./scripts/version-infrastructure.sh
# View version history
cat infra/terraform/.versions/VERSIONS.md
# View specific snapshot
cat infra/terraform/.versions/versions-20260601-103000.json | jq '.'{
"timestamp": "2026-06-01T10:30:00Z",
"git_commit": "abc1234def5678",
"git_branch": "main",
"terraform_version": "v1.7.0",
"aws_provider_version": "~> 5.0",
"environment": "production",
"region": "us-east-1"
}File: .github/workflows/terraform.yml
Triggers:
- Push to
mainwith Terraform changes - Pull request with Terraform changes
Steps:
- Checkout code
- Configure AWS credentials
- Setup Terraform
- Format check
- Terraform init
- Terraform validate
- TFLint analysis
- Checkov scanning
- OPA policy validation
- Terraform plan
- Cost estimation
- PR comment with results
- Terraform apply (on main push only)
PR Created
β
Validation Runs
ββ Format Check
ββ Terraform Validate
ββ TFLint
ββ Checkov
ββ OPA Policies
β
Results Commented on PR
β
Manual Review & Approval
β
Merge to Main
β
Terraform Apply
β
Infrastructure Tests
β
Version Snapshot
β
Deployment Complete
deny[msg] {
resource := input.resource_changes[_]
resource.type == "aws_security_group"
rule := resource.change.after.ingress[_]
rule.from_port == 0
rule.to_port == 65535
rule.cidr_blocks[_] == "0.0.0.0/0"
msg := sprintf("Security group %s allows unrestricted access", [resource.address])
}deny[msg] {
resource := input.resource_changes[_]
resource.type == "aws_rds_cluster"
resource.change.after.storage_encrypted == false
msg := sprintf("RDS cluster %s must have encryption enabled", [resource.address])
}deny[msg] {
resource := input.resource_changes[_]
resource.type in ["aws_rds_cluster", "aws_s3_bucket"]
not resource.change.after.tags.Environment
msg := sprintf("Resource %s must have Environment tag", [resource.address])
}Problem: Format check fails
Solution:
# Auto-fix formatting
terraform fmt -recursive infra/terraformProblem: TFLint reports issues
Solution:
# View detailed TFLint output
tflint -f json infra/terraform | jq '.'
# Disable specific rule
# Add to .tflint.hcl:
# rule "aws_instance_default_security_group" {
# enabled = false
# }Problem: OPA policies block deployment
Solution:
- Review policy violation message
- Fix infrastructure code
- Re-run validation
- If policy is incorrect, update
terraform.rego
Problem: Checkov reports security issues
Solution:
# View detailed Checkov output
checkov -d infra/terraform --framework terraform --output cli
# Skip specific check (if acceptable)
# Add to Terraform resource:
# checkov:skip=CKV_AWS_123:Reason for skipping- Always validate locally before pushing
- Review Terraform plans carefully
- Keep policies updated with security requirements
- Test infrastructure changes in staging first
- Document policy exceptions with justification
- Monitor validation results in CI/CD
- Update baselines when intentional changes are made
- Maintain version history for audit trail