-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.tf
396 lines (319 loc) · 11.8 KB
/
aws.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
provider "aws" {
region = var.cloud_provider_region
}
# Conditional creation of data bucket
module "automq_byoc_data_bucket_name" {
source = "terraform-aws-modules/s3-bucket/aws"
version = "4.1.2"
create_bucket = var.automq_byoc_data_bucket_name == "" ? true : false
bucket = "automq-data-${var.automq_byoc_env_id}"
force_destroy = true
tags = {
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
# Conditional creation of ops bucket
module "automq_byoc_ops_bucket_name" {
source = "terraform-aws-modules/s3-bucket/aws"
version = "4.1.2"
create_bucket = var.automq_byoc_ops_bucket_name == "" ? true : false
bucket = "automq-ops-${var.automq_byoc_env_id}"
force_destroy = true
tags = {
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
data "aws_availability_zones" "available_azs" {}
module "automq_byoc_vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
count = var.create_new_vpc ? 1 : 0
cidr = "10.0.0.0/16"
name = "automq-byoc-vpc-${var.automq_byoc_env_id}"
azs = slice(data.aws_availability_zones.available_azs.names, 0, 3)
public_subnets = ["10.0.0.0/20"]
private_subnets = ["10.0.128.0/20", "10.0.144.0/20", "10.0.160.0/20"]
enable_dns_support = true
enable_dns_hostnames = true
# NAT Gateway
# if the deploy type is k8s, then enable_nat_gateway is true, single_nat_gateway is true
enable_nat_gateway = var.automq_byoc_default_deploy_type == "k8s"
single_nat_gateway = var.automq_byoc_default_deploy_type == "k8s"
tags = {
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
resource "aws_security_group" "vpc_endpoint_sg" {
count = var.create_new_vpc ? 1 : 0
description = "Security group for VPC endpoint"
vpc_id = module.automq_byoc_vpc[0].vpc_id
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "automq-byoc-endpoint-sg-${var.automq_byoc_env_id}"
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
resource "aws_vpc_endpoint" "ec2_endpoint" {
count = var.create_new_vpc ? 1 : 0
vpc_id = module.automq_byoc_vpc[0].vpc_id
service_name = "com.amazonaws.${var.cloud_provider_region}.ec2"
vpc_endpoint_type = "Interface"
security_group_ids = [aws_security_group.vpc_endpoint_sg[0].id]
subnet_ids = module.automq_byoc_vpc[0].private_subnets
private_dns_enabled = true
tags = {
Name = "automq-byoc-ec2-endpoint-${var.automq_byoc_env_id}"
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
resource "aws_vpc_endpoint" "s3_endpoint" {
count = var.create_new_vpc ? 1 : 0
vpc_id = module.automq_byoc_vpc[0].vpc_id
service_name = "com.amazonaws.${var.cloud_provider_region}.s3"
vpc_endpoint_type = "Gateway"
route_table_ids = concat(
module.automq_byoc_vpc[0].public_route_table_ids,
module.automq_byoc_vpc[0].private_route_table_ids
)
tags = {
Name = "automq-byoc-s3-endpoint-${var.automq_byoc_env_id}"
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
resource "aws_vpc_endpoint" "s3table_endpoint" {
count = var.create_new_vpc && (var.cloud_provider_region == "us-east-1" || var.cloud_provider_region == "us-east-2" || var.cloud_provider_region == "us-west-2") ? 1 : 0
vpc_id = module.automq_byoc_vpc[0].vpc_id
service_name = "com.amazonaws.${var.cloud_provider_region}.s3tables"
vpc_endpoint_type = "Interface"
security_group_ids = [aws_security_group.vpc_endpoint_sg[0].id]
subnet_ids = module.automq_byoc_vpc[0].private_subnets
private_dns_enabled = true
tags = {
Name = "automq-byoc-ec2-endpoint-${var.automq_byoc_env_id}"
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
resource "aws_vpc_endpoint" "glue_endpoint" {
count = var.create_new_vpc ? 1 : 0
vpc_id = module.automq_byoc_vpc[0].vpc_id
service_name = "com.amazonaws.${var.cloud_provider_region}.glue"
vpc_endpoint_type = "Interface"
security_group_ids = [aws_security_group.vpc_endpoint_sg[0].id]
subnet_ids = module.automq_byoc_vpc[0].private_subnets
private_dns_enabled = true
tags = {
Name = "automq-byoc-ec2-endpoint-${var.automq_byoc_env_id}"
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
locals {
automq_byoc_vpc_id = var.create_new_vpc ? module.automq_byoc_vpc[0].vpc_id : var.automq_byoc_vpc_id
automq_byoc_env_console_public_subnet_id = var.create_new_vpc ? element(module.automq_byoc_vpc[0].public_subnets, 0) : var.automq_byoc_env_console_public_subnet_id
automq_data_bucket = var.automq_byoc_data_bucket_name == "" ? module.automq_byoc_data_bucket_name.s3_bucket_id : "${var.automq_byoc_data_bucket_name}"
automq_ops_bucket = var.automq_byoc_ops_bucket_name == "" ? module.automq_byoc_ops_bucket_name.s3_bucket_id : "${var.automq_byoc_ops_bucket_name}"
}
data "aws_vpc" "vpc_id" {
id = local.automq_byoc_vpc_id
}
locals {
image_name = var.use_custom_ami ? var.automq_byoc_env_console_ami : format("AutoMQ-control-center-Prod-%s-x86_64", var.automq_byoc_env_version)
}
data "aws_ami" "console_ami" {
most_recent = true
filter {
name = "name"
values = [local.image_name]
}
}
resource "aws_security_group" "automq_byoc_console_sg" {
vpc_id = data.aws_vpc.vpc_id.id
name = "automq-byoc-console-sg-${var.automq_byoc_env_id}"
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = [var.automq_byoc_env_console_cidr]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [var.automq_byoc_env_console_cidr]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
resource "aws_iam_role" "automq_byoc_role" {
name = "automq-byoc-service-role-${var.automq_byoc_env_id}"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
tags = {
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
resource "aws_iam_role" "automq_byoc_node_role" {
count = var.automq_byoc_default_deploy_type == "k8s" ? 1 : 0
name = "automq-byoc-node-role-${var.automq_byoc_env_id}"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
tags = {
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
# https://docs.aws.amazon.com/zh_cn/eks/latest/userguide/create-node-role.html
resource "aws_iam_role_policy_attachment" "nodes-AmazonEKSWorkerNodePolicy" {
count = var.automq_byoc_default_deploy_type == "k8s" ? 1 : 0
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
role = aws_iam_role.automq_byoc_node_role[0].name
}
resource "aws_iam_role_policy_attachment" "nodes-AmazonEKS_CNI_Policy" {
count = var.automq_byoc_default_deploy_type == "k8s" ? 1 : 0
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
role = aws_iam_role.automq_byoc_node_role[0].name
}
resource "aws_iam_role_policy_attachment" "nodes-AmazonEC2ContainerRegistryReadOnly" {
count = var.automq_byoc_default_deploy_type == "k8s" ? 1 : 0
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
role = aws_iam_role.automq_byoc_node_role[0].name
}
# https://kubernetes-sigs.github.io/aws-load-balancer-controller/latest/deploy/installation/#option-b-attach-iam-policies-to-nodes
resource "aws_iam_role_policy" "aws_load-balancer_policy" {
count = var.automq_byoc_default_deploy_type == "k8s" ? 1 : 0
name = "aws-load-balancer-controller-service-policy-${var.automq_byoc_env_id}"
role = aws_iam_role.automq_byoc_node_role[0].name
policy = file("${path.module}/tpls/aws_load_balancer_controller_service_policy.json.tpl")
}
# https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/cloudprovider/aws/README.md
resource "aws_iam_role_policy" "aws_cluster_auto_scaler_policy" {
count = var.automq_byoc_default_deploy_type == "k8s" ? 1 : 0
name = "aws-cluster-auto-scaler-policy-${var.automq_byoc_env_id}"
role = aws_iam_role.automq_byoc_node_role[0].name
policy = file("${path.module}/tpls/aws_cluster_auto_scaler_policy.json.tpl")
}
resource "aws_iam_role_policy" "automq_s3_policy" {
count = var.automq_byoc_default_deploy_type == "k8s" ? 1 : 0
name = "automq-s3-policy-${var.automq_byoc_env_id}"
role = aws_iam_role.automq_byoc_node_role[0].name
policy = templatefile("${path.module}/tpls/automq_node_s3_policy.json.tpl", {
automq_data_bucket = local.automq_data_bucket
automq_ops_bucket = local.automq_ops_bucket
})
}
resource "aws_iam_policy" "automq_byoc_policy" {
count = var.automq_byoc_default_deploy_type == "vm" ? 1 : 0
name = "automq-byoc-service-policy-${var.automq_byoc_env_id}"
description = "Custom policy for automq_byoc service"
policy = templatefile("${path.module}/tpls/automq_byoc_role_policy.json.tpl", {
automq_data_bucket = local.automq_data_bucket
automq_ops_bucket = local.automq_ops_bucket
})
tags = {
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
resource "aws_iam_policy" "automq_byoc_k8s_policy" {
count = var.automq_byoc_default_deploy_type == "k8s" ? 1 : 0
name = "automq-byoc-service-k8s-policy-${var.automq_byoc_env_id}"
description = "Custom policy for automq_byoc service"
policy = templatefile("${path.module}/tpls/automq_byoc_role_k8s_policy.json.tpl", {
automq_data_bucket = local.automq_data_bucket
automq_ops_bucket = local.automq_ops_bucket
})
tags = {
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
resource "aws_iam_role_policy_attachment" "automq_byoc_role_attachment" {
role = aws_iam_role.automq_byoc_role.name
policy_arn = var.automq_byoc_default_deploy_type == "k8s" ? aws_iam_policy.automq_byoc_k8s_policy[0].arn : aws_iam_policy.automq_byoc_policy[0].arn
}
resource "aws_iam_instance_profile" "automq_byoc_instance_profile" {
name = "automq-byoc-instance-profile-${var.automq_byoc_env_id}"
role = aws_iam_role.automq_byoc_role.name
tags = {
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
resource "aws_route53_zone" "private_r53" {
name = "${var.automq_byoc_env_id}.automq.private"
vpc {
vpc_id = local.automq_byoc_vpc_id
}
lifecycle {
create_before_destroy = true
}
tags = {
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
resource "aws_eip" "web_ip" {
instance = aws_instance.automq_byoc_console.id
tags = {
automqVendor = "automq"
automqEnvironmentID = var.automq_byoc_env_id
}
}
locals {
aws_iam_instance_profile_arn_encoded = urlencode(aws_iam_instance_profile.automq_byoc_instance_profile.arn)
}
locals {
public_subnet_id = var.create_new_vpc ? module.automq_byoc_vpc[0].public_subnets[0] : var.automq_byoc_env_console_public_subnet_id
}
data "aws_subnet" "public_subnet_info" {
id = local.public_subnet_id
}