The interactive prompt
When the audit identifies unused security groups, you’ll see:- No attached resources (EC2 instances, load balancers, databases, etc.)
- No references from other security groups
Confirmation process
The tool accepts multiple variations for confirmation:Accepting the deletion
You can type any of the following (case-insensitive):sí(Spanish for “yes”)si(without accent)s(short form)yes(English)y(English short form)
Declining the deletion
Any other input (or typingno, n, no, etc.) will cancel the operation:
The tool defaults to NOT deleting if your input doesn’t match the acceptance keywords. This is a safety feature to prevent accidental deletions.
What happens when you delete
When you confirm deletion by typing an accepted keyword:Success confirmation
If the deletion succeeds, you’ll see:Each successful deletion is confirmed individually.
Error handling during deletion
Deletion can fail for several reasons. The tool catches and displays errors:Common deletion errors
Dependency violation error
Dependency violation error
Error message:Cause: Between the audit scan and the deletion attempt, a resource started using this security group, or another security group now references it.Resolution: Re-run the audit to get updated results.
Permission denied error
Permission denied error
Error message:Cause: Your AWS credentials lack the
ec2:DeleteSecurityGroup permission.Resolution: Update your IAM policy to include deletion permissions.Cannot delete default security group
Cannot delete default security group
Error message:Cause: AWS does not allow deletion of default VPC security groups.Resolution: This is expected behavior. Default security groups cannot and should not be deleted.
The audit should not flag default security groups as unused if they’re properly configured, but this error can occur in edge cases.
Security group not found
Security group not found
Error message:Cause: The security group was already deleted (possibly by another user or process) between the audit and deletion.Resolution: No action needed. The security group is already gone.
Partial deletion handling
If some deletions succeed and others fail:The deletion is irreversible
Best practices for safe deletion
Review the log file first
Before accepting the deletion prompt, open the Ensure the description and ID match security groups you’re comfortable deleting.
{account_id}_sg_log.txt file and verify each unused security group:Document before deletion
For compliance or rollback purposes, export security group rules before deletion:
Start with a test run
On your first audit, decline the deletion (
no) to review results without making changes. Run the audit again later when you’re confident in the findings.Example deletion session
Here’s a complete example of the deletion workflow:What if I accidentally delete a needed security group?
If you delete a security group that was actually needed:- Immediate impact: Resources that were using it may lose network connectivity or fail health checks
- Recovery: You must manually recreate the security group with the same rules
- Mitigation: Keep backup exports of your security group configurations
Running without interactive deletion
If you want to audit without the deletion prompt (for reporting only):- Run the audit normally:
python check_sg_usage.py - When prompted, type
noor press Ctrl+C to exit - Review the log file:
cat {account_id}_sg_log.txt - Manually delete security groups through the AWS Console or CLI if needed
Next steps
After deleting unused security groups:- Save the log file for audit records
- Schedule regular audits (monthly or quarterly) to catch newly unused security groups
- Review your security group tagging strategy to better track ownership and purpose