I have a simple bit of code that goes into aws and grabs some data then prints it out to console
MyCode:
import boto3
from pprint import pprint
ec2 = boto3.resource('ec2')
client = boto3.client('ec2')
#This is the VPC ID and Linked Tags
for vpctags in client.describe_vpcs()['Vpcs']:
print("VPC ID: ", vpctags['VpcId'])
print("Tags: ", vpctags['Tags'])
for subnet in client.describe_subnets()['Subnets']:
print("Subnet ID: ", subnet['SubnetId'])
print("Subnet ID: ", subnet['Tags'])
###############################################
I get an error because one or more of my subnets don't have tags:
print("Subnet ID: ", subnet['Tags']) KeyError: 'Tags'
I'm not expecting every subnet to have tags so is there a way to simply ignore the lack of tags and just print empty or simply move on?
sorry if this sounds like a silly question, I've searched google and found some ideas but they look a little advanced for what I have.
Yes, You can replace
print("Subnet ID: ", subnet['Tags'])
with
print("Subnet ID: ", subnet.get('Tags', ''))
Using get with allow you to define a default value in case Tags doesn't exist
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With