Python – Access value boto3 dictionary response

Access value boto3 dictionary response… here is a solution to the problem.

Access value boto3 dictionary response

I’m getting an error when trying to get a value from the data returned by boto3. I was able to print the entire response (see below) but couldn’t figure out what I needed to do to make the NetworkInterfaceId not respond.

I ran it in Python 2.7.5 because this is the instance that needs to run it by default. I’m new to python, so hopefully I’m missing something simple, thanks for your help!

Error

TypeError: list indices must be integers, not str

Code

#!/usr/bin/python
import boto3

ec2 = boto3.client('ec2')
response = ec2.describe_route_tables(
              RouteTableIds=[
                  "rtb-4a1efc23",
              ],
              Filters=[
                  {
                      'Name': 'route.destination-cidr-block',
                      'Values': [
                          "172.29.0.0/16",
                      ]
                  },
              ]
          )

#print(response)
print(response["RouteTables"][0]["Routes"]["NetworkInterfaceId"])

Response

 {'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': 'a8e7ba60-7599-450a-a708-8d90e429d59e', 'HTTPHeaders': {'transfer-encoding': 'chunked' , 'vary': 'Accept-Encoding', 'server': 'AmazonEC2', 'content-type': 'text/xml; charset=UTF-8', 'date': 'Wed, 08 Feb 2017 11:51:47 GMT'}}, u'RouteTables': [{u'Associations': [{u'SubnetId': 'subnet-d7040aaf', u'RouteTableAssociationId': 'rtbassoc-867a94ef', u'Main': False, u'RouteTableId': 'rtb-4a1efc23'}, {u'SubnetId': 'subnet-e0fcd3aa', u'RouteTableAssociationId': 'rtbassoc-9f7a94f6', u'Main': False, u'RouteTableId': ' rtb-4a1efc23'}], u'RouteTableId': 'rtb-4a1efc23', u'VpcId': 'vpc-0d00e264', u'PropagatingVgws': [{u'GatewayId': 'vgw-fcf479cc'}], u'Tags': [{u'Value': 'pub', u'Key': 'Name'}], u'Routes': [{u'GatewayId': 'local', u'DestinationCidrBlock': '172.28.0.0/16', u'State': 'active', u'Origin': 'CreateRouteTable'}, {u'Origin': 'CreateRoute', u'DestinationCidrBlock': '172.29.0.0/16', u'InstanceId': 'i-0b84e502d9dc49443', u'NetworkInterfaceId': 'eni-08f55373', u'State': 'active', u'InstanceOwnerId': '444456106883'}, {u'Origin': 'CreateRoute', u'DestinationCidrBlock': '172.31.0.0/16', u'InstanceId': 'i-0b84e502d9dc49443', u'NetworkInterfaceId': 'eni-08f55373', u'State': 'active', u'InstanceOwnerId': '444456106883'}, {u'GatewayId': 'igw-7b03e012', u'DestinationCidrBlock': '0.0.0.0/0', u'State': 'active', u'Origin': 'CreateRoute'}, {u'GatewayId': ' vgw-fcf479cc', u'DestinationCidrBlock': '10.114.112.192/27', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock ': '10.114.210.160/27', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.172.32/27', u'State': ' active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.172.96/27', u'State': 'active', u'Origin': ' EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.114.105.128/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, { u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.115.80.0/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.115.131.0/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': ' 10.138.17.128/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.83.64/26', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.138.180.128/26', u'State': 'active', u'Origin': ' EnableVgwRoutePropagation'}, {u'GatewayId': 'vgw-fcf479cc', u'DestinationCidrBlock': '10.192.0.0/16', u'State': 'active', u'Origin': 'EnableVgwRoutePropagation'}]}]} 

Solution

From documentation, you can see that Routes is a list. If you want to get the NetworkInterfaceId, you should loop through the routes.

for route in response["RouteTables"][0]['Routes']:
    if 'NetworkInterfaceId' in route:
        print route['NetworkInterfaceId']

Note that the NetworkInterfaceId may or may not appear in the response. I know this from the reply you pasted here.

Related Problems and Solutions