GitHub Repository: https://github.com/Copubah/aws-log-monitoring-system
This Terraform project creates an automated AWS monitoring and alerting system that detects failed logins and error messages from EC2 instance logs.
EC2 Instances -> CloudWatch Agent -> CloudWatch Logs -> Metric Filters -> CloudWatch Alarms -> SNS -> Email/Lambda
- Centralized Logging: CloudWatch Log Groups for EC2 instance logs
- Pattern Detection: Metric filters for "ERROR" and "Failed login" patterns
- Real-time Alerting: CloudWatch Alarms with SNS email notifications
- Automated Response: Optional Lambda function for incident remediation
- Security: IAM roles with least privilege access
- Cost Management: Log retention policies and resource tagging
├── main.tf # Main Terraform configuration
├── variables.tf # Input variables
├── outputs.tf # Output values
├── terraform.tfvars # Variable values (create from template)
├── modules/
│ ├── monitoring/ # CloudWatch and SNS resources
│ ├── iam/ # IAM roles and policies
│ └── lambda/ # Lambda function for remediation
├── backend.tf # Remote state configuration
└── README.md # This file
- AWS CLI configured with appropriate credentials
- Terraform >= 1.0 installed
- S3 bucket for remote state (update backend.tf)
- DynamoDB table for state locking (update backend.tf)
# Initialize Terraform
terraform init
# Validate configuration
terraform validate
# Format code
terraform fmt -recursive
Create terraform.tfvars
from the template:
cp terraform.tfvars.example terraform.tfvars
Edit terraform.tfvars
with your values:
project_name = "log-monitoring"
environment = "dev"
alert_email = "your-email@example.com"
region = "us-west-2"
# Alarm thresholds
error_threshold = 5
failed_login_threshold = 3
evaluation_periods = 1
period_seconds = 300
# Log retention
log_retention_days = 7
# Tags
tags = {
Project = "LogMonitoring"
Environment = "Development"
Owner = "CloudEngineer"
CostCenter = "Engineering"
}
# Plan deployment
terraform plan
# Apply changes
terraform apply
# Confirm with 'yes' when prompted
- Confirm SNS Subscription: Check your email and confirm the SNS subscription
- Generate Test Logs: SSH to an EC2 instance and generate test logs:
# Generate error logs logger "ERROR: Test error message for monitoring" logger "Failed login attempt from user testuser"
- Monitor CloudWatch: Check CloudWatch Logs and Metrics in AWS Console
- Verify Alerts: You should receive email alerts when thresholds are exceeded
# Destroy all resources
terraform destroy
# Confirm with 'yes' when prompted
AWS_REGION
: AWS region for deploymentAWS_PROFILE
: AWS CLI profile to use
Use workspaces for different environments:
# Create and switch to staging workspace
terraform workspace new staging
terraform workspace select staging
# Deploy to staging
terraform apply -var-file="staging.tfvars"
The system creates CloudWatch dashboards for monitoring:
- Log ingestion rates
- Alarm states
- Error patterns over time
- Log retention policies automatically delete old logs
- Resource tags enable cost allocation tracking
- Use CloudWatch Insights for efficient log querying
- IAM roles follow least privilege principle
- CloudWatch Logs encrypted at rest
- SNS topics use server-side encryption
- Lambda functions have minimal permissions
-
SNS Subscription Not Confirmed
- Check email spam folder
- Verify email address in variables
-
CloudWatch Agent Not Sending Logs
- Ensure EC2 instances have proper IAM role
- Check CloudWatch Agent configuration
- Verify network connectivity
-
Alarms Not Triggering
- Check metric filter patterns
- Verify alarm thresholds
- Review CloudWatch Logs for data
# Check Terraform state
terraform show
# List resources
terraform state list
# Import existing resources
terraform import aws_cloudwatch_log_group.example /aws/ec2/logs
# Refresh state
terraform refresh
┌─────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ EC2 │ │ CloudWatch │ │ CloudWatch │
│ Instances │───▶│ Logs │───▶│ Metric Filters │
│ │ │ │ │ │
└─────────────┘ └──────────────────┘ └─────────────────┘
│
┌─────────────┐ ┌──────────────────┐ │
│ Email │◀───│ SNS │◀─────────────┘
│ Alerts │ │ Topic │
└─────────────┘ └──────────────────┘
│
▼
┌──────────────────┐
│ Lambda │
│ (Optional) │
│ Remediation │
└──────────────────┘
- Follow Terraform best practices
- Update documentation for any changes
- Test in development environment first
- Use consistent naming conventions
- Add appropriate tags to all resources
This project is for educational and portfolio purposes.