-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.tf
258 lines (207 loc) · 6.83 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
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
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
}
}
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}-${var.automq_byoc_env_id}"
automq_ops_bucket = var.automq_byoc_ops_bucket_name == "" ? module.automq_byoc_ops_bucket_name.s3_bucket_id : "${var.automq_byoc_ops_bucket_name}-${var.automq_byoc_env_id}"
}
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]
}
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_policy" "automq_byoc_policy" {
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_role_policy_attachment" "automq_byoc_role_attachment" {
role = aws_iam_role.automq_byoc_role.name
policy_arn = aws_iam_policy.automq_byoc_policy.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
}