When to run audits
Schedule regular audits
Run security group audits on a regular schedule to prevent accumulation of unused resources:Monthly audits
Monthly audits
Recommended for:
- Production accounts with frequent deployments
- Accounts with multiple teams creating resources
- Environments using Infrastructure as Code that may leave orphaned security groups
Quarterly audits
Quarterly audits
Recommended for:
- Development and staging accounts
- Stable environments with infrequent changes
- Accounts with strict change control processes
Ad-hoc audits
Ad-hoc audits
Run immediately after:
- Major application decommissioning
- Infrastructure migrations (e.g., moving to containers)
- Post-incident cleanup
- Before cost optimization reviews
Optimal timing
Testing safely
Start with non-production accounts
Review the log file
Examine the
{account_id}_sg_log.txt file to understand what the script detected:Verify results manually
Before deleting, manually verify in the AWS Console that identified security groups are truly unused.
Use the dry-run approach
The script has a built-in safety mechanism: it always asks for confirmation before deleting security groups (check_sg_usage.py:352).
- Run the audit and respond “no” to the deletion prompt
- Review the log file thoroughly
- Manually verify the unused security groups
- Run the script again and respond “yes” only after verification
Review logs before deletion
Understanding the log file
The log file ({account_id}_sg_log.txt) contains detailed information about each security group:
Key indicators of active security groups
Key indicators of active security groups
Look for these associations in the log:
Instancia EC2 asociada- Attached to EC2 instancesALB/NLB Asociado- Used by load balancersInstancia RDS Asociada- Attached to databasesECS Servicio Asociado- Used by ECS servicesReferenciado por otros SGs- Referenced in other security group rules- Any other service associations (EKS, ElastiCache, etc.)
What to verify before deletion
Check for recent creation
Newly created security groups may not have resources attached yet. Verify creation date in the AWS Console before deleting.
Look for naming patterns
Security groups with names like “prod-”, “-live”, or “critical-*” deserve extra scrutiny even if they appear unused.
Consider pending deployments
Check with your team if any deployments are planned that might use the security groups.
Backing up security group rules
Export before deletion
Before deleting security groups, export their rules for future reference:Document why security groups were deleted
Keep a log of deletion activities:Minimize false positives
Services not currently checked
The script checks 20+ AWS services, but some services are not yet supported: Mitigation:- Always manually verify before deletion
- Check the AWS Console for any resources using the security group
- Use AWS CLI to search for network interfaces:
Cross-account references
If you use VPC peering or AWS PrivateLink, security groups may be referenced across accounts:The script only checks resources within the current account. Security groups referenced in peering connections or shared services may appear unused.
Compliance and governance
Audit trail
Maintain compliance by keeping audit records:Tag-based policies
Implement tagging standards to prevent accidental deletion:Multi-region strategies
Check all active regions
Security groups are region-specific. Run the audit in every region where you have resources:Regional service availability
Some AWS services checked by the script (like Neptune, DocumentDB, or Amazon MQ) may not be available in all regions. The script will skip unavailable services automatically.
Cost optimization
Why unused security groups matter
While security groups themselves don’t incur direct costs, they:- Increase complexity and management overhead
- Make compliance audits more difficult
- Can lead to security misconfigurations
- Clutter the AWS Console and API responses
Security considerations
IAM permissions
Use least-privilege IAM policies:Avoid default security group deletion
Never delete the default security group for a VPC:AWS does not allow deletion of default security groups. If you try to delete one, you’ll receive a
CannotDelete error.Continuous improvement
Monitor script effectiveness
Track metrics over time:- Number of unused security groups per audit
- Time to complete audits (may indicate throttling)
- False positive rate (manually verified as in-use)
Contribute improvements
If you identify services not currently checked, consider contributing to the project:- Add support for additional AWS services
- Improve error handling
- Add parallel processing for better performance