-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain.tf
More file actions
277 lines (243 loc) · 8.5 KB
/
main.tf
File metadata and controls
277 lines (243 loc) · 8.5 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
resource "random_string" "autogenerated_password" {
length = 16
special = false
}
resource "aws_ssm_parameter" "rabbit_password" {
count = !var.create_aws_activemq && var.create_aws_ec2_rabbitmq ? 1: 0
name = "/${var.environment_name}/rabbit/PASSWORD"
description = "Rabbit Password"
type = "SecureString"
value = random_string.autogenerated_password.result
}
resource "aws_ssm_parameter" "rabbit_username" {
count = !var.create_aws_activemq && var.create_aws_ec2_rabbitmq ? 1: 0
name = "/${var.environment_name}/rabbit/USERNAME"
description = "Rabbit Username"
type = "String"
value = "admin"
}
resource "aws_ssm_parameter" "rabbit_endpoint" {
count = !var.create_aws_activemq && var.create_aws_ec2_rabbitmq ? 1: 0
name = "/${var.environment_name}/rabbit/ENDPOINT"
description = "Rabbit Endpoint"
type = "String"
value = "${aws_instance.ec2_rabbitmq_master[0].private_ip}"
depends_on = [
aws_instance.ec2_rabbitmq_master
]
}
resource "aws_ssm_parameter" "active_password" {
count = var.create_aws_activemq && !var.create_aws_ec2_rabbitmq ? 1 : 0
name = "/${var.environment_name}/activemq/PASSWORD"
description = "Activemq Password"
type = "SecureString"
value = random_string.autogenerated_password.result
}
resource "aws_ssm_parameter" "activemq_username" {
count = var.create_aws_activemq && !var.create_aws_ec2_rabbitmq ? 1 : 0
name = "/${var.environment_name}/activemq/USERNAME"
description = "Rabbit Username"
type = "String"
value = "admin"
}
resource "aws_ssm_parameter" "activemq_endpoint" {
count = var.create_aws_activemq && !var.create_aws_ec2_rabbitmq ? 1 : 0
name = "/${var.environment_name}/activemq/ENDPOINT"
description = "Activemq Endpoint"
type = "String"
value = "${aws_mq_broker.activemq[0].instances[0].console_url}"
}
resource "aws_mq_broker" "activemq" {
count = var.create_aws_activemq && !var.create_aws_ec2_rabbitmq ? 1 : 0
broker_name = "${var.project_name_prefix}-Activemq"
engine_type = var.engine_type
engine_version = var.engine_version
storage_type = var.storage_type
auto_minor_version_upgrade = var.auto_minor_version_upgrade
apply_immediately = var.apply_immediately
host_instance_type = var.host_instance_type
security_groups = ["${aws_security_group.rabbit_sg.id}"]
deployment_mode = var.deployment_mode
subnet_ids = var.subnet_ids
publicly_accessible = var.publicly_accessible
logs {
audit = var.audit_logs
general = var.general_logs
}
user {
username = var.activemq_username
password = var.activemq_password != "" ? var.activemq_password : random_string.autogenerated_password.result
console_access = var.console_access
}
}
data "aws_ami" "amazon-linux-2" {
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn2-ami-hvm*"]
}
owners = ["amazon"]
}
data "template_file" "user_data" {
template = file("${path.module}/user_data.sh")
vars = {
environment_name = var.environment_name
region = var.region
}
}
resource "aws_instance" "ec2_rabbitmq_master" {
count = !var.create_aws_activemq && var.create_aws_ec2_rabbitmq ? var.master: 0
ami = var.ami_id == "" ? data.aws_ami.amazon-linux-2.id : var.ami_id
instance_type = var.instance_type
subnet_id = var.ec2_subnet_id
vpc_security_group_ids = ["${aws_security_group.rabbit_sg.id}"]
key_name = var.key_name
iam_instance_profile = "${aws_iam_instance_profile.rabbit-instance-profile.name}"
ebs_optimized = var.ebs_optimized
disable_api_termination = var.disable_api_termination
user_data = data.template_file.user_data.rendered
source_dest_check = var.source_dest_check
disable_api_stop = var.disable_api_stop
volume_tags = merge(var.common_tags, tomap({ "Name" : "${var.project_name_prefix}-Rabbit-MQ-Master" }))
tags = merge(var.common_tags, tomap({ "Name" : "${var.project_name_prefix}-Rabbit-MQ-Master"}))
root_block_device {
delete_on_termination = var.delete_on_termination
encrypted = var.encrypted
kms_key_id = var.kms_key_id
volume_size = var.root_volume_size
volume_type = var.volume_type
}
depends_on = [
aws_ssm_parameter.rabbit_password
]
}
data "template_file" "user_data_worker" {
template = file("${path.module}/worker.sh")
vars = {
environment_name = var.environment_name
region = var.region
Name = "${var.project_name_prefix}-Rabbit-MQ-Master"
}
}
resource "aws_instance" "ec2_rabbitmq_worker" {
count = !var.create_aws_activemq && var.create_aws_ec2_rabbitmq ? var.worker: 0
ami = data.aws_ami.amazon-linux-2.id
instance_type = var.instance_type
subnet_id = var.ec2_subnet_id
vpc_security_group_ids = ["${aws_security_group.rabbit_sg.id}"]
key_name = var.key_name
iam_instance_profile = "${aws_iam_instance_profile.rabbit-instance-profile.name}"
ebs_optimized = var.ebs_optimized
disable_api_termination = var.disable_api_termination
user_data = data.template_file.user_data_worker.rendered
source_dest_check = var.source_dest_check
disable_api_stop = var.disable_api_stop
volume_tags = merge(var.common_tags, tomap({ "Name" : "${var.project_name_prefix}-Rabbit-MQ-worker" }))
tags = merge(var.common_tags, tomap({ "Name" : "${var.project_name_prefix}-Rabbit-MQ-worker"}))
root_block_device {
delete_on_termination = var.delete_on_termination
encrypted = var.encrypted
kms_key_id = var.kms_key_id
volume_size = var.root_volume_size
volume_type = var.volume_type
}
depends_on = [
aws_instance.ec2_rabbitmq_master
]
}
###security group for rabbit
resource "aws_security_group" "rabbit_sg" {
name = "${var.rabbit_sg_name}"
vpc_id = "${var.vpc_id}"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["${var.vpc_cidr_block}"]
}
ingress {
from_port = 15672
to_port = 15672
protocol = "tcp"
cidr_blocks = ["${var.vpc_cidr_block}"]
}
ingress {
from_port = 5672
to_port = 5672
protocol = "tcp"
cidr_blocks = ["${var.vpc_cidr_block}"]
}
ingress {
from_port = 8162
to_port = 8162
protocol = "tcp"
cidr_blocks = ["${var.vpc_cidr_block}"]
}
ingress {
from_port = 0
to_port = 0
protocol = "-1"
self = true
}
ingress {
from_port = -1
to_port = -1
protocol = "icmp"
cidr_blocks = ["${var.vpc_cidr_block}"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.rabbit_sg_name}"
}
}
###IAM policy for rabbit instance
data "aws_iam_policy_document" "instance-assume-role-policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "rabbit-role" {
name = "${var.environment_name}-${var.region}-rabbit_role"
path = "/system/"
assume_role_policy = "${data.aws_iam_policy_document.instance-assume-role-policy.json}"
managed_policy_arns = ["arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM"]
}
resource "aws_iam_instance_profile" "rabbit-instance-profile" {
name = "${var.environment_name}-${var.region}-rabbit-instance-profile"
role = "${aws_iam_role.rabbit-role.name}"
}
resource "aws_iam_role_policy" "ec2-describe-instance-policy" {
name = "${var.environment_name}-${var.region}-ec2-describe-instance-policy"
role = "${aws_iam_role.rabbit-role.id}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances",
"ec2:DescribeTags",
"ssm:GetParameterHistory",
"ssm:GetParameters",
"ssm:GetParameter"
],
"Resource": "*"
}
]
}
EOF
}