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.

70 lines
1.9 KiB

  1. import boto3
  2. from botocore.exceptions import ClientError
  3. region = 'eu-central-1'
  4. availabilityZone = 'eu-central-1b'
  5. subnet1 = 'subnet-41422b28'
  6. subnet2 = 'subnet-5c5f6d16'
  7. subnet3 = 'subnet-6f2ea214'
  8. client = boto3.setup_default_session(region_name=region)
  9. ec2Client = boto3.client("ec2")
  10. ec2Resource = boto3.resource('ec2')
  11. response = ec2Client.describe_vpcs()
  12. vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
  13. elbv2Client = boto3.client('elbv2')
  14. for i in range(3, 4):
  15. print("Running new Web Server instance...")
  16. print("------------------------------------")
  17. response = ec2Client.run_instances(
  18. ImageId=imageId,
  19. InstanceType=instanceType,
  20. Placement={'AvailabilityZone': availabilityZone, },
  21. KeyName=keyName,
  22. MinCount=1,
  23. MaxCount=1,
  24. UserData=userDataWebServer,
  25. SecurityGroupIds=[
  26. security_group_id,
  27. ],
  28. TagSpecifications=[
  29. {
  30. 'ResourceType': 'instance',
  31. 'Tags': [
  32. {'Key': 'Name', 'Value': 'tug-of-war-webserver1'},
  33. {'Key': 'tug-of-war', 'Value': 'webserver'}
  34. ],
  35. }
  36. ],
  37. )
  38. instanceIdWeb = response['Instances'][0]['InstanceId']
  39. instance = ec2Resource.Instance(instanceIdWeb)
  40. instance.wait_until_running()
  41. instance.load()
  42. print("tug-of-war-in-the-clouds can be accessed at: " + instance.public_ip_address)
  43. try:
  44. response = elbv2Client.describe_target_groups(Names=['tug-of-war-targetgroup'])
  45. targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
  46. except ClientError as e:
  47. print(e)
  48. print("Registering instance...")
  49. print("------------------------------------")
  50. response = elbv2Client.register_targets(
  51. TargetGroupArn=targetgroup_arn,
  52. Targets=[
  53. {
  54. 'Id': instanceIdWeb,
  55. },
  56. ],
  57. )