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.

107 lines
3.0 KiB

  1. import boto3
  2. ################################################################################################
  3. #
  4. # Configuration Parameters
  5. #
  6. ################################################################################################
  7. region = 'eu-central-1'
  8. availabilityZone = 'eu-central-1b'
  9. vpc_id = 'vpc-eedd4187'
  10. subnet1 = 'subnet-41422b28'
  11. subnet2 = 'subnet-5c5f6d16'
  12. subnet3 = 'subnet-6f2ea214'
  13. # if you only have one VPC, vpc_id can be retrieved using:
  14. #
  15. # response = ec2Client.describe_vpcs()
  16. # vpc_id = response.get('Vpcs', [{}])[0].get('VpcId', '')
  17. ################################################################################################
  18. #
  19. # boto3 code
  20. #
  21. ################################################################################################
  22. client = boto3.setup_default_session(region_name=region)
  23. ec2Client = boto3.client("ec2")
  24. ec2Resource = boto3.resource('ec2')
  25. response = ec2Client.describe_security_groups(Filters=[{'Name': 'group-name', 'Values': ['tug-of-war']}])
  26. security_group_id = response.get('SecurityGroups', [{}])[0].get('GroupId', '')
  27. elbv2Client = boto3.client('elbv2')
  28. response = elbv2Client.create_load_balancer(
  29. Name='tug-of-war-loadbalancer',
  30. Subnets=[
  31. subnet1,
  32. subnet2,
  33. subnet3,
  34. ],
  35. SecurityGroups=[
  36. security_group_id
  37. ]
  38. )
  39. loadbalancer_arn = response.get('LoadBalancers', [{}])[0].get('LoadBalancerArn', '')
  40. loadbalancer_dns = response.get('LoadBalancers', [{}])[0].get('DNSName', '')
  41. response = elbv2Client.create_target_group(
  42. Name='tug-of-war-targetgroup',
  43. Port=80,
  44. Protocol='HTTP',
  45. VpcId=vpc_id,
  46. )
  47. targetgroup_arn = response.get('TargetGroups', [{}])[0].get('TargetGroupArn', '')
  48. response = elbv2Client.create_listener(
  49. DefaultActions=[
  50. {
  51. 'TargetGroupArn': targetgroup_arn,
  52. 'Type': 'forward',
  53. },
  54. ],
  55. LoadBalancerArn=loadbalancer_arn,
  56. Port=80,
  57. Protocol='HTTP',
  58. )
  59. response = elbv2Client.modify_target_group_attributes(
  60. TargetGroupArn=targetgroup_arn,
  61. Attributes=[
  62. {
  63. 'Key': 'stickiness.enabled',
  64. 'Value': 'true'
  65. },
  66. ]
  67. )
  68. print("Registering instances...")
  69. print("------------------------------------")
  70. response = ec2Client.describe_instances(Filters=[{'Name': 'tag:tug-of-war', 'Values': ['webserver']}])
  71. print(response)
  72. reservations = response['Reservations']
  73. for reservation in reservations:
  74. for instance in reservation['Instances']:
  75. if instance['State']['Name'] == "running" or instance['State']['Name'] == "pending":
  76. response = elbv2Client.register_targets(
  77. TargetGroupArn=targetgroup_arn,
  78. Targets=[
  79. {
  80. 'Id': instance['InstanceId'],
  81. },
  82. ],
  83. )
  84. print('Waiting for Load Balancer to become available...')
  85. waiter = elbv2Client.get_waiter('load_balancer_available')
  86. waiter.wait(LoadBalancerArns=[loadbalancer_arn])
  87. print('Load Balancer should be reachable at: ' + loadbalancer_dns)