Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Allow ec2-modify-ebs-volume.py to connect to regions other than default #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions ec2-modify-ebs-volume/ec2-modify-ebs-volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,19 @@ def wait_aws_event(object_in, object_type, wait_for_string):
return object_status


def get_region_connection(region):
try:
logging.info('establishing AWS API connection with region {!s}'.format(region))
return ec2.connect_to_region(region)
except:
logging.critical('An error occured when attempting to connect to the AWS region ' + region)
exit(1)


default_region = 'us-east-1'
# creates ec2_connection object
try:
ec2_connection = ec2.connect_to_region('us-east-1')
except:
logging.critical('An error occured when attempting to connect to the AWS API.')
exit(1)
ec2_connection = get_region_connection(default_region)

# aws_regions contains a list of strings representing AWS regions
aws_regions = [ (str(region.name)) for region in ec2_connection.get_all_regions() ]

Expand All @@ -300,7 +307,7 @@ def wait_aws_event(object_in, object_type, wait_for_string):
help='set the log level when running {!s}.'.format(app_name))
parser.add_argument('--region',
help='set the region where instances should be located.',
default='us-east-1', choices=aws_regions)
default=default_region, choices=aws_regions)
parser.add_argument('--volume-size', dest='volume_size', type=int, default=None,
help='set the volume size that the EBS volume should be.')
parser.add_argument('--volume-type', dest='volume_type', default=None,
Expand All @@ -317,6 +324,10 @@ def wait_aws_event(object_in, object_type, wait_for_string):
instance_id = args.instance_id
device = args.device

# reconnect if we want to use a non-default region
if region != default_region:
ec2_connection = get_region_connection(region)

# gets an instance object corresponding to the instance_id
selected_instance = get_selected_instances(instance_id)

Expand Down