Using Console
Using CLI
<security-group-id>
with the ID of the security group that has too many rules.
<ip-permissions>
with the IP permissions of the rule that you want to delete. You can get the IP permissions from the output of the previous command.
Using Python
describe_security_groups
method from the boto3
library in python. This method will return a list of all the security groups in your AWS account. You can then iterate through this list to identify the security groups with excessive counts.
revoke_ingress
method to remove unnecessary inbound rules. This method takes in the GroupId
parameter to identify the security group and the IpPermissions
parameter to specify the inbound rules to be removed.
authorize_ingress
method to add the necessary inbound rules. This method takes in the GroupId
parameter to identify the security group and the IpPermissions
parameter to specify the inbound rules to be added.
revoke_ingress
method. We are then adding two necessary inbound rules for SSH and HTTP using the authorize_ingress
method. You can modify the code to suit your specific requirements.