-
Notifications
You must be signed in to change notification settings - Fork 83
/
main.tf
226 lines (189 loc) · 4.95 KB
/
main.tf
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
data "aws_vpc" "vpc" {
id = var.vpc_id
}
data "aws_region" "current" {
}
data "aws_ami_ids" "ami" {
owners = ["amazon"]
filter {
name = "name"
values = ["amzn-ami-hvm-2017*-gp2"]
}
}
locals {
cluster_name = "rabbitmq-${var.name}"
}
resource "random_string" "admin_password" {
length = 32
special = false
}
resource "random_string" "rabbit_password" {
length = 32
special = false
}
resource "random_string" "secret_cookie" {
length = 64
special = false
}
data "aws_iam_policy_document" "policy_doc" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
data "template_file" "cloud-init" {
template = file("${path.module}/cloud-init.yaml")
vars = {
sync_node_count = 3
asg_name = local.cluster_name
region = data.aws_region.current.name
admin_password = random_string.admin_password.result
rabbit_password = random_string.rabbit_password.result
secret_cookie = random_string.secret_cookie.result
message_timeout = 3 * 24 * 60 * 60 * 1000 # 3 days
}
}
resource "aws_iam_role" "role" {
name = local.cluster_name
assume_role_policy = data.aws_iam_policy_document.policy_doc.json
}
resource "aws_iam_role_policy" "policy" {
name = local.cluster_name
role = aws_iam_role.role.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"autoscaling:DescribeAutoScalingInstances",
"ec2:DescribeInstances"
],
"Resource": [
"*"
]
}
]
}
EOF
}
resource "aws_iam_instance_profile" "profile" {
name_prefix = local.cluster_name
role = aws_iam_role.role.name
}
resource "aws_security_group" "rabbitmq_elb" {
name = "rabbitmq_elb-${var.name}"
vpc_id = var.vpc_id
description = "Security Group for the rabbitmq elb"
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "rabbitmq ${var.name} ELB"
}
}
resource "aws_security_group" "rabbitmq_nodes" {
name = "${local.cluster_name}-nodes"
vpc_id = var.vpc_id
description = "Security Group for the rabbitmq nodes"
ingress {
protocol = -1
from_port = 0
to_port = 0
self = true
}
ingress {
protocol = "tcp"
from_port = 5672
to_port = 5672
security_groups = [aws_security_group.rabbitmq_elb.id]
}
ingress {
protocol = "tcp"
from_port = 15672
to_port = 15672
security_groups = [aws_security_group.rabbitmq_elb.id]
}
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = [
"0.0.0.0/0",
]
}
tags = {
Name = "rabbitmq ${var.name} nodes"
}
}
resource "aws_launch_configuration" "rabbitmq" {
name = local.cluster_name
image_id = data.aws_ami_ids.ami.ids[0]
instance_type = var.instance_type
key_name = var.ssh_key_name
security_groups = concat([aws_security_group.rabbitmq_nodes.id], var.nodes_additional_security_group_ids)
iam_instance_profile = aws_iam_instance_profile.profile.id
user_data = data.template_file.cloud-init.rendered
root_block_device {
volume_type = var.instance_volume_type
volume_size = var.instance_volume_size
iops = var.instance_volume_iops
delete_on_termination = true
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "rabbitmq" {
name = local.cluster_name
min_size = var.min_size
desired_capacity = var.desired_size
max_size = var.max_size
health_check_grace_period = 300
health_check_type = "ELB"
force_delete = true
launch_configuration = aws_launch_configuration.rabbitmq.name
load_balancers = [aws_elb.elb.name]
vpc_zone_identifier = var.subnet_ids
tag {
key = "Name"
value = local.cluster_name
propagate_at_launch = true
}
}
resource "aws_elb" "elb" {
name = "${local.cluster_name}-elb"
listener {
instance_port = 5672
instance_protocol = "tcp"
lb_port = 5672
lb_protocol = "tcp"
}
listener {
instance_port = 15672
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
health_check {
interval = 30
unhealthy_threshold = 10
healthy_threshold = 2
timeout = 3
target = "TCP:5672"
}
subnets = var.subnet_ids
idle_timeout = 3600
internal = true
security_groups = concat([aws_security_group.rabbitmq_elb.id], var.elb_additional_security_group_ids)
tags = {
Name = local.cluster_name
}
}