Overview
This module implements five layers of security:- KMS (Key Management Service) - Centralized encryption key management
- CloudTrail - Complete audit log of all AWS account actions
- GuardDuty - Intelligent threat detection and intrusion detection
- Security Hub - Unified security posture and compliance dashboard
- WAF (Web Application Firewall) - HTTP-level attack protection
- Secrets Manager - Secure credential storage
- Cost Anomaly Detection - Alerts on unexpected spending
Resources Created
KMS Key
- Resource:
aws_kms_key.govtech_main - Master encryption key for all sensitive data
- Automatic annual key rotation
- 30-day deletion window for recovery
- Used by: RDS, S3, CloudTrail logs, Secrets Manager
CloudTrail
- Resource:
aws_cloudtrail.govtech_audit - Records all API calls and actions in AWS account
- Multi-region trail capturing global events
- S3 data events enabled (who accessed which files)
- Log file validation for integrity
- Logs encrypted with KMS
- Streamed to CloudWatch Logs for real-time analysis
GuardDuty
- Resource:
aws_guardduty_detector.main - Analyzes VPC Flow Logs, CloudTrail, DNS logs, S3 events
- Kubernetes audit log analysis
- Malware scanning for EC2/EKS volumes
- Detects: compromised instances, data exfiltration, cryptocurrency mining, C2 communication
Security Hub
- Resource:
aws_securityhub_account.main - Aggregates findings from GuardDuty, Inspector, IAM Access Analyzer, Macie
- Compliance checks against:
- CIS AWS Foundations Benchmark v1.4.0
- AWS Foundational Security Best Practices
- Multi-region finding aggregation
WAF Web ACL
- Resource:
aws_wafv2_web_acl.govtech - Filters HTTP/HTTPS traffic before reaching application
- AWS Managed Rules:
- AmazonIpReputationList: Blocks known malicious IPs
- CommonRuleSet: OWASP Top 10 protection (XSS, path traversal, etc.)
- SQLiRuleSet: SQL injection prevention
- KnownBadInputsRuleSet: Log4Shell, SSRF, and other exploit attempts
- Custom rate limiting: 2,000 requests per 5 minutes per IP
Secrets Manager
- Resources:
aws_secretsmanager_secret.db_credentials- Database credentialsaws_secretsmanager_secret.jwt_secret- JWT signing key
- Encrypted with KMS
- 7-day recovery window for deleted secrets
- Access logged in CloudTrail
Cost Anomaly Detection
- Resource:
aws_ce_anomaly_monitor.govtech - Machine learning-based spending analysis
- Alerts on anomalies > $50
- Detects: unauthorized resource creation, misconfigured auto-scaling, crypto mining
CloudWatch Alarms
- Root account usage - Alerts when AWS root account is used
- Unauthorized API calls - 5+ failed API calls in 5 minutes
Variables
Project name for resource naming and tagging
Deployment environment:
dev, staging, or prodAffects:- CloudTrail log retention (365 days for prod, 90 for non-prod)
AWS account ID for KMS key policy.Obtain with:
aws sts get-caller-identity --query Account --output textAWS region for Security Hub standards ARNs
S3 bucket ID for storing CloudTrail and WAF logs.Must be created before this module with appropriate bucket policy allowing CloudTrail writes.
Outputs
KMS key ID (UUID format)
KMS key ARN for use in RDS, S3, and Secrets Manager encryption configurations
GuardDuty detector ID for managing findings and suppressions
WAF Web ACL ARN to associate with Application Load Balancer
CloudTrail trail ARN
ARN of database credentials secret in Secrets Manager
ARN of JWT secret in Secrets Manager
ARN of cost anomaly monitor
Usage Example
CloudTrail Log Analysis
Query Logs in CloudWatch Insights
Find who created a specific resource:S3 Data Events
CloudTrail records who accessed which S3 objects:GuardDuty Response
Common Findings
| Finding Type | Severity | Description | Action |
|---|---|---|---|
| UnauthorizedAccess:EC2/SSHBruteForce | Medium | SSH brute force attempt | Block source IP in security group |
| Trojan:EC2/BlackholeTraffic | High | EC2 communicating with known botnet | Isolate instance, investigate |
| Recon:EC2/PortProbeUnprotectedPort | Low | Port scanning detected | Review security groups |
| Exfiltration:S3/AnomalousBehavior | High | Unusual S3 data download | Review IAM credentials, rotate keys |
| CryptoCurrency:EC2/BitcoinTool | High | Bitcoin mining detected | Terminate instance, investigate compromise |
Automated Response
Create EventBridge rule to respond to findings:Security Hub Dashboard
Compliance Standards
Review compliance posture in Security Hub console:-
CIS AWS Foundations Benchmark v1.4.0
- IAM best practices
- Logging and monitoring
- Networking configuration
-
AWS Foundational Security Best Practices
- S3 bucket permissions
- EC2 security groups
- RDS encryption and backups
Failed Controls
Common findings and remediation:| Control | Issue | Remediation |
|---|---|---|
| IAM.1 | Root account has access keys | Delete root access keys |
| EC2.2 | Security group allows 0.0.0.0/0 on port 22 | Restrict SSH to specific IPs |
| S3.1 | S3 bucket lacks encryption | Enable default encryption |
| RDS.3 | RDS instance not encrypted | Recreate with encryption enabled |
| CloudTrail.1 | CloudTrail not enabled | Enable via security module |
WAF Configuration
Associate with Load Balancer
After ALB creation:WAF Monitoring
View blocked requests in CloudWatch:Custom WAF Rules
Block specific countries (geo-blocking):Secrets Manager Integration
Update Database Credentials
After RDS creation:Access from Kubernetes
Using External Secrets Operator:Cost Anomaly Alerts
Alert Destinations
Update email address in module:Common Anomalies
- NAT Gateway data transfer spike (data exfiltration?)
- EC2 instance type change (unauthorized scaling?)
- S3 request surge (DDoS or scraping?)
- RDS storage rapid growth (application bug?)
Compliance and Auditing
FISMA/FedRAMP Requirements
This module addresses several FISMA controls:- AC-2: CloudTrail logs all account activity
- AU-2: Comprehensive audit logging
- SC-13: FIPS-validated encryption (KMS)
- SI-4: GuardDuty provides intrusion detection
- SC-7: WAF provides boundary protection
Audit Report Generation
Export Security Hub findings:Best Practices
Enable MFA Delete on Logs Bucket
Prevent log deletion even with root access:Regular Security Reviews
- Weekly: Review GuardDuty findings
- Monthly: Security Hub compliance posture
- Quarterly: CloudTrail log analysis for anomalies
- Annually: Penetration testing and architecture review
Incident Response Plan
- Detection: GuardDuty/Security Hub alert
- Containment: Isolate affected resources (security group changes)
- Investigation: Analyze CloudTrail logs, VPC Flow Logs
- Eradication: Rotate credentials, patch vulnerabilities
- Recovery: Restore from known-good backups
- Lessons Learned: Update security controls, document incident