From 4733dc6b5beeaf8bc1d78248bfbad96e39bb2a21 Mon Sep 17 00:00:00 2001 From: jclaybaugh Date: Fri, 7 Jun 2013 18:13:22 +0000 Subject: [PATCH 1/2] added error checking around boto ec2 connection errors when using a bad region name --- plugins/inventory/ec2.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/plugins/inventory/ec2.py b/plugins/inventory/ec2.py index e001385dcc0..a680b08b3af 100755 --- a/plugins/inventory/ec2.py +++ b/plugins/inventory/ec2.py @@ -244,7 +244,11 @@ class Ec2Inventory(object): conn.APIVersion = '2010-08-31' else: conn = ec2.connect_to_region(region) - + + if conn is None: + print("AWS is down or region name: %s likely not supported. connection to region failed." % region) + sys.exit(1) + reservations = conn.get_all_instances() for reservation in reservations: for instance in reservation.instances: @@ -279,6 +283,10 @@ class Ec2Inventory(object): else: conn = ec2.connect_to_region(region) + if conn is None: + print("AWS is down or region name: %s likely not supported. connection to region failed." % region) + sys.exit(1) + reservations = conn.get_all_instances([instance_id]) for reservation in reservations: for instance in reservation.instances: From f4db6d1a9115face2f64a727728fba451764fbe2 Mon Sep 17 00:00:00 2001 From: jclaybaugh Date: Fri, 7 Jun 2013 18:25:14 +0000 Subject: [PATCH 2/2] fixed whitespace and added a comment to explain if statement --- plugins/inventory/ec2.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/plugins/inventory/ec2.py b/plugins/inventory/ec2.py index a680b08b3af..3b495d47071 100755 --- a/plugins/inventory/ec2.py +++ b/plugins/inventory/ec2.py @@ -245,9 +245,10 @@ class Ec2Inventory(object): else: conn = ec2.connect_to_region(region) - if conn is None: - print("AWS is down or region name: %s likely not supported. connection to region failed." % region) - sys.exit(1) + # connect_to_region will fail "silently" by returning None if the region name is wrong or not supported + if conn is None: + print("region name: %s likely not supported, or AWS is down. connection to region failed." % region) + sys.exit(1) reservations = conn.get_all_instances() for reservation in reservations: @@ -283,9 +284,10 @@ class Ec2Inventory(object): else: conn = ec2.connect_to_region(region) + # connect_to_region will fail "silently" by returning None if the region name is wrong or not supported if conn is None: - print("AWS is down or region name: %s likely not supported. connection to region failed." % region) - sys.exit(1) + print("region name: %s likely not supported, or AWS is down. connection to region failed." % region) + sys.exit(1) reservations = conn.get_all_instances([instance_id]) for reservation in reservations: