added code section comments, added config param for VPC to support starting in envs with multiple vpcs, added output for lb dns
This commit is contained in:
		@@ -3,44 +3,41 @@ import time
 | 
				
			|||||||
import boto3
 | 
					import boto3
 | 
				
			||||||
from botocore.exceptions import ClientError
 | 
					from botocore.exceptions import ClientError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					################################################################################################
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# Configuration Parameters
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					################################################################################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
region = 'eu-central-1'
 | 
					region = 'eu-central-1'
 | 
				
			||||||
availabilityZone = 'eu-central-1b'
 | 
					availabilityZone = 'eu-central-1b'
 | 
				
			||||||
imageId = 'ami-0cc293023f983ed53'
 | 
					imageId = 'ami-0cc293023f983ed53'
 | 
				
			||||||
instanceType = 't3.nano'
 | 
					instanceType = 't3.nano'
 | 
				
			||||||
keyName = 'srieger-pub'
 | 
					keyName = 'srieger-pub'
 | 
				
			||||||
 | 
					vpc_id = 'vpc-eedd4187'
 | 
				
			||||||
subnet1 = 'subnet-41422b28'
 | 
					subnet1 = 'subnet-41422b28'
 | 
				
			||||||
subnet2 = 'subnet-5c5f6d16'
 | 
					subnet2 = 'subnet-5c5f6d16'
 | 
				
			||||||
subnet3 = 'subnet-6f2ea214'
 | 
					subnet3 = 'subnet-6f2ea214'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
userDataDB = ('#!/bin/bash\n'
 | 
					# if you only have one VPC, vpc_id can be retrieved using:
 | 
				
			||||||
              '#!/bin/bash\n'
 | 
					#
 | 
				
			||||||
              '# extra repo for RedHat rpms\n'
 | 
					# response = ec2Client.describe_vpcs()
 | 
				
			||||||
              'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n'
 | 
					# vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
 | 
				
			||||||
              '# essential tools\n'
 | 
					
 | 
				
			||||||
              'yum install -y joe htop git\n'
 | 
					
 | 
				
			||||||
              '# mysql\n'
 | 
					################################################################################################
 | 
				
			||||||
              'yum install -y mariadb mariadb-server\n'
 | 
					#
 | 
				
			||||||
              '\n'
 | 
					# boto3 code
 | 
				
			||||||
              '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)
 | 
					client = boto3.setup_default_session(region_name=region)
 | 
				
			||||||
ec2Client = boto3.client("ec2")
 | 
					ec2Client = boto3.client("ec2")
 | 
				
			||||||
ec2Resource = boto3.resource('ec2')
 | 
					ec2Resource = boto3.resource('ec2')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
response = ec2Client.describe_vpcs()
 | 
					 | 
				
			||||||
vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
elbv2Client = boto3.client('elbv2')
 | 
					elbv2Client = boto3.client('elbv2')
 | 
				
			||||||
asClient = boto3.client('autoscaling')
 | 
					asClient = boto3.client('autoscaling')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -156,6 +153,27 @@ except ClientError as e:
 | 
				
			|||||||
print("Running new DB instance...")
 | 
					print("Running new DB instance...")
 | 
				
			||||||
print("------------------------------------")
 | 
					print("------------------------------------")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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 user-data from script with: cat install-mysql | sed "s/^/'/; s/$/\\\n'/"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
response = ec2Client.run_instances(
 | 
					response = ec2Client.run_instances(
 | 
				
			||||||
    ImageId=imageId,
 | 
					    ImageId=imageId,
 | 
				
			||||||
    InstanceType=instanceType,
 | 
					    InstanceType=instanceType,
 | 
				
			||||||
@@ -319,3 +337,5 @@ response = asClient.put_scaling_policy(
 | 
				
			|||||||
        'TargetValue': 5.0,
 | 
					        'TargetValue': 5.0,
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print('Load Balancer should be reachable at: ' + loadbalancer_dns)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,36 +3,23 @@ import time
 | 
				
			|||||||
import boto3
 | 
					import boto3
 | 
				
			||||||
from botocore.exceptions import ClientError
 | 
					from botocore.exceptions import ClientError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					################################################################################################
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# Configuration Parameters
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					################################################################################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
region = 'eu-central-1'
 | 
					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'/"
 | 
					################################################################################################
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					# boto3 code
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					################################################################################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
client = boto3.setup_default_session(region_name=region)
 | 
					client = boto3.setup_default_session(region_name=region)
 | 
				
			||||||
ec2Client = boto3.client("ec2")
 | 
					ec2Client = boto3.client("ec2")
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user