From 4188d098fcdfd3340503701804f048f2005c93d8 Mon Sep 17 00:00:00 2001 From: alok Date: Tue, 3 Jul 2018 10:48:58 -0700 Subject: [PATCH] creating user specific availability zones --- aws-code/configuration.txt | 16 ++++++++-------- aws-code/create_ondemand_instances.py | 20 +++++++++++++------- aws-code/get_availability_zones.sh | 13 +++++++++++++ 3 files changed, 34 insertions(+), 15 deletions(-) create mode 100755 aws-code/get_availability_zones.sh diff --git a/aws-code/configuration.txt b/aws-code/configuration.txt index e0f8b1acc..88fef8859 100644 --- a/aws-code/configuration.txt +++ b/aws-code/configuration.txt @@ -1,8 +1,8 @@ -1,us-east-1,virginia-key-benchmark,virginia-security-group,virginia -2,us-east-2,ohio-key-benchmark,ohio-security-group,ohio -3,us-west-1,california-key-benchmark,california-security-group,california -4,us-west-2,oregon-key-benchmark,oregon-security-group,oregon -5,ap-northeast-1,tokyo-key-benchmark,tokyo-security-group,tokyo -6,ap-southeast-1,singapore-key-benchmark,singapore-security-group,singapore -7,eu-central-1,frankfurt-key-benchmark,frankfurt-security-group,frankfurt -8,eu-west-1,ireland-key-benchmark,ireland-security-group,ireland \ No newline at end of file +1,us-east-1,virginia-key-benchmark,virginia-security-group,virginia,ami-b70554c8 +2,us-east-2,ohio-key-benchmark,ohio-security-group,ohio,ami-8c122be9 +3,us-west-1,california-key-benchmark,california-security-group,california,ami-e0ba5c83 +4,us-west-2,oregon-key-benchmark,oregon-security-group,oregon,ami-a9d09ed1 +5,ap-northeast-1,tokyo-key-benchmark,tokyo-security-group,tokyo,ami-e99f4896 +6,ap-southeast-1,singapore-key-benchmark,singapore-security-group,singapore,ami-05868579 +7,eu-central-1,frankfurt-key-benchmark,frankfurt-security-group,frankfurt,ami-7c4f7097 +8,eu-west-1,ireland-key-benchmark,ireland-security-group,ireland,ami-466768ac \ No newline at end of file diff --git a/aws-code/create_ondemand_instances.py b/aws-code/create_ondemand_instances.py index 62269bf87..533c702ce 100644 --- a/aws-code/create_ondemand_instances.py +++ b/aws-code/create_ondemand_instances.py @@ -9,7 +9,7 @@ REGION_KEY = 'region_key' REGION_SECURITY_GROUP = 'region_security_group' REGION_HUMAN_NAME = 'region_human_name' INSTANCE_TYPE = 't2.micro' -AMI = 'ami-a9d09ed1' +REGION_AMI = 'region_ami' USER_DATA = 'user-data.sh' IAM_INSTANCE_PROFILE = 'BenchMarkCodeDeployInstanceProfile' @@ -24,16 +24,20 @@ def read_configuration_file(filename): config[region_num][REGION_KEY] = mylist[2] config[region_num][REGION_SECURITY_GROUP] = mylist[3] config[region_num][REGION_HUMAN_NAME] = mylist[4] + config[region_num][REGION_AMI] = mylist[5] return config +def region_variant(region_name): + return region_name + "a" + def create_custom_json(config,num_instances,region_num,current_session): print(num_instances) input_cli = {} input_cli['MinCount'] = num_instances input_cli['MaxCount'] = num_instances - input_cli['ImageId'] = 'ami-b70554c8' + input_cli['ImageId'] = config[region_num][REGION_AMI] input_cli['Placement'] = {} - input_cli['Placement']['AvailabilityZone'] = config[region_num][REGION_NAME] +"a" + input_cli['Placement']['AvailabilityZone'] = region_variant(config[region_num][REGION_NAME]) input_cli['SecurityGroups'] = [] input_cli['SecurityGroups'].append(config[region_num][REGION_SECURITY_GROUP]) input_cli['IamInstanceProfile'] = {} @@ -47,23 +51,25 @@ def create_custom_json(config,num_instances,region_num,current_session): my_dir = "input_jsons/" + "session-"+ current_session if not os.path.exists(my_dir): os.makedirs(my_dir) - cli_input_file = os.path.join(my_dir,config[region_num][REGION_HUMAN_NAME]+".json") + #cli_input_file = os.path.join(my_dir,config[region_num][REGION_HUMAN_NAME]+".json") cli_input_file = "local.json" with open(cli_input_file,'w') as g: json.dump(input_cli,g) print("INPUT CLI JSON FILE: %s" % cli_input_file) return cli_input_file -def create_instances(region_list,instances_list,current_session): +def create_instances(config,region_list,instances_list,current_session): for i in range(len(region_list)): region_num = region_list[i] num_instances = int(instances_list[i]) cli_input_file = create_custom_json(config,num_instances,region_num,current_session) + cmd_str = "aws ec2 --region " + config[region_num][REGION_NAME] + " run-instances " + " --cli-input-json " + str(cli_input_file) + print(cmd_str) if __name__ == "__main__": parser = argparse.ArgumentParser(description='This script helps you start instances across multiple regions') parser.add_argument('--regions',type=str,dest='regions',default='3',help="Supply a csv list of all regions") - parser.add_argument('--instances', type=str,dest='numInstances',default=1,help='number of shards') + parser.add_argument('--instances', type=str,dest='numInstances',default=1,help='number of instances') parser.add_argument('--configuration',type=str,dest='config',default='configuration.txt') args = parser.parse_args() config = read_configuration_file(args.config) @@ -73,6 +79,6 @@ if __name__ == "__main__": time_stamp = time.time() current_session = datetime.datetime.fromtimestamp(time_stamp).strftime('%H-%M-%S-%Y-%m-%d') print("current session is %s" % current_session) - create_instances(region_list,instances_list,current_session) + create_instances(config,region_list,instances_list,current_session) \ No newline at end of file diff --git a/aws-code/get_availability_zones.sh b/aws-code/get_availability_zones.sh new file mode 100755 index 000000000..c5382fb0d --- /dev/null +++ b/aws-code/get_availability_zones.sh @@ -0,0 +1,13 @@ +echo "Getting list of Availability Zones" +all_regions=$(aws ec2 describe-regions --output text --query 'Regions[*].[RegionName]' | sort) +all_az=() + +echo $all_regions +while read -r region; do + az_per_region=$(aws ec2 describe-availability-zones --region $region --query 'AvailabilityZones[*].[ZoneName]' --output text | sort) + echo $region $az_per_region + while read -r az; do + all_az+=($az) + done <<< "$az_per_region" +done <<< "$all_regions" +echo $all_az