Automated Lambda function to isolate compromised EC2 instances and rotate leaked IAM access keys.
- ✅ EC2 Instance Isolation: Creates isolation security group with no inbound/outbound rules
- ✅ Termination Protection: Enables termination protection on compromised instances
- ✅ Forensic Snapshots: Creates EBS volume snapshots for investigation
- ✅ IAM Key Rotation: Deactivates compromised keys and creates new ones
- ✅ Secure Storage: New credentials stored in AWS Secrets Manager
- ✅ Bulk Operations: Handles multiple instances and users in single invocation
- ✅ SNS Notifications: Optional real-time alerts to security team
- ✅ Audit Trail: Comprehensive CloudWatch logging
- ✅ Resource Tagging: All resources tagged for tracking
- ✅ Error Handling: Continues processing even if individual resources fail
- Architecture
- Prerequisites
- Quick Start
- Deployment
- Usage
- Security Features
- Monitoring
- Troubleshooting
- Contributing
- License
┌─────────────────┐
│ EventBridge │ (Optional trigger)
│ GuardDuty │
│ Manual Invoke │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Lambda │
│ Function │
└────────┬────────┘
│
┌────┴────┬──────────┬─────────┐
▼ ▼ ▼ ▼
┌───────┐ ┌───────┐ ┌─────────┐ ┌─────┐
│ EC2 │ │ IAM │ │ Secrets │ │ SNS │
│ │ │ │ │ Manager │ │ │
└───────┘ └───────┘ └─────────┘ └─────┘
- AWS CLI configured with appropriate credentials
- Python 3.12 runtime
- IAM permissions to create Lambda functions and roles
- AWS account with EC2 and IAM access
git clone https://github.com/fardeenxbaig/aws-incident-isolator.git
cd aws-incident-isolator# Create IAM role
aws iam create-role \
--role-name IncidentResponseLambdaRole \
--assume-role-policy-document file://trust-policy.json
# Attach permissions
aws iam put-role-policy \
--role-name IncidentResponseLambdaRole \
--policy-name IncidentResponsePolicy \
--policy-document file://lambda-policy.json# Replace YOUR_ACCOUNT_ID with your AWS account ID
aws lambda create-function \
--function-name SecurityIncidentResponse \
--runtime python3.12 \
--role arn:aws:iam::<YOUR_ACCOUNT_ID>:role/IncidentResponseLambdaRole \
--handler incident_response.lambda_handler \
--timeout 60 \
--zip-file fileb://incident_response.zip \
--region us-east-1# Edit email in script
nano setup-sns.sh
# Run setup
./setup-sns.shSee QUICKSTART.md for detailed deployment instructions.
{
"instance_id": "i-0123456789abcdef0",
"access_key_id": "AKIAIOSFODNN7EXAMPLE",
"iam_user": "compromised-user"
}{
"instance_ids": [
"i-0123456789abcdef0",
"i-0fedcba9876543210"
],
"compromised_users": [
{
"iam_user": "user1",
"access_key_id": "AKIAIOSFODNN7EXAMPLE"
},
{
"iam_user": "user2",
"access_key_id": "AKIAI44QH8DHBEXAMPLE"
}
]
}{
"instance_ids": ["i-xxx"],
"compromised_users": [{"iam_user": "user1", "access_key_id": "AKIA..."}],
"sns_topic_arn": "arn:aws:sns:us-east-1:ACCOUNT:SecurityIncidentAlerts"
}aws lambda invoke \
--function-name SecurityIncidentResponse \
--cli-binary-format raw-in-base64-out \
--payload file://payload.json \
response.jsonEC2 Instance Isolation:
- Creates isolation security group (no inbound/outbound traffic)
- Applies security group to instance
- Enables termination protection
- Creates forensic EBS snapshots
- Tags all resources with incident ID
IAM Access Key Rotation:
- Deactivates compromised key
- Handles 2-key limit (deletes oldest inactive key if needed)
- Creates new access key
- Stores credentials in AWS Secrets Manager (encrypted)
- Returns secret ARN (not the actual secret)
Security Enhancements:
- ✅ Input validation (prevents injection attacks)
- ✅ Structured logging (audit trail without sensitive data)
- ✅ SNS notifications (real-time alerts)
- ✅ Resource tagging (incident tracking)
- ✅ Error handling (no information leakage)
See SECURITY_ENHANCEMENTS.md for complete details.
aws logs tail /aws/lambda/SecurityIncidentResponse --follow# EC2 instances
aws ec2 describe-instances \
--filters "Name=tag:IncidentResponse,Values=true"
# Secrets
aws secretsmanager list-secrets \
--filters Key=tag-key,Values=IncidentResponse
# Snapshots
aws ec2 describe-snapshots \
--filters "Name=tag:IncidentResponse,Values=true"See README.md for detailed troubleshooting guide.
Common Issues:
InvalidGroup.Duplicate: Security group already exists - delete it firstLimitExceeded: User has 2 keys - Lambda auto-handles thisUnauthorizedOperation: Lambda role missing permissions
Contributions welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
MIT License - see LICENSE file for details.
Built for AWS security teams to automate incident response workflows.
- Issues: GitHub Issues
- Documentation: See Quickstart.md
This tool is provided as-is. Test thoroughly in non-production environments before deploying to production. Always follow your organization's security policies and incident response procedures.