[python]Get VPC ID

I am looking for a solution to get the vpcid so that i can start another script to add routes and subnets for the chosen vpc.
See this post, which makes the below code possible.

import boto3
from pprint import pprint


filter = [{'Name':'tag:Name', 'Values':['*']}]
ec2_client = boto3.client('ec2')
""":type : pyboto3.ec2"""
vpcs = ec2_client.describe_vpcs(Filters=filter)['Vpcs']
for vpc in vpcs:
    pprint(vpc['VpcId'])

Results

The json structure after the filter was applied

[{'CidrBlock': '192.168.1.0/24',
  'CidrBlockAssociationSet': [{'AssociationId': 'vpc-cidr-assoc-0e252bc4c542b4f28',
                               'CidrBlock': '192.168.1.0/24',
                               'CidrBlockState': {'State': 'associated'}}],
  'DhcpOptionsId': 'dopt-9f1249f8',
  'InstanceTenancy': 'default',
  'IsDefault': False,
  'OwnerId': '203367053771',
  'State': 'available',
  'Tags': [{'Key': 'Name', 'Value': 'cyruslab_1'}],
  'VpcId': 'vpc-072f78e3f64873a46'},
 {'CidrBlock': '192.168.2.0/24',
  'CidrBlockAssociationSet': [{'AssociationId': 'vpc-cidr-assoc-01861ed0994c5b920',
                               'CidrBlock': '192.168.2.0/24',
                               'CidrBlockState': {'State': 'associated'}}],
  'DhcpOptionsId': 'dopt-9f1249f8',
  'InstanceTenancy': 'default',
  'IsDefault': False,
  'OwnerId': '203367053771',
  'State': 'available',
  'Tags': [{'Key': 'Name', 'Value': 'cyruslab_2'}],
  'VpcId': 'vpc-0d5a397499f3bc8b8'}]

The above json output is a list of two dictionaries.
First is the below, take the above json output is stored in a variable named “response”, then the below will be response[0]:

{'CidrBlock': '192.168.1.0/24',
  'CidrBlockAssociationSet': [{'AssociationId': 'vpc-cidr-assoc-0e252bc4c542b4f28',
                               'CidrBlock': '192.168.1.0/24',
                               'CidrBlockState': {'State': 'associated'}}],
  'DhcpOptionsId': 'dopt-9f1249f8',
  'InstanceTenancy': 'default',
  'IsDefault': False,
  'OwnerId': '203367053771',
  'State': 'available',
  'Tags': [{'Key': 'Name', 'Value': 'cyruslab_1'}],
  'VpcId': 'vpc-072f78e3f64873a46'}

See response[1]:

{'CidrBlock': '192.168.2.0/24',
  'CidrBlockAssociationSet': [{'AssociationId': 'vpc-cidr-assoc-01861ed0994c5b920',
                               'CidrBlock': '192.168.2.0/24',
                               'CidrBlockState': {'State': 'associated'}}],
  'DhcpOptionsId': 'dopt-9f1249f8',
  'InstanceTenancy': 'default',
  'IsDefault': False,
  'OwnerId': '203367053771',
  'State': 'available',
  'Tags': [{'Key': 'Name', 'Value': 'cyruslab_2'}],
  'VpcId': 'vpc-0d5a397499f3bc8b8'}

Python uses for in statement to enumerate each item in a list.
So for vpc in vpcs will mean vpc = vpcs[0] then vpc = vpcs[1], until the entire item is enumerated within the for loop. As mentioned the result after filtered, is a list of two dictionaries, in order to get the value, I need to reference its key.
Hence for vpc in vpcs means vpc = vpcs[0], vpc is the first dictionary in the list, I only interested in vpc id, hence vpc['VpcId] and so on.. until the entire list is enumerated.

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