adapted aws-boto3-standalone-db example of tug-of-war in the clouds to be usable with AWS Educate Accounts
This commit is contained in:
		@@ -7,17 +7,28 @@ import boto3
 | 
				
			|||||||
################################################################################################
 | 
					################################################################################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
region = 'eu-central-1'
 | 
					# place your credentials in ~/.aws/credentials, as mentioned in AWS Educate Classroom,
 | 
				
			||||||
availabilityZone = 'eu-central-1b'
 | 
					# Account Details, AWC CLI -> Show (Copy and paste the following into ~/.aws/credentials)
 | 
				
			||||||
vpc_id = 'vpc-eedd4187'
 | 
					 | 
				
			||||||
subnet1 = 'subnet-41422b28'
 | 
					 | 
				
			||||||
subnet2 = 'subnet-5c5f6d16'
 | 
					 | 
				
			||||||
subnet3 = 'subnet-6f2ea214'
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# if you only have one VPC, vpc_id can be retrieved using:
 | 
					# changed to use us-east, to be able to use AWS Educate Classroom
 | 
				
			||||||
#
 | 
					region = 'us-east-1'
 | 
				
			||||||
# response = ec2Client.describe_vpcs()
 | 
					availabilityZone1 = 'us-east-1a'
 | 
				
			||||||
# vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
 | 
					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")
 | 
					ec2Client = boto3.client("ec2")
 | 
				
			||||||
ec2Resource = boto3.resource('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']}])
 | 
					response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war']}])
 | 
				
			||||||
security_group_id = response.get('SecurityGroups', [{}])[0].get('GroupId', '')
 | 
					security_group_id = response.get('SecurityGroups', [{}])[0].get('GroupId', '')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -39,9 +79,9 @@ elbv2Client = boto3.client('elbv2')
 | 
				
			|||||||
response = elbv2Client.create_load_balancer(
 | 
					response = elbv2Client.create_load_balancer(
 | 
				
			||||||
    Name='tug-of-war-loadbalancer',
 | 
					    Name='tug-of-war-loadbalancer',
 | 
				
			||||||
    Subnets=[
 | 
					    Subnets=[
 | 
				
			||||||
        subnet1,
 | 
					        subnet_id1,
 | 
				
			||||||
        subnet2,
 | 
					        subnet_id2,
 | 
				
			||||||
        subnet3,
 | 
					        subnet_id3
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
    SecurityGroups=[
 | 
					    SecurityGroups=[
 | 
				
			||||||
        security_group_id
 | 
					        security_group_id
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,3 +1,5 @@
 | 
				
			|||||||
 | 
					import time
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import boto3
 | 
					import boto3
 | 
				
			||||||
from botocore.exceptions import ClientError
 | 
					from botocore.exceptions import ClientError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -9,17 +11,26 @@ from botocore.exceptions import ClientError
 | 
				
			|||||||
################################################################################################
 | 
					################################################################################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
region = 'eu-central-1'
 | 
					# place your credentials in ~/.aws/credentials, as mentioned in AWS Educate Classroom,
 | 
				
			||||||
availabilityZone = 'eu-central-1b'
 | 
					# Account Details, AWC CLI -> Show (Copy and paste the following into ~/.aws/credentials)
 | 
				
			||||||
vpc_id = 'vpc-eedd4187'
 | 
					 | 
				
			||||||
imageId = 'ami-0cc293023f983ed53'
 | 
					 | 
				
			||||||
instanceType = 't3.nano'
 | 
					 | 
				
			||||||
keyName = 'srieger-pub'
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# if you only have one VPC, vpc_id can be retrieved using:
 | 
					# changed to use us-east, to be able to use AWS Educate Classroom
 | 
				
			||||||
#
 | 
					region = 'us-east-1'
 | 
				
			||||||
# response = ec2Client.describe_vpcs()
 | 
					availabilityZone = 'us-east-1b'
 | 
				
			||||||
# vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
 | 
					# 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 = ec2Resource.Instance(instanceIdWeb)
 | 
				
			||||||
    instance.wait_until_running()
 | 
					    instance.wait_until_running()
 | 
				
			||||||
    instance.load()
 | 
					    instance.load()
 | 
				
			||||||
 | 
					    # sometimes even after reloading instance details, public IP cannot be retrieved using current boto3 version and
 | 
				
			||||||
    print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address)
 | 
					    # 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:
 | 
					try:
 | 
				
			||||||
    response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup'])
 | 
					    response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup'])
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,3 +1,5 @@
 | 
				
			|||||||
 | 
					import time
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import boto3
 | 
					import boto3
 | 
				
			||||||
from botocore.exceptions import ClientError
 | 
					from botocore.exceptions import ClientError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -9,17 +11,26 @@ from botocore.exceptions import ClientError
 | 
				
			|||||||
################################################################################################
 | 
					################################################################################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
region = 'eu-central-1'
 | 
					# place your credentials in ~/.aws/credentials, as mentioned in AWS Educate Classroom,
 | 
				
			||||||
availabilityZone = 'eu-central-1b'
 | 
					# Account Details, AWC CLI -> Show (Copy and paste the following into ~/.aws/credentials)
 | 
				
			||||||
vpc_id = 'vpc-eedd4187'
 | 
					 | 
				
			||||||
imageId = 'ami-0cc293023f983ed53'
 | 
					 | 
				
			||||||
instanceType = 't3.nano'
 | 
					 | 
				
			||||||
keyName = 'srieger-pub'
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# if you only have one VPC, vpc_id can be retrieved using:
 | 
					# changed to use us-east, to be able to use AWS Educate Classroom
 | 
				
			||||||
#
 | 
					region = 'us-east-1'
 | 
				
			||||||
# response = ec2Client.describe_vpcs()
 | 
					availabilityZone = 'us-east-1a'
 | 
				
			||||||
# vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
 | 
					# 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")
 | 
					ec2Client = boto3.client("ec2")
 | 
				
			||||||
ec2Resource = boto3.resource('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(
 | 
					subnet_id = ec2Client.describe_subnets(
 | 
				
			||||||
    Filters=[
 | 
					    Filters=[
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -205,5 +223,16 @@ for i in range(1, 3):
 | 
				
			|||||||
    instance = ec2Resource.Instance(instanceIdWeb)
 | 
					    instance = ec2Resource.Instance(instanceIdWeb)
 | 
				
			||||||
    instance.wait_until_running()
 | 
					    instance.wait_until_running()
 | 
				
			||||||
    instance.load()
 | 
					    instance.load()
 | 
				
			||||||
 | 
					    # sometimes even after reloading instance details, public IP cannot be retrieved using current boto3 version and
 | 
				
			||||||
    print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address)
 | 
					    # 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.")
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,7 +10,8 @@ from botocore.exceptions import ClientError
 | 
				
			|||||||
################################################################################################
 | 
					################################################################################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
region = 'eu-central-1'
 | 
					region = 'us-east-1'
 | 
				
			||||||
 | 
					# region = 'eu-central-1'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
################################################################################################
 | 
					################################################################################################
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user