Mar-07-2019, 09:04 PM
(This post was last modified: Mar-07-2019, 09:04 PM by bluethundr.)
Apparently this is better handled as an if statement. However I am having trouble dealing with dictionary keys that are missing.
I get the following error when the code runs into a stopped AWS instance that does not have a Public IP address:
I get the following error when the code runs into a stopped AWS instance that does not have a Public IP address:
Error:-------------------------------------
Instance ID: i-86533615
Type: m4.xlarge
State: stopped
Private IP: 10.1.233.18
Traceback (most recent call last):
File ".\aws_ec2_list_instances.py", line 43, in <module>
print(Fore.CYAN + "{0}: {1}".format(key, instance[key]))
KeyError: 'Public IP'
Here is my code:#!usr/bin/env python import boto3 import collections from collections import defaultdict import time from datetime import datetime from colorama import init, deinit, Fore, Back, Style init() print(Fore.CYAN) print('******************************************************') print(' Terminate AWS Instances', 'blue') print('******************************************************\n') print(Fore.YELLOW) aws_account = input("Enter the name of the AWS account you'll be working in: ") session = boto3.Session(profile_name=aws_account) ec2 = session.client('ec2') response = ec2.describe_instances() ec2info = defaultdict() for reservation in response["Reservations"]: for instance in reservation["Instances"]: instance_state = instance['State']['Name'] if 'PrivateIpAddress' in instance and 'PublicIpAddress' in instance: launch_time = instance['LaunchTime'] launch_time_friendly = launch_time.strftime("%B %d %Y") ec2info[instance['InstanceId']] = { 'Instance ID': instance['InstanceId'], 'Type': instance['InstanceType'], 'State': instance_state, 'Private IP': instance['PrivateIpAddress'], 'Public IP': instance['PublicIpAddress'], 'Launch Time' : launch_time_friendly } attributes = ['Instance ID', 'Type', 'State', 'Private IP', 'Public IP', 'Launch Time' ] for instance_id, instance in ec2info.items(): print(Fore.RESET + "-------------------------------------") for key in attributes: print(Fore.CYAN + "{0}: {1}".format(key, instance[key])) print(Fore.RESET + "-------------------------------------") elif 'PrivateIpAddress' in instance: launch_time = instance['LaunchTime'] launch_time_friendly = launch_time.strftime("%B %d %Y") ec2info[instance['InstanceId']] = { 'Instance ID': instance['InstanceId'], 'Type': instance['InstanceType'], 'State': instance_state, 'Private IP': instance['PrivateIpAddress'], 'Launch Time' : launch_time_friendly } attributes = ['Instance ID', 'Type', 'State', 'Private IP', 'Launch Time' ] for instance_id, instance in ec2info.items(): print(Fore.RESET + "-------------------------------------") for key in attributes: print(Fore.CYAN + "{0}: {1}".format(key, instance[key])) print(Fore.RESET + "-------------------------------------") else: launch_time = instance['LaunchTime'] launch_time_friendly = launch_time.strftime("%B %d %Y") ec2info[instance['InstanceId']] = { 'Instance ID': instance['InstanceId'], 'Type': instance['InstanceType'], 'State': instance_state, 'Launch Time' : launch_time_friendly } attributes = ['Instance ID', 'Type', 'State', 'Launch Time' ] for instance_id, instance in ec2info.items(): print(Fore.RESET + "-------------------------------------") for key in attributes: print(Fore.CYAN + "{0}: {1}".format(key, instance[key])) print(Fore.RESET + "-------------------------------------")How can I control the output in the case of stopped instances that don't have either a public IP or a private IP address?