[python]Finding your internet gateway id in aws

On previous post, I have this function:

def find_internet_gateway_id(client, vpc_id):
    igw_id = ""
    igw_responses = client.describe_internet_gateways(Filters=filter)
    for i in igw_responses['InternetGateways']:
        for j in i['Attachments']:
            if vpc_id in j['VpcId']:
                igw_id = i['InternetGatewayId']
    return igw_id

This function looks for the internet gateway id associated with your vpc id.

Json response from describe_internet_gateways
The below response is from the describe_internet_gateways method in boto3, here’s the code snippet:

igw_responses = ec2_client.describe_internet_gateways(Filters=filter)
pprint(igw_responses)

The below is a dictionary of two main keys – InternetGateways and ResponseMetadata.

{'InternetGateways': [{'Attachments': [{'State': 'available',
                                        'VpcId': 'vpc-0d5a397499f3bc8b8'}],
                       'InternetGatewayId': 'igw-028b0d9cc6fda54fa',
                       'OwnerId': '203367053771',
                       'Tags': [{'Key': 'Name', 'Value': 'cyruslab_igw_2'}]},
                      {'Attachments': [{'State': 'available',
                                        'VpcId': 'vpc-00b16be35cdd83960'}],
                       'InternetGatewayId': 'igw-0d05c62094670bdb4',
                       'OwnerId': '203367053771',
                       'Tags': [{'Key': 'Name', 'Value': 'cyruslab_igw_3'}]},
                      {'Attachments': [{'State': 'available',
                                        'VpcId': 'vpc-072f78e3f64873a46'}],
                       'InternetGatewayId': 'igw-0fd6b4aea334c6555',
                       'OwnerId': '203367053771',
                       'Tags': [{'Key': 'Name', 'Value': 'cyruslab_igw_1'}]}],
 'ResponseMetadata': {'HTTPHeaders': {'content-length': '1863',
                                      'content-type': 'text/xml;charset=UTF-8',
                                      'date': 'Mon, 06 May 2019 10:17:59 GMT',
                                      'server': 'AmazonEC2'},
                      'HTTPStatusCode': 200,
                      'RequestId': '394e330b-0c24-4d8f-bcb5-d46d121d9578',
                      'RetryAttempts': 0}}

Now I am only interested in the InternetGateways key, hence by using pprint(igw_responses['InternetGateways'])
I got this:

[{'Attachments': [{'State': 'available', 'VpcId': 'vpc-0d5a397499f3bc8b8'}],
  'InternetGatewayId': 'igw-028b0d9cc6fda54fa',
  'OwnerId': '203367053771',
  'Tags': [{'Key': 'Name', 'Value': 'cyruslab_igw_2'}]},
 {'Attachments': [{'State': 'available', 'VpcId': 'vpc-00b16be35cdd83960'}],
  'InternetGatewayId': 'igw-0d05c62094670bdb4',
  'OwnerId': '203367053771',
  'Tags': [{'Key': 'Name', 'Value': 'cyruslab_igw_3'}]},
 {'Attachments': [{'State': 'available', 'VpcId': 'vpc-072f78e3f64873a46'}],
  'InternetGatewayId': 'igw-0fd6b4aea334c6555',
  'OwnerId': '203367053771',
  'Tags': [{'Key': 'Name', 'Value': 'cyruslab_igw_1'}]}]

The value is a list of three dictionaries, each dictionaries has these keys:

  • Attachments – a list of one dictionary.
  • InternetGatewayId – has a value of IGW ID, which is what i need.
  • OwnerId
  • Tags – a list of one dictionary. To modify the tag, you need create_tags method in boto3.

How do I retrieve the vpc_id?
To access the values of key Attachments, supposed I need to get the vpc_id of cyruslab_igw_2 which is the first item in the list, I need to do this.
igw_responses['InternetGateways'][0]['Attachments'][0]['VpcId']

  • The Top of the entire json structure has two keys: ‘InternetGateways’ and ‘ResponseMetadata’, hence to access the values of ‘InternetGateways’ I use igw_responses['InternetGateways'].
  • The value of the key ‘InternetGateways’ is a list of three dictionaries, ‘cyruslab_igw_2’ is the first item in the list. Hence to access this first item in the list I use igw_responses['InternetGateways][0]
  • In order to get the value of vpc_id, I need to access through the key ‘Attachments’, hence i use igw_responses['InternetGateways'][0]['Attachments'].
  • The value of key 'Attachments' only has ONE dictionary, hence the first item always starts from 0 in a list. I use igw_responses['InternetGateways'][0]['Attachments'][0].
  • Now only the key value pair of 'VpcId', I only want the value hence I access it through the key, I use igw_responses['InternetGateways'][0]['Attachments'][0]['VpcId'].
  • I need to match the value against the supplied value.

These explains the logic of this for...in... loop.

    for i in igw_responses['InternetGateways']:
        for j in i['Attachments']:
            if vpc_id in j['VpcId']:

The VpcId is like a locator to locate which dictionary matches my supplied VpcId, in this example is igw_responses['InternetGateways'][0]. This dictionary has a key 'InternetGatewayId', I only want the value of the key, hence I use igw_responses['InternetGateways'][0]['InternetGatewayId']. This explains the following return value of the function.

igw_id = i['InternetGatewayId']
    return igw_id

afterword
I have been doing network and network security for a decade, I have evolved from a cli fan boy to a scripting enthusiast, my first api experience was with xml data structure, json structure used to intimidate me, however after a few practise I realise working with json structure is easier than xml structure. Manipulating the xml tree with beautifulsoup4 is easier than xml module, however there are at times i need xml module, working with xml structure in python is really hard.... Henceforth if the rest api provides both json and xml, i will prefer to work with json to xml.

Advertisements
This entry was posted in Python, Scripting and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s