Browse Source

changed examples to fit current AWS Academy lab env

master
Sebastian Rieger 3 years ago
parent
commit
14347b0b47
  1. 20
      example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/start.py
  2. 6
      example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/stop.py
  3. 28
      example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db-autoscaling/start.py
  4. 4
      example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db-autoscaling/stop.py
  5. 2
      example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/create-loadbalancer.py
  6. 10
      example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start-and-add-new-target.py
  7. 4
      example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/stop.py

20
example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/start.py

@ -28,9 +28,13 @@ imageId = 'ami-0d5eff06f840b45e9'
# potentially change instanceType to t2.micro for "free tier" if using a regular account # potentially change instanceType to t2.micro for "free tier" if using a regular account
# for production, t3.nano seams better # for production, t3.nano seams better
instanceType = 't2.nano'
# as of SoSe 2022 t2.nano seams to be a bit too low on memory, mariadb first start can fail
# due to innodb cache out of memory, therefore t2.micro or swap in t2.nano currently recommended
# instanceType = 't2.nano'
instanceType = 't2.micro'
keyName = 'srieger-pub'
# keyName = 'srieger-pub'
keyName = 'vockey'
################################################################################################ ################################################################################################
# #
@ -139,7 +143,10 @@ except ClientError as e:
print("Running new DB instance...") print("Running new DB instance...")
print("------------------------------------") print("------------------------------------")
print("...can take 10s of minutes using free AWS accounts and also takes substantially more time compared to"
" creating an EC2 instance also for payed accounts.")
# TODO potentially upgrade to recent RDS instance types (t4g.small or t3.small), possibly switch to mariadb or aurora
response = rdsClient.create_db_instance(DBInstanceIdentifier="tug-of-war-rds-db1", response = rdsClient.create_db_instance(DBInstanceIdentifier="tug-of-war-rds-db1",
AllocatedStorage=20, AllocatedStorage=20,
DBName='cloud_tug_of_war', DBName='cloud_tug_of_war',
@ -237,4 +244,11 @@ for i in range(1, 2):
instance.wait_until_running() instance.wait_until_running()
instance.load() instance.load()
print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address)
print("tug-of-war-in-the-clouds can be accessed at: http://" + instance.public_ip_address)
print("This example uses an RDS instance (managed MySQL offered by AWS) instead of installing the db on our own in"
"an EC2 instance. You can see different options esp. for replication etc. in RDS. The larger the instance,"
"the more fault tolerance options (esp. for Aurora) and performance, but obviously at higher costs. Compare"
" the trade-off between flexibility, maintenance effort and related costs.")
print("You can watch the creation of the DB in the cli using 'aws rds ...' or the web console.")

6
example-projects/tug-of-war-in-the-clouds/aws-boto3-rds-db/stop.py

@ -51,7 +51,7 @@ subnet_id = ec2Client.describe_subnets(
} }
])['Subnets'][0]['SubnetId'] ])['Subnets'][0]['SubnetId']
print("Deleting old instance...")
print("Deleting instance...")
print("------------------------------------") print("------------------------------------")
response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war-rds']}]) response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war-rds']}])
@ -65,7 +65,7 @@ for reservation in reservations:
instanceToTerminate = ec2Resource.Instance(instance['InstanceId']) instanceToTerminate = ec2Resource.Instance(instance['InstanceId'])
instanceToTerminate.wait_until_terminated() instanceToTerminate.wait_until_terminated()
print("Deleting old DB instance...")
print("Deleting DB instance...")
print("------------------------------------") print("------------------------------------")
try: try:
@ -82,7 +82,7 @@ waiter.wait(DBInstanceIdentifier='tug-of-war-rds-db1')
#time.sleep(5) #time.sleep(5)
print("Delete old security group...")
print("Delete security group...")
print("------------------------------------") print("------------------------------------")
try: try:

28
example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db-autoscaling/start.py

@ -41,6 +41,10 @@ instanceType = 't2.micro'
# keyName = 'srieger-pub' # keyName = 'srieger-pub'
keyName = 'vockey' keyName = 'vockey'
# see, e.g., AWS Academy Lab readme, or "aws iam list-instance-profiles | grep InstanceProfileName"
# for roles see: "aws iam list-roles | grep RoleName"
iamRole = 'LabInstanceProfile'
################################################################################################ ################################################################################################
# #
@ -85,7 +89,7 @@ subnet_id3 = ec2Client.describe_subnets(
])['Subnets'][0]['SubnetId'] ])['Subnets'][0]['SubnetId']
print("Deleting auto scaling group...")
print("Deleting old auto scaling group...")
print("------------------------------------") print("------------------------------------")
try: try:
@ -93,7 +97,7 @@ try:
except ClientError as e: except ClientError as e:
print(e) print(e)
print("Deleting launch configuration...")
print("Deleting old launch configuration...")
print("------------------------------------") print("------------------------------------")
try: try:
@ -118,7 +122,7 @@ for reservation in reservations:
instanceToTerminate.wait_until_terminated() instanceToTerminate.wait_until_terminated()
print("Deleting load balancer and deps...")
print("Deleting old load balancer and deps...")
print("------------------------------------") print("------------------------------------")
try: try:
@ -275,7 +279,7 @@ print("------------------------------------")
response = asClient.create_launch_configuration( response = asClient.create_launch_configuration(
#IamInstanceProfile='my-iam-role', #IamInstanceProfile='my-iam-role',
IamInstanceProfile='EMR_AutoScaling_DefaultRole ',
IamInstanceProfile=iamRole,
ImageId=imageId, ImageId=imageId,
InstanceType=instanceType, InstanceType=instanceType,
LaunchConfigurationName='tug-of-war-asg-launchconfig', LaunchConfigurationName='tug-of-war-asg-launchconfig',
@ -367,6 +371,9 @@ print(loadbalancer_arn)
print(targetgroup_arn) print(targetgroup_arn)
print('app/tug-of-war-asg-loadbalancer/'+str(loadbalancer_arn).split('/')[3]+'/targetgroup/tug-of-war-asg-targetgroup/'+str(targetgroup_arn).split('/')[2]) print('app/tug-of-war-asg-loadbalancer/'+str(loadbalancer_arn).split('/')[3]+'/targetgroup/tug-of-war-asg-targetgroup/'+str(targetgroup_arn).split('/')[2])
print('If target group is not found, creation was delayed in AWS Academy lab, need to add a check that target group is'
'existing before executing the next lines in the future... If the error occurs, rerun script...')
response = asClient.put_scaling_policy( response = asClient.put_scaling_policy(
AutoScalingGroupName='tug-of-war-asg-autoscalinggroup', AutoScalingGroupName='tug-of-war-asg-autoscalinggroup',
PolicyName='tug-of-war-asg-scalingpolicy', PolicyName='tug-of-war-asg-scalingpolicy',
@ -381,4 +388,15 @@ response = asClient.put_scaling_policy(
} }
) )
print('Load Balancer should be reachable at: ' + loadbalancer_dns)
print('Load Balancer should be reachable at: http://' + loadbalancer_dns)
print('As always, you need to wait some time, until load balancer is provisioned, instances are healthy (cloud-init '
'did its job as specified in the launch configuration). ')
print('You can use "aws elbv2 ..." commands or the web console to examine the current state. Take a look at Load'
'Balancer, Target Group, Auto Scaling Group and esp. Monitoring of the Load Balancer and related Cloud Watch'
'alarms.')
print('If you "pull" a lot of clouds in the game, generating a lot of requests, you will see the alarm being fired and'
'further instances started (scale-out) (involves some clicking for about three minutes). After 15 min of idling,'
'instances will automatically be stopped (scale-in).')

4
example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db-autoscaling/stop.py

@ -50,7 +50,7 @@ except ClientError as e:
print("Deleting old instances...")
print("Deleting instances...")
print("------------------------------------") print("------------------------------------")
response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war-asg']}]) response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war-asg']}])
@ -91,7 +91,7 @@ try:
except ClientError as e: except ClientError as e:
print(e) print(e)
print("Delete old security group...")
print("Delete security group...")
print("------------------------------------") print("------------------------------------")
try: try:

2
example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/create-loadbalancer.py

@ -148,4 +148,4 @@ print('Waiting for Load Balancer to become available...')
waiter = elbv2Client.get_waiter('load_balancer_available') waiter = elbv2Client.get_waiter('load_balancer_available')
waiter.wait(LoadBalancerArns=[loadbalancer_arn]) waiter.wait(LoadBalancerArns=[loadbalancer_arn])
print('Load Balancer should be reachable at: ' + loadbalancer_dns)
print('Load Balancer should be reachable at: http://' + loadbalancer_dns)

10
example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/start-and-add-new-target.py

@ -90,8 +90,8 @@ userDataWebServer = ('#!/bin/bash\n'
for i in range(3, 4): for i in range(3, 4):
print("Running new Web Server instance...")
print("------------------------------------")
print("Running new Web Server instance (in additional availability zone)...")
print("-----------------------------------------------------------------------------------------")
response = ec2Client.run_instances( response = ec2Client.run_instances(
ImageId=imageId, ImageId=imageId,
@ -135,15 +135,15 @@ for i in range(3, 4):
print("Could not get public IP using boto3, this is likely an AWS Educate problem. You can however lookup the " 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.") "public ip from the AWS management console.")
print("Registering instance in load balancer, load balancer has to be already created...")
print("-----------------------------------------------------------------------------------------")
try: try:
response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup']) response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup'])
targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '') targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
except ClientError as e: except ClientError as e:
print(e) print(e)
print("Registering instance...")
print("------------------------------------")
response = elbv2Client.register_targets( response = elbv2Client.register_targets(
TargetGroupArn=targetgroup_arn, TargetGroupArn=targetgroup_arn,
Targets=[ Targets=[

4
example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db/stop.py

@ -48,7 +48,7 @@ except ClientError as e:
print("Deleting old instances...")
print("Deleting instances...")
print("------------------------------------") print("------------------------------------")
response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war']}]) response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war']}])
@ -62,7 +62,7 @@ for reservation in reservations:
instanceToTerminate = ec2Resource.Instance(instance['InstanceId']) instanceToTerminate = ec2Resource.Instance(instance['InstanceId'])
instanceToTerminate.wait_until_terminated() instanceToTerminate.wait_until_terminated()
print("Delete old security group...")
print("Delete security group...")
print("------------------------------------") print("------------------------------------")
try: try:

Loading…
Cancel
Save