How to find unused Amazon EC2 security groups
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
To effectively manage your Amazon EC2 instances, ensuring optimal security configurations is crucial. One aspect of EC2 security management involves identifying unused security groups. Security groups act as virtual firewalls for your instance to control inbound and outbound traffic. Since unused security groups can cause clutter and potential misconfigurations, it's essential to identify and remove them regularly.
Below, we'll explore how to find these unused Amazon EC2 security groups using both the AWS Management Console and AWS Command Line Interface (CLI).
Understanding Security Groups
Security groups in AWS act as a filtering mechanism for incoming and outgoing traffic to EC2 instances. Each EC2 instance can be associated with one or more security groups that define which traffic is allowed to reach the instance and what traffic the instance is allowed to send.
Characteristics of Security Groups
- Instance Association: A security group can be attached to multiple instances.
- Stateful Traffic: Security groups support stateful traffic where return traffic is automatically allowed regardless of outbound/inbound rules.
- Dynamic Updates: Modifications to rules are instantly applied to any instance associated with the security group.
Identifying Unused Security Groups
Unused security groups are those not associated with any EC2 instances, Elastic Network Interfaces (ENIs), or other AWS resources. Identifying and removing them minimizes clutter and potential security risks.
Using AWS Management Console
- Navigate to Security Groups: Log in to your AWS Management Console. Under the 'EC2 Dashboard', select 'Network & Security' -> 'Security Groups'.
- Identify Unused Security Groups:
- Hover over each security group and note the count of attached resources.
- A security group with zero associated instances/ENIs is considered unused.
- Review Before Deletion: Before deleting an unused security group, verify it isn’t needed by other AWS resources or pending deployments.
Using AWS CLI
For a more automated and scalable approach, use the AWS CLI.
- List All Security Groups:
- Check Associated Resources:
- Describe Network Interfaces: Fetch ENIs associated with security groups.
- Match with Instances: Use describe-instances to find security groups attached to instances.
- Filter Unused Groups: Compare the list of security groups with those actually associated with any ENIs or instances. Groups that do not appear in either list are unused.
- Automate Identification: You can automate the process by crafting a script that compares IDs from
describe-security-groupswith names fromdescribe-network-interfacesanddescribe-instances.
Example Script to Identify Unused Security Groups
Summary Table
The table below summarizes the process of identifying unused security groups in AWS EC2:
| Step | Description |
| 1 | Access AWS Management Console or configure AWS CLI. |
| 2 | List all security groups using Console
or describe-security-groups. |
| 3 | Identify all associated security groups using
describe-network-interfaces and
describe-instances. |
| 4 | Compare both lists to find unused groups. |
| 5 | Review unused groups before deletion to avoid accidental removal of required configurations. |
Additional Considerations
Automation with AWS Lambda
You might consider creating AWS Lambda functions to periodically check for unused security groups and notify you via SNS or email when they are found. This can further automate governance and hygiene of cloud resources.
Integration with AWS Trusted Advisor
AWS Trusted Advisor can also help identify idle EC2 instances and unused resources as part of its checks, which might indirectly help you spot associated unused security groups.
Future Resource Expansion
Before deleting any group, anticipate your resource requirements. You might have unused security groups because they were set aside for future use.
Identifying and managing unused security groups is a crucial aspect of cloud governance and resource management in AWS. Keeping security configurations tight and centralized helps maintain a secure and efficient cloud environment.
Related reading
- How to fire EC2 instances and upload/run a startup script on each of them?
- How to fix a drifted AWS CloudFormation stack?
- How to fix 'Access Denied' while deleting empty S3 Elastic Beanstalk?
- How to fix apt-get command not found on AWS EC2?
- How to fix bad certificate error in traefik 2.0?
- How to force 'docker login' command to ignore existing credentials helper?
- How to force https on elastic beanstalk?
- How to force SSL for Kubernetes Ingress on GKE

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.