-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_load_balancer.rb
More file actions
87 lines (68 loc) · 1.86 KB
/
create_load_balancer.rb
File metadata and controls
87 lines (68 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require File.expand_path(File.dirname(__FILE__) + '/config')
(vpc_id, load_balancer_name) = ARGV
unless vpc_id && load_balancer_name
puts "Usage: create_load_balancer <VPC_ID> <load_balancer_name>"
exit 1
end
ec2Client = Aws::EC2::Client::new
# Grab the private subnets. Note this assumes we know the subnets we
# are looking for via knowledge of how the VPC was created.
subnet_infos = ec2Client.describe_subnets({
:filters => [
{
:name => "vpc-id",
:values => [vpc_id]
},
{
:name => "cidr",
:values => ["10.0.0.0/24", "10.0.2.0/24"]
}
]
})[:subnets]
subnet_ids = subnet_infos.map do |subnet|
subnet[:subnet_id]
end
puts "subnet ids #{subnet_ids}"
# Need the security group subnet
sg_infos = ec2Client.describe_security_groups({
:filters => [
{
:name => "vpc-id",
:values => [vpc_id]
}
]
})[:security_groups]
load_balancer_sg = (sg_infos.select {
|sg| sg[:group_name] == "load_balancer_sg"
})
puts "load balancer sg array is #{load_balancer_sg}"
load_balancer_sg_ids = load_balancer_sg.map do |sg|
sg[:group_id]
end
elbClient = Aws::ElasticLoadBalancing::Client::new
# Create the load balancer
dns_name = elbClient.create_load_balancer({
:load_balancer_name => load_balancer_name,
:listeners => [
{
:protocol => "HTTP",
:load_balancer_port => 80,
:instance_protocol => "HTTP",
:instance_port => 9000
}
],
:subnets => subnet_ids,
:security_groups => load_balancer_sg_ids
})[:dns_name]
puts "Created load balancer with DNS name #{dns_name}"
#Add the health check
health_check = elbClient.configure_health_check({
:load_balancer_name => load_balancer_name,
:health_check => {
:target => "HTTP:9000/b2bnext-webapp/",
:interval => 30,
:timeout => 5,
:unhealthy_threshold => 2,
:healthy_threshold => 4
}
})[:health_check]