AWS Boto3: Using Boto3 to Describe VPC
To describe a VPC is to retrieve the values of it attributes. A task we might perform to validate configuration.
This article will demonstrate the following:
- Find VPC ID using filters
- Retrieve VPC configuration values
Information on Boto3 can be found here.
This post assumes that you already have a working Boto3 installation. Including IAM configuration to perform the task. If you have not, click here for the install document.
Modules and EC2 connection
We require the JSON and Boto3 modules. Thus, they will be imported at the start of the script. The reason for Boto3 should be fairly straight forward. To make the responses readable, JSON is required.
The third line connects to EC2 for our region. Adjust the region name as required.
Filters are used to scope our results. These filters are based on tags. Such as, Name, VPCID etc. You can use multiple entries for the value or a wildcard “*”.
Now to perform the query using the ec2.vpcs resource.
The variable vpcs now contains a list of VPCIDs for the VPCs which matched our filter.
Describing the VPCs
We have searched for VPCs and their IDs are now in the list vpcs. It’s now time to see some information on them.
We will utilise a for loop to achieve this.
vpc.id provides the ID number for each entry.
I have ended this with a print. You can of course change that to store the output for further use.
The formatted response should be similar to this:
The script provides a framework to audit VPCs and their configurations. I hope that you found it helpful.
The script in full can be found at https://github.com/oversizedspoon/DescribeVPC-BOTO3