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.

109 lines
3.0 KiB

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