Browse Source

improved autoscaling example

master
Sebastian Rieger 5 years ago
parent
commit
b7143441f2
  1. 28
      example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db-autoscaling/start.py
  2. 120
      example-projects/tug-of-war-in-the-clouds/aws-boto3-standalone-db-autoscaling/stop.py

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

@ -70,7 +70,7 @@ print(response)
reservations = response['Reservations']
for reservation in reservations:
for instance in reservation['Instances']:
if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending":
if instance['State']['Name'] == "running":
response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']])
print(response)
instanceToTerminate = ec2Resource.Instance(instance['InstanceId'])
@ -84,24 +84,38 @@ try:
response = elbv2Client.describe_load_balancers(Names=['tug-of-war-asg-loadbalancer'])
loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '')
response = elbv2Client.delete_load_balancer(LoadBalancerArn=loadbalancer_arn)
waiter = elbv2Client.get_waiter('load_balancers_deleted')
waiter.wait(LoadBalancerArns=[loadbalancer_arn])
except ClientError as e:
print(e)
time.sleep(5)
try:
response = elbv2Client.describe_target_groups(Names=['tug-of-war-asg-targetgroup'])
while len(response.get('TargetGroups', [{}])) > 0:
targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
try:
response = elbv2Client.delete_target_group(TargetGroupArn=targetgroup_arn)
except ClientError as e:
print(e)
response = elbv2Client.describe_target_groups(Names=['tug-of-war-asg-targetgroup'])
time.sleep(5)
except ClientError as e:
print(e)
print("Delete old security group...")
print("------------------------------------")
try:
response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war-asg']}])
while len(response.get('SecurityGroups', [{}])) > 0:
security_group_id = response.get('SecurityGroups', [{}])[0].get('GroupId', '')
try:
response = ec2Client.delete_security_group(GroupName='tug-of-war-asg')
except ClientError as e:
print(e)
response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war-asg']}])
time.sleep(5)
except ClientError as e:
print(e)
@ -288,7 +302,9 @@ response = asClient.create_auto_scaling_group(
],
)
print('app/tug-of-war-asg-loadbalancer/'+str(loadbalancer_arn).split('/')[2]+'/targetgroup/tug-of-war-asg-targetgroup/'+str(targetgroup_arn).split('/')[1]+'target-group-id')
print(loadbalancer_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])
response = asClient.put_scaling_policy(
AutoScalingGroupName='tug-of-war-asg-autoscalinggroup',
@ -298,7 +314,7 @@ response = asClient.put_scaling_policy(
TargetTrackingConfiguration={
'PredefinedMetricSpecification': {
'PredefinedMetricType': 'ALBRequestCountPerTarget',
'ResourceLabel': 'app/tug-of-war-asg-loadbalancer/'+str(loadbalancer_arn).split('/')[2]+'/targetgroup/tug-of-war-asg-targetgroup/'+str(targetgroup_arn).split('/')[1]+'target-group-id'
'ResourceLabel': 'app/tug-of-war-asg-loadbalancer/'+str(loadbalancer_arn).split('/')[3]+'/targetgroup/tug-of-war-asg-targetgroup/'+str(targetgroup_arn).split('/')[2]
},
'TargetValue': 5.0,
}

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

@ -0,0 +1,120 @@
import time
import boto3
from botocore.exceptions import ClientError
region = 'eu-central-1'
availabilityZone = 'eu-central-1b'
imageId = 'ami-0cc293023f983ed53'
instanceType = 't3.nano'
keyName = 'srieger-pub'
subnet1 = 'subnet-41422b28'
subnet2 = 'subnet-5c5f6d16'
subnet3 = 'subnet-6f2ea214'
userDataDB = ('#!/bin/bash\n'
'#!/bin/bash\n'
'# extra repo for RedHat rpms\n'
'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n'
'# essential tools\n'
'yum install -y joe htop git\n'
'# mysql\n'
'yum install -y mariadb mariadb-server\n'
'\n'
'service mariadb start\n'
'\n'
'echo "create database cloud_tug_of_war" | mysql -u root\n'
'\n'
'echo "create table clouds ( cloud_id INT AUTO_INCREMENT, name VARCHAR(255) NOT NULL, value INT, max_value INT, PRIMARY KEY (cloud_id))" | mysql -u root cloud_tug_of_war\n'
'\n'
'echo "CREATE USER \'cloud_tug_of_war\'@\'%\' IDENTIFIED BY \'cloudpass\';" | mysql -u root\n'
'echo "GRANT ALL PRIVILEGES ON cloud_tug_of_war.* TO \'cloud_tug_of_war\'@\'%\';" | mysql -u root\n'
'echo "FLUSH PRIVILEGES" | mysql -u root\n'
)
# convert with: cat install-mysql | sed "s/^/'/; s/$/\\\n'/"
client = boto3.setup_default_session(region_name=region)
ec2Client = boto3.client("ec2")
ec2Resource = boto3.resource('ec2')
response = ec2Client.describe_vpcs()
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
elbv2Client = boto3.client('elbv2')
asClient = boto3.client('autoscaling')
print("Deleting auto scaling group...")
print("------------------------------------")
try:
response = asClient.delete_auto_scaling_group(AutoScalingGroupName='tug-of-war-asg-autoscalinggroup', ForceDelete=True)
except ClientError as e:
print(e)
print("Deleting launch configuration...")
print("------------------------------------")
try:
response = asClient.delete_launch_configuration(LaunchConfigurationName='tug-of-war-asg-launchconfig')
except ClientError as e:
print(e)
print("Deleting old instances...")
print("------------------------------------")
response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war-asg']}])
print(response)
reservations = response['Reservations']
for reservation in reservations:
for instance in reservation['Instances']:
if instance['State']['Name'] == "running":
response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']])
print(response)
instanceToTerminate = ec2Resource.Instance(instance['InstanceId'])
instanceToTerminate.wait_until_terminated()
print("Deleting load balancer and deps...")
print("------------------------------------")
try:
response = elbv2Client.describe_load_balancers(Names=['tug-of-war-asg-loadbalancer'])
loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '')
response = elbv2Client.delete_load_balancer(LoadBalancerArn=loadbalancer_arn)
waiter = elbv2Client.get_waiter('load_balancers_deleted')
waiter.wait(LoadBalancerArns=[loadbalancer_arn])
except ClientError as e:
print(e)
try:
response = elbv2Client.describe_target_groups(Names=['tug-of-war-asg-targetgroup'])
while len(response.get('TargetGroups', [{}])) > 0:
targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
try:
response = elbv2Client.delete_target_group(TargetGroupArn=targetgroup_arn)
except ClientError as e:
print(e)
response = elbv2Client.describe_target_groups(Names=['tug-of-war-asg-targetgroup'])
time.sleep(5)
except ClientError as e:
print(e)
print("Delete old security group...")
print("------------------------------------")
try:
response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war-asg']}])
while len(response.get('SecurityGroups', [{}])) > 0:
security_group_id = response.get('SecurityGroups', [{}])[0].get('GroupId', '')
try:
response = ec2Client.delete_security_group(GroupName='tug-of-war-asg')
except ClientError as e:
print(e)
response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war-asg']}])
time.sleep(5)
except ClientError as e:
print(e)
Loading…
Cancel
Save