boto3
boto3 copied to clipboard
Issues with Revoking Security Group Rules
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'>
I forgot to put the dict permission in brackets. [] response = ec2_client.revoke_security_group_ingress(GroupId=group['GroupId'],IpPermissions=[permission])