You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

402 lines
15 KiB

  1. import time
  2. import boto3
  3. from botocore.exceptions import ClientError
  4. ################################################################################################
  5. #
  6. # Configuration Parameters
  7. #
  8. ################################################################################################
  9. # print("!!!!!!!! You cannot use Auto Scaling Group in AWS Educate Account !!!!!!!!")
  10. # exit(-1)
  11. # place your credentials in ~/.aws/credentials, as mentioned in AWS Educate Classroom,
  12. # Account Details, AWC CLI -> Show (Copy and paste the following into ~/.aws/credentials)
  13. # changed to use us-east, to be able to use AWS Educate Classroom
  14. region = 'us-east-1'
  15. availabilityZone1 = 'us-east-1a'
  16. availabilityZone2 = 'us-east-1b'
  17. availabilityZone3 = 'us-east-1c'
  18. # region = 'eu-central-1'
  19. # availabilityZone = 'eu-central-1b'
  20. # AMI ID of Amazon Linux 2 image 64-bit x86 in us-east-1 (can be retrieved, e.g., at
  21. # https://console.aws.amazon.com/ec2/v2/home?region=us-east-1#LaunchInstanceWizard:)
  22. # TODO update to recent version of Amazon Linux 2 AMI?
  23. imageId = 'ami-0d5eff06f840b45e9'
  24. # for eu-central-1, AMI ID of Amazon Linux 2 would be:
  25. # imageId = 'ami-0cc293023f983ed53'
  26. # potentially change instanceType to t2.micro for "free tier" if using a regular account
  27. # for production, t3.nano seams better
  28. # as of SoSe 2022 t2.nano seams to be a bit too low on memory, mariadb first start can fail
  29. # due to innodb cache out of memory, therefore t2.micro or swap in t2.nano currently recommended
  30. # instanceType = 't2.nano'
  31. instanceType = 't2.micro'
  32. # keyName = 'srieger-pub'
  33. keyName = 'vockey'
  34. # see, e.g., AWS Academy Lab readme, or "aws iam list-instance-profiles | grep InstanceProfileName"
  35. # for roles see: "aws iam list-roles | grep RoleName"
  36. iamRole = 'LabInstanceProfile'
  37. ################################################################################################
  38. #
  39. # boto3 code
  40. #
  41. ################################################################################################
  42. client = boto3.setup_default_session(region_name=region)
  43. ec2Client = boto3.client("ec2")
  44. ec2Resource = boto3.resource('ec2')
  45. elbv2Client = boto3.client('elbv2')
  46. asClient = boto3.client('autoscaling')
  47. # if you only have one VPC, vpc_id can be retrieved using:
  48. response = ec2Client.describe_vpcs()
  49. vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
  50. # if you have more than one VPC, vpc_id should be specified, and code
  51. # top retrieve VPC id below needs to be commented out
  52. # vpc_id = 'vpc-eedd4187'
  53. subnet_id1 = ec2Client.describe_subnets(
  54. Filters=[
  55. {
  56. 'Name': 'availability-zone', 'Values': [availabilityZone1]
  57. }
  58. ])['Subnets'][0]['SubnetId']
  59. subnet_id2 = ec2Client.describe_subnets(
  60. Filters=[
  61. {
  62. 'Name': 'availability-zone', 'Values': [availabilityZone2]
  63. }
  64. ])['Subnets'][0]['SubnetId']
  65. subnet_id3 = ec2Client.describe_subnets(
  66. Filters=[
  67. {
  68. 'Name': 'availability-zone', 'Values': [availabilityZone3]
  69. }
  70. ])['Subnets'][0]['SubnetId']
  71. print("Deleting old auto scaling group...")
  72. print("------------------------------------")
  73. try:
  74. response = asClient.delete_auto_scaling_group(AutoScalingGroupName='tug-of-war-asg-autoscalinggroup', ForceDelete=True)
  75. except ClientError as e:
  76. print(e)
  77. print("Deleting old launch configuration...")
  78. print("------------------------------------")
  79. try:
  80. response = asClient.delete_launch_configuration(LaunchConfigurationName='tug-of-war-asg-launchconfig')
  81. except ClientError as e:
  82. print(e)
  83. print("Deleting old instances...")
  84. print("------------------------------------")
  85. response = ec2Client.describe_instances(Filters=[{'Name': 'tag-key', 'Values': ['tug-of-war-asg']}])
  86. print(response)
  87. reservations = response['Reservations']
  88. for reservation in reservations:
  89. for instance in reservation['Instances']:
  90. if instance['State']['Name'] == "running":
  91. response = ec2Client.terminate_instances(InstanceIds=[instance['InstanceId']])
  92. print(response)
  93. instanceToTerminate = ec2Resource.Instance(instance['InstanceId'])
  94. instanceToTerminate.wait_until_terminated()
  95. print("Deleting old load balancer and deps...")
  96. print("------------------------------------")
  97. try:
  98. response = elbv2Client.describe_load_balancers(Names=['tug-of-war-asg-loadbalancer'])
  99. loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '')
  100. response = elbv2Client.delete_load_balancer(LoadBalancerArn=loadbalancer_arn)
  101. waiter = elbv2Client.get_waiter('load_balancers_deleted')
  102. waiter.wait(LoadBalancerArns=[loadbalancer_arn])
  103. except ClientError as e:
  104. print(e)
  105. try:
  106. response = elbv2Client.describe_target_groups(Names=['tug-of-war-asg-targetgroup'])
  107. while len(response.get('TargetGroups', [{}])) > 0:
  108. targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
  109. try:
  110. response = elbv2Client.delete_target_group(TargetGroupArn=targetgroup_arn)
  111. except ClientError as e:
  112. print(e)
  113. response = elbv2Client.describe_target_groups(Names=['tug-of-war-asg-targetgroup'])
  114. time.sleep(5)
  115. except ClientError as e:
  116. print(e)
  117. print("Delete old security group...")
  118. print("------------------------------------")
  119. try:
  120. response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war-asg']}])
  121. while len(response.get('SecurityGroups', [{}])) > 0:
  122. security_group_id = response.get('SecurityGroups', [{}])[0].get('GroupId', '')
  123. try:
  124. response = ec2Client.delete_security_group(GroupName='tug-of-war-asg')
  125. except ClientError as e:
  126. print(e)
  127. response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war-asg']}])
  128. time.sleep(5)
  129. except ClientError as e:
  130. print(e)
  131. print("Create security group...")
  132. print("------------------------------------")
  133. try:
  134. response = ec2Client.create_security_group(GroupName='tug-of-war-asg',
  135. Description='tug-of-war-asg',
  136. VpcId=vpc_id)
  137. security_group_id = response['GroupId']
  138. print('Security Group Created %s in vpc %s.' % (security_group_id, vpc_id))
  139. data = ec2Client.authorize_security_group_ingress(
  140. GroupId=security_group_id,
  141. IpPermissions=[
  142. {'IpProtocol': 'tcp',
  143. 'FromPort': 3306,
  144. 'ToPort': 3306,
  145. 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]},
  146. {'IpProtocol': 'tcp',
  147. 'FromPort': 22,
  148. 'ToPort': 22,
  149. 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]},
  150. {'IpProtocol': 'tcp',
  151. 'FromPort': 80,
  152. 'ToPort': 80,
  153. 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]},
  154. {'IpProtocol': 'tcp',
  155. 'FromPort': 443,
  156. 'ToPort': 443,
  157. 'IpRanges': [{'CidrIp': '0.0.0.0/0'}]}
  158. ])
  159. print('Ingress Successfully Set %s' % data)
  160. except ClientError as e:
  161. print(e)
  162. print("Running new DB instance...")
  163. print("------------------------------------")
  164. userDataDB = ('#!/bin/bash\n'
  165. '# extra repo for RedHat rpms\n'
  166. 'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n'
  167. '# essential tools\n'
  168. 'yum install -y joe htop git\n'
  169. '# mysql\n'
  170. 'yum install -y mariadb mariadb-server\n'
  171. '\n'
  172. 'service mariadb start\n'
  173. '\n'
  174. 'echo "create database cloud_tug_of_war" | mysql -u root\n'
  175. '\n'
  176. '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'
  177. '\n'
  178. 'echo "CREATE USER \'cloud_tug_of_war\'@\'%\' IDENTIFIED BY \'cloudpass\';" | mysql -u root\n'
  179. 'echo "GRANT ALL PRIVILEGES ON cloud_tug_of_war.* TO \'cloud_tug_of_war\'@\'%\';" | mysql -u root\n'
  180. 'echo "FLUSH PRIVILEGES" | mysql -u root\n'
  181. )
  182. # convert user-data from script with: cat install-mysql | sed "s/^/'/; s/$/\\\n'/"
  183. response = ec2Client.run_instances(
  184. ImageId=imageId,
  185. InstanceType=instanceType,
  186. Placement={'AvailabilityZone': availabilityZone1, },
  187. KeyName=keyName,
  188. MinCount=1,
  189. MaxCount=1,
  190. UserData=userDataDB,
  191. SecurityGroupIds=[
  192. security_group_id,
  193. ],
  194. TagSpecifications=[
  195. {
  196. 'ResourceType': 'instance',
  197. 'Tags': [
  198. {'Key': 'Name', 'Value': 'tug-of-war-asg-db1'},
  199. {'Key': 'tug-of-war-asg', 'Value': 'db'}
  200. ],
  201. }
  202. ],
  203. )
  204. instanceIdDB = response['Instances'][0]['InstanceId']
  205. privateIpDB = response['Instances'][0]['PrivateIpAddress']
  206. # privateIpDB = response['Instances'][0]['NetworkInterfaces'][0]['NetworkInterfaceId']
  207. instance = ec2Resource.Instance(instanceIdDB)
  208. instance.wait_until_running()
  209. print(instanceIdDB)
  210. userDataWebServer = ('#!/bin/bash\n'
  211. '# extra repo for RedHat rpms\n'
  212. 'yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm\n'
  213. '# essential tools\n'
  214. 'yum install -y joe htop git\n'
  215. '# mysql\n'
  216. 'yum install -y httpd php php-mysql\n'
  217. '\n'
  218. 'service httpd start\n'
  219. '\n'
  220. # 'wget http://mmnet.informatik.hs-fulda.de/cloudcomp/tug-of-war-in-the-clouds.tar.gz\n'
  221. # 'cp tug-of-war-in-the-clouds.tar.gz /var/www/html/\n'
  222. # 'tar zxvf tug-of-war-in-the-clouds.tar.gz\n'
  223. 'cd /var/www/html\n'
  224. 'wget https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/example-projects/tug-of-war-in-the-clouds/web-content/index.php\n'
  225. 'wget https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/example-projects/tug-of-war-in-the-clouds/web-content/cloud.php\n'
  226. 'wget https://gogs.informatik.hs-fulda.de/srieger/cloud-computing-msc-ai-examples/raw/master/example-projects/tug-of-war-in-the-clouds/web-content/config.php\n'
  227. '\n'
  228. '# change hostname of db connection\n'
  229. 'sed -i s/localhost/' + privateIpDB + '/g /var/www/html/config.php\n'
  230. )
  231. print("Creating launch configuration...")
  232. print("------------------------------------")
  233. response = asClient.create_launch_configuration(
  234. #IamInstanceProfile='my-iam-role',
  235. IamInstanceProfile=iamRole,
  236. ImageId=imageId,
  237. InstanceType=instanceType,
  238. LaunchConfigurationName='tug-of-war-asg-launchconfig',
  239. UserData=userDataWebServer,
  240. KeyName=keyName,
  241. SecurityGroups=[
  242. security_group_id,
  243. ],
  244. )
  245. elbv2Client = boto3.client('elbv2')
  246. print("Creating load balancer...")
  247. print("------------------------------------")
  248. response = elbv2Client.create_load_balancer(
  249. Name='tug-of-war-asg-loadbalancer',
  250. Subnets=[
  251. subnet_id1,
  252. subnet_id2,
  253. subnet_id3,
  254. ],
  255. SecurityGroups=[
  256. security_group_id
  257. ]
  258. )
  259. loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '')
  260. loadbalancer_dns = response.get('LoadBalancers', [{}])[0].get('DNSName', '')
  261. print("Creating target group...")
  262. print("------------------------------------")
  263. response = elbv2Client.create_target_group(
  264. Name='tug-of-war-asg-targetgroup',
  265. Port=80,
  266. Protocol='HTTP',
  267. VpcId=vpc_id,
  268. )
  269. targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
  270. print("Creating listener...")
  271. print("------------------------------------")
  272. response = elbv2Client.create_listener(
  273. DefaultActions=[
  274. {
  275. 'TargetGroupArn': targetgroup_arn,
  276. 'Type': 'forward',
  277. },
  278. ],
  279. LoadBalancerArn=loadbalancer_arn,
  280. Port=80,
  281. Protocol='HTTP',
  282. )
  283. response = elbv2Client.modify_target_group_attributes(
  284. TargetGroupArn=targetgroup_arn,
  285. Attributes=[
  286. {
  287. 'Key': 'stickiness.enabled',
  288. 'Value': 'true'
  289. },
  290. ]
  291. )
  292. print("Creating auto scaling group...")
  293. print("------------------------------------")
  294. response = asClient.create_auto_scaling_group(
  295. AutoScalingGroupName='tug-of-war-asg-autoscalinggroup',
  296. LaunchConfigurationName='tug-of-war-asg-launchconfig',
  297. MaxSize=3,
  298. MinSize=1,
  299. HealthCheckGracePeriod=120,
  300. HealthCheckType='ELB',
  301. TargetGroupARNs=[
  302. targetgroup_arn,
  303. ],
  304. VPCZoneIdentifier=subnet_id1 + ', ' + ', ' + subnet_id2 + ', ' + subnet_id3,
  305. Tags=[
  306. {'Key': 'Name', 'Value': 'tug-of-war-asg-webserver', 'PropagateAtLaunch': True},
  307. {'Key': 'tug-of-war', 'Value': 'webserver', 'PropagateAtLaunch': True}
  308. ],
  309. )
  310. print(loadbalancer_arn)
  311. print(targetgroup_arn)
  312. print('app/tug-of-war-asg-loadbalancer/'+str(loadbalancer_arn).split('/')[3]+'/targetgroup/tug-of-war-asg-targetgroup/'+str(targetgroup_arn).split('/')[2])
  313. print('If target group is not found, creation was delayed in AWS Academy lab, need to add a check that target group is'
  314. 'existing before executing the next lines in the future... If the error occurs, rerun script...')
  315. response = asClient.put_scaling_policy(
  316. AutoScalingGroupName='tug-of-war-asg-autoscalinggroup',
  317. PolicyName='tug-of-war-asg-scalingpolicy',
  318. PolicyType='TargetTrackingScaling',
  319. EstimatedInstanceWarmup=30,
  320. TargetTrackingConfiguration={
  321. 'PredefinedMetricSpecification': {
  322. 'PredefinedMetricType': 'ALBRequestCountPerTarget',
  323. 'ResourceLabel': 'app/tug-of-war-asg-loadbalancer/'+str(loadbalancer_arn).split('/')[3]+'/targetgroup/tug-of-war-asg-targetgroup/'+str(targetgroup_arn).split('/')[2]
  324. },
  325. 'TargetValue': 5.0,
  326. }
  327. )
  328. print('Load Balancer should be reachable at: http://' + loadbalancer_dns)
  329. print('As always, you need to wait some time, until load balancer is provisioned, instances are healthy (cloud-init '
  330. 'did its job as specified in the launch configuration). ')
  331. print('You can use "aws elbv2 ..." commands or the web console to examine the current state. Take a look at Load'
  332. 'Balancer, Target Group, Auto Scaling Group and esp. Monitoring of the Load Balancer and related Cloud Watch'
  333. 'alarms.')
  334. print('If you "pull" a lot of clouds in the game, generating a lot of requests, you will see the alarm being fired and'
  335. 'further instances started (scale-out) (involves some clicking for about three minutes). After 15 min of idling,'
  336. 'instances will automatically be stopped (scale-in).')