I am new to boto3 and trying to access some items that are in an object:
acl = ec2_client.describe_network_acls( Filters=[{'Name':'tag:demo','Values': ['true']}] )
This returns the correctly tagged ACL from AWS and stores the object in "acl". Now I would like to get some items out of the object. I need to be able to find the inbound and outbound rules numbered 100 and 105 from the data set below:
{
u 'NetworkAcls': [{
u 'Associations': [],
u 'NetworkAclId': 'acl-xxxxxxxx',
u 'VpcId': 'vpc-xxxxxxx',
u 'Tags': [{
u 'Value': 'test-demo-dev',
u 'Key': 'Name'
}, {
u 'Value': 'dev',
u 'Key': 'env'
}, {
u 'Value': 'true',
u 'Key': 'demo'
}],
u 'Entries': [{
u 'RuleNumber': 100,
u 'Protocol': '6',
u 'PortRange': {
u 'To': 443,
u 'From': 443
},
u 'Egress': True,
u 'RuleAction': 'allow',
u 'CidrBlock': '34.x.x.x/32'
}, {
u 'RuleNumber': 105,
u 'Protocol': '6',
u 'PortRange': {
u 'To': 443,
u 'From': 443
},
u 'Egress': True,
u 'RuleAction': 'allow',
u 'CidrBlock': '54.x.x.x/32'
}, {
u 'RuleNumber': 110,
u 'Protocol': '6',
u 'PortRange': {
u 'To': 65535,
u 'From': 1024
},
u 'Egress': True,
u 'RuleAction': 'allow',
u 'CidrBlock': '10.x.x.x/x'
}, {
u 'RuleNumber': 115,
u 'Protocol': '6',
u 'PortRange': {
u 'To': 65535,
u 'From': 1024
},
u 'Egress': True,
u 'RuleAction': 'allow',
u 'CidrBlock': '10.x.x.x/x'
}, {
u 'RuleNumber': 120,
u 'Protocol': '6',
u 'PortRange': {
u 'To': 65535,
u 'From': 1024
},
u 'Egress': True,
u 'RuleAction': 'allow',
u 'CidrBlock': '10.x.x.x/x'
}, {
u 'RuleNumber': 32767,
u 'Protocol': '-1',
u 'Egress': True,
u 'CidrBlock': '0.0.0.0/0',
u 'RuleAction': 'deny'
}, {
u 'RuleNumber': 100,
u 'Protocol': '6',
u 'PortRange': {
u 'To': 65535,
u 'From': 1024
},
u 'Egress': False,
u 'RuleAction': 'allow',
u 'CidrBlock': '34.x.x.x/32'
}, {
u 'RuleNumber': 105,
u 'Protocol': '6',
u 'PortRange': {
u 'To': 65535,
u 'From': 1024
},
u 'Egress': False,
u 'RuleAction': 'allow',
u 'CidrBlock': '54.x.x.x/32'
}, {
u 'RuleNumber': 110,
u 'Protocol': '6',
u 'PortRange': {
u 'To': 443,
u 'From': 443
},
u 'Egress': False,
u 'RuleAction': 'allow',
u 'CidrBlock': '10.x.x.x/x'
}, {
u 'RuleNumber': 115,
u 'Protocol': '6',
u 'PortRange': {
u 'To': 443,
u 'From': 443
},
u 'Egress': False,
u 'RuleAction': 'allow',
u 'CidrBlock': '10.x.x.x/x'
}, {
u 'RuleNumber': 120,
u 'Protocol': '6',
u 'PortRange': {
u 'To': 443,
u 'From': 443
},
u 'Egress': False,
u 'RuleAction': 'allow',
u 'CidrBlock': '10.x.x.x/x'
}, {
u 'RuleNumber': 32767,
u 'Protocol': '-1',
u 'Egress': False,
u 'CidrBlock': '0.0.0.0/0',
u 'RuleAction': 'deny'
}],
u 'IsDefault': False
}], 'ResponseMetadata': {
'RetryAttempts': 0,
'HTTPStatusCode': 200,
'RequestId': '37258db0-6b9e-4a20-96f3-dc3bc5214b52',
'HTTPHeaders': {
'transfer-encoding': 'chunked',
'vary': 'Accept-Encoding',
'server': 'AmazonEC2',
'content-type': 'text/xml;charset=UTF-8',
'date': 'Tue, 20 Jun 2017 11:22:47 GMT'
}
}
}
In particular I would like to be able to find out the 'CidrBlock' value for inbound and outbound rules 100 and 105.
Can anyone help? I am sure once I see it working for the first time I can go on and apply to other scenarios but just can't seem to get my head around it for the first time.
I guess I need to loop over parts of the object (Entries) and if 'RuleNumber' 100 or 105 and i need to store ingress and egress separately but where to start.....
The data you have mentioned contains a unicode string keys and you have accidently added an extra space while asking question .
For eg -
InCorrect - u 'NetworkAcls'
Correct - u'NetworkAcls'
For getting CIDR Block of rules 100 and 105 from the data you mentioned above, you can get it like this -
NetworkAcls = acl['NetworkAcls']
for netacl in NetworkAcls:
entries = netacl['Entries']
for entry in entries:
if entry.get('RuleNumber') == 100 or entry.get('RuleNumber') == 105 :
print(entry.get('CidrBlock' ,None))
Output
34.x.x.x/32
54.x.x.x/32
34.x.x.x/32
54.x.x.x/32
Is that what you are looking for ?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.