diff --git a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/create-loadbalancer.py b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/create-loadbalancer.py index 7114094..2e5f725 100644 --- a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/create-loadbalancer.py +++ b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/create-loadbalancer.py @@ -7,17 +7,28 @@ import boto3 ################################################################################################ -region = 'eu-central-1' -availabilityZone = 'eu-central-1b' -vpc_id = 'vpc-eedd4187' -subnet1 = 'subnet-41422b28' -subnet2 = 'subnet-5c5f6d16' -subnet3 = 'subnet-6f2ea214' +# place your credentials in ~/.aws/credentials, as mentioned in AWS Educate Classroom, +# Account Details, AWC CLI -> Show (Copy and paste the following into ~/.aws/credentials) -# if you only have one VPC, vpc_id can be retrieved using: -# -# response = ec2Client.describe_vpcs() -# vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') +# changed to use us-east, to be able to use AWS Educate Classroom +region = 'us-east-1' +availabilityZone1 = 'us-east-1a' +availabilityZone2 = 'us-east-1b' +availabilityZone3 = 'us-east-1c' +# region = 'eu-central-1' +# availabilityZone = 'eu-central-1b' + +# AMI ID of Amazon Linux 2 image 64-bit x86 in us-east-1 (can be retrieved, e.g., at +# https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#LaunchInstanceWizard:) +imageId = 'ami-0d5eff06f840b45e9' +# for eu-central-1, AMI ID of Amazon Linux 2 would be: +# imageId = 'ami-0cc293023f983ed53' + +# potentially change instanceType to t2.micro for "free tier" if using a regular account +# for production, t3.nano seams better +instanceType = 't2.nano' + +keyName = 'srieger-pub' ################################################################################################ @@ -31,6 +42,35 @@ client = boto3.setup_default_session(region_name=region) ec2Client = boto3.client("ec2") ec2Resource = boto3.resource('ec2') +# if you only have one VPC, vpc_id can be retrieved using: +response = ec2Client.describe_vpcs() +vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') +# if you have more than one VPC, vpc_id should be specified, and code +# top retrieve VPC id below needs to be commented out +# vpc_id = 'vpc-eedd4187' + +subnet_id1 = ec2Client.describe_subnets( + Filters=[ + { + 'Name': 'availability-zone', 'Values': [availabilityZone1] + } + ])['Subnets'][0]['SubnetId'] + +subnet_id2 = ec2Client.describe_subnets( + Filters=[ + { + 'Name': 'availability-zone', 'Values': [availabilityZone2] + } + ])['Subnets'][0]['SubnetId'] + +subnet_id3 = ec2Client.describe_subnets( + Filters=[ + { + 'Name': 'availability-zone', 'Values': [availabilityZone3] + } + ])['Subnets'][0]['SubnetId'] + + response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war']}]) security_group_id = response.get('SecurityGroups', [{}])[0].get('GroupId', '') @@ -39,9 +79,9 @@ elbv2Client = boto3.client('elbv2') response = elbv2Client.create_load_balancer( Name='tug-of-war-loadbalancer', Subnets=[ - subnet1, - subnet2, - subnet3, + subnet_id1, + subnet_id2, + subnet_id3 ], SecurityGroups=[ security_group_id diff --git a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start-and-add-new-target.py b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start-and-add-new-target.py index a8d4dfe..3feb52b 100644 --- a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start-and-add-new-target.py +++ b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start-and-add-new-target.py @@ -1,3 +1,5 @@ +import time + import boto3 from botocore.exceptions import ClientError @@ -9,17 +11,26 @@ from botocore.exceptions import ClientError ################################################################################################ -region = 'eu-central-1' -availabilityZone = 'eu-central-1b' -vpc_id = 'vpc-eedd4187' -imageId = 'ami-0cc293023f983ed53' -instanceType = 't3.nano' -keyName = 'srieger-pub' +# place your credentials in ~/.aws/credentials, as mentioned in AWS Educate Classroom, +# Account Details, AWC CLI -> Show (Copy and paste the following into ~/.aws/credentials) -# if you only have one VPC, vpc_id can be retrieved using: -# -# response = ec2Client.describe_vpcs() -# vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') +# changed to use us-east, to be able to use AWS Educate Classroom +region = 'us-east-1' +availabilityZone = 'us-east-1b' +# region = 'eu-central-1' +# availabilityZone = 'eu-central-1b' + +# AMI ID of Amazon Linux 2 image 64-bit x86 in us-east-1 (can be retrieved, e.g., at +# https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#LaunchInstanceWizard:) +imageId = 'ami-0d5eff06f840b45e9' +# for eu-central-1, AMI ID of Amazon Linux 2 would be: +# imageId = 'ami-0cc293023f983ed53' + +# potentially change instanceType to t2.micro for "free tier" if using a regular account +# for production, t3.nano seams better +instanceType = 't2.nano' + +keyName = 'srieger-pub' ################################################################################################ @@ -105,8 +116,19 @@ for i in range(3, 4): instance = ec2Resource.Instance(instanceIdWeb) instance.wait_until_running() instance.load() - - print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address) + # sometimes even after reloading instance details, public IP cannot be retrieved using current boto3 version and + # AWS Educate accounts, try for 10 secs, and ask user to get it from AWS console otherwise + timeout = 10 + while instance.public_ip_address is None and timeout > 0: + print("Waiting for public IP to become available...") + instance.load() + time.sleep(1) + timeout -= 1 + if instance.public_ip_address is not None: + print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address) + else: + print("Could not get public IP using boto3, this is likely an AWS Educate problem. You can however lookup the " + "public ip from the AWS management console.") try: response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup']) diff --git a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start.py b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start.py index f95de7a..f38255c 100644 --- a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start.py +++ b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start.py @@ -1,3 +1,5 @@ +import time + import boto3 from botocore.exceptions import ClientError @@ -9,17 +11,26 @@ from botocore.exceptions import ClientError ################################################################################################ -region = 'eu-central-1' -availabilityZone = 'eu-central-1b' -vpc_id = 'vpc-eedd4187' -imageId = 'ami-0cc293023f983ed53' -instanceType = 't3.nano' -keyName = 'srieger-pub' +# place your credentials in ~/.aws/credentials, as mentioned in AWS Educate Classroom, +# Account Details, AWC CLI -> Show (Copy and paste the following into ~/.aws/credentials) -# if you only have one VPC, vpc_id can be retrieved using: -# -# response = ec2Client.describe_vpcs() -# vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') +# changed to use us-east, to be able to use AWS Educate Classroom +region = 'us-east-1' +availabilityZone = 'us-east-1a' +# region = 'eu-central-1' +# availabilityZone = 'eu-central-1b' + +# AMI ID of Amazon Linux 2 image 64-bit x86 in us-east-1 (can be retrieved, e.g., at +# https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#LaunchInstanceWizard:) +imageId = 'ami-0d5eff06f840b45e9' +# for eu-central-1, AMI ID of Amazon Linux 2 would be: +# imageId = 'ami-0cc293023f983ed53' + +# potentially change instanceType to t2.micro for "free tier" if using a regular account +# for production, t3.nano seams better +instanceType = 't2.nano' + +keyName = 'srieger-pub' ################################################################################################ @@ -33,6 +44,13 @@ client = boto3.setup_default_session(region_name=region) ec2Client = boto3.client("ec2") ec2Resource = boto3.resource('ec2') +# if you only have one VPC, vpc_id can be retrieved using: +response = ec2Client.describe_vpcs() +vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '') +# if you have more than one VPC, vpc_id should be specified, and code +# top retrieve VPC id below needs to be commented out +# vpc_id = 'vpc-eedd4187' + subnet_id = ec2Client.describe_subnets( Filters=[ { @@ -205,5 +223,16 @@ for i in range(1, 3): instance = ec2Resource.Instance(instanceIdWeb) instance.wait_until_running() instance.load() - - print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address) + # sometimes even after reloading instance details, public IP cannot be retrieved using current boto3 version and + # AWS Educate accounts, try for 10 secs, and ask user to get it from AWS console otherwise + timeout = 10 + while instance.public_ip_address is None and timeout > 0: + print("Waiting for public IP to become available...") + instance.load() + time.sleep(1) + timeout -= 1 + if instance.public_ip_address is not None: + print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address) + else: + print("Could not get public IP using boto3, this is likely an AWS Educate problem. You can however lookup the " + "public ip from the AWS management console.") diff --git a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/stop.py b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/stop.py index 233da15..48ff8c2 100644 --- a/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/stop.py +++ b/example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/stop.py @@ -10,7 +10,8 @@ from botocore.exceptions import ClientError ################################################################################################ -region = 'eu-central-1' +region = 'us-east-1' +# region = 'eu-central-1' ################################################################################################