boto3 icon indicating copy to clipboard operation
boto3 copied to clipboard

Issues with Revoking Security Group Rules

Open mandeepbal opened this issue 10 years ago • 1 comments

Hi,

I'm having trouble revoking all the security group rules using boto3. I'm trying to create a script that will delete all the security group ingress rules for any VPC I specify. However, I can't figure out a way to pull the current security group rules and then pass them into a revoke_security_group_ingress method. My code is below:

ec2_client = session.client('ec2')

clientsecgrps = ec2_client.describe_security_groups(Filters=[ { 'Name': 'vpc-id', 'Values': [vpcid] }])

#response = client.describe_security_groups(Filters=filters)
groups = clientsecgrps['SecurityGroups']
for group in groups:
    print "------------------------------------------------"
    print group['GroupId']
    # If the SecGroup has Inbound rules then execute
    if len(group['IpPermissions']) > 0:
        for permission in group['IpPermissions']:
            print permission
            response = ec2_client.revoke_security_group_ingress(GroupId=group['GroupId'],IpPermissions=permission)
            print response

I keep getting a invalid type error, even though the IpPermissions seem to match up.

botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter IpPermissions, value: {u'IpProtocol': '-1', u'IpRanges': [], u'UserIdGroupPairs': [{u'UserId': '586507063878', u'GroupId': 'sg-635ee507'}], u'PrefixListIds': []}, type: <type 'dict'>, valid types: <type 'list'>, <type 'tuple'>

mandeepbal avatar Dec 19 '15 22:12 mandeepbal

I forgot to put the dict permission in brackets. [] response = ec2_client.revoke_security_group_ingress(GroupId=group['GroupId'],IpPermissions=[permission])

mandeepbal avatar Dec 21 '15 00:12 mandeepbal