@ -10,21 +10,31 @@ from botocore.exceptions import ClientError
#
#
################################################################################################
################################################################################################
print ( " !!!!!!!! You cannot use Auto Scaling Group in AWS Educate Account !!!!!!!! " )
exit ( - 1 )
# 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)
# 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 '
region = ' eu-central-1 '
availabilityZone = ' eu-central-1b '
imageId = ' ami-0cc293023f983ed53 '
instanceType = ' t3.nano '
keyName = ' srieger-pub '
keyName = ' srieger-pub '
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:
#
# response = ec2Client.describe_vpcs()
# vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
################################################################################################
################################################################################################
@ -41,6 +51,35 @@ ec2Resource = boto3.resource('ec2')
elbv2Client = boto3 . client ( ' elbv2 ' )
elbv2Client = boto3 . client ( ' elbv2 ' )
asClient = boto3 . client ( ' autoscaling ' )
asClient = boto3 . client ( ' autoscaling ' )
# 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 ' ]
print ( " Deleting auto scaling group... " )
print ( " Deleting auto scaling group... " )
print ( " ------------------------------------ " )
print ( " ------------------------------------ " )
@ -176,7 +215,7 @@ userDataDB = ('#!/bin/bash\n'
response = ec2Client . run_instances (
response = ec2Client . run_instances (
ImageId = imageId ,
ImageId = imageId ,
InstanceType = instanceType ,
InstanceType = instanceType ,
Placement = { ' AvailabilityZone ' : availabilityZone , } ,
Placement = { ' AvailabilityZone ' : availabilityZone1 , } ,
KeyName = keyName ,
KeyName = keyName ,
MinCount = 1 ,
MinCount = 1 ,
MaxCount = 1 ,
MaxCount = 1 ,
@ -249,9 +288,9 @@ print("------------------------------------")
response = elbv2Client . create_load_balancer (
response = elbv2Client . create_load_balancer (
Name = ' tug-of-war-asg-loadbalancer ' ,
Name = ' tug-of-war-asg-loadbalancer ' ,
Subnets = [
Subnets = [
subnet1 ,
subnet2 ,
subnet3 ,
subnet_id 1 ,
subnet_id 2 ,
subnet_id 3 ,
] ,
] ,
SecurityGroups = [
SecurityGroups = [
security_group_id
security_group_id
@ -311,7 +350,7 @@ response = asClient.create_auto_scaling_group(
TargetGroupARNs = [
TargetGroupARNs = [
targetgroup_arn ,
targetgroup_arn ,
] ,
] ,
VPCZoneIdentifier = subnet1 + ' , ' + ' , ' + subnet2 + ' , ' + subnet3 ,
VPCZoneIdentifier = subnet_id 1 + ' , ' + ' , ' + subnet_id 2 + ' , ' + subnet_id 3 ,
Tags = [
Tags = [
{ ' Key ' : ' Name ' , ' Value ' : ' tug-of-war-asg-webserver ' , ' PropagateAtLaunch ' : True } ,
{ ' Key ' : ' Name ' , ' Value ' : ' tug-of-war-asg-webserver ' , ' PropagateAtLaunch ' : True } ,
{ ' Key ' : ' tug-of-war ' , ' Value ' : ' webserver ' , ' PropagateAtLaunch ' : True }
{ ' Key ' : ' tug-of-war ' , ' Value ' : ' webserver ' , ' PropagateAtLaunch ' : True }