-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
279 lines (235 loc) · 7.67 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
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
module "networking" {
source = "./modules/networking"
# The namespace and environment variables are used to construct the names of the resources
# e.g. ${namespace}-${environment}-vpc
namespace = var.namespace
environment = var.environment
vpc_cidr = var.vpc_cidr
availability_zones = var.availability_zones
private_subnet_cidrs = var.private_subnet_cidrs
public_subnet_cidrs = var.public_subnet_cidrs
single_nat_gateway = var.single_nat_gateway
tags = local.common_tags
}
module "eks" {
source = "./modules/eks"
# The namespace and environment variables are used to construct the names of the resources
# e.g. ${namespace}-${environment}-eks
namespace = var.namespace
environment = var.environment
cluster_version = var.cluster_version
vpc_id = local.network_id
private_subnet_ids = local.network_private_subnet_ids
node_group_desired_size = var.node_group_desired_size
node_group_min_size = var.node_group_min_size
node_group_max_size = var.node_group_max_size
node_group_instance_types = var.node_group_instance_types
node_group_ami_type = var.node_group_ami_type
cluster_enabled_log_types = var.cluster_enabled_log_types
node_group_capacity_type = var.node_group_capacity_type
enable_cluster_creator_admin_permissions = var.enable_cluster_creator_admin_permissions
tags = local.common_tags
}
module "storage" {
source = "./modules/storage"
# The namespace and environment variables are used to construct the names of the resources
# e.g. ${namespace}-${environment}-storage
namespace = var.namespace
environment = var.environment
bucket_lifecycle_rules = var.bucket_lifecycle_rules
enable_bucket_encryption = var.enable_bucket_encryption
enable_bucket_versioning = var.enable_bucket_versioning
bucket_force_destroy = var.bucket_force_destroy
tags = local.common_tags
}
module "database" {
source = "./modules/database"
# The namespace and environment variables are used to construct the names of the resources
# e.g. ${namespace}-${environment}-db
namespace = var.namespace
environment = var.environment
postgres_version = var.postgres_version
instance_class = var.db_instance_class
allocated_storage = var.db_allocated_storage
database_name = var.database_name
database_username = var.database_username
multi_az = var.db_multi_az
database_subnet_ids = local.network_private_subnet_ids
vpc_id = local.network_id
eks_security_group_id = module.eks.cluster_security_group_id
eks_node_security_group_id = module.eks.node_security_group_id
max_allocated_storage = var.db_max_allocated_storage
database_password = var.database_password
tags = local.common_tags
}
module "operator" {
source = "github.com/MaterializeInc/terraform-helm-materialize?ref=v0.1.2"
count = var.install_materialize_operator ? 1 : 0
install_metrics_server = var.install_metrics_server
depends_on = [
module.eks,
module.database,
module.storage
]
namespace = var.namespace
environment = var.environment
operator_version = var.operator_version
operator_namespace = var.operator_namespace
helm_values = local.merged_helm_values
instances = local.instances
providers = {
kubernetes = kubernetes
helm = helm
}
}
locals {
network_id = var.create_vpc ? module.networking.vpc_id : var.network_id
network_private_subnet_ids = var.create_vpc ? module.networking.private_subnet_ids : var.network_private_subnet_ids
default_helm_values = {
observability = {
podMetrics = {
enabled = true
}
}
operator = {
image = {
tag = var.orchestratord_version
}
cloudProvider = {
type = "aws"
region = data.aws_region.current.name
providers = {
aws = {
enabled = true
accountID = data.aws_caller_identity.current.account_id
iam = {
roles = {
environment = aws_iam_role.materialize_s3.arn
}
}
}
}
}
}
}
merged_helm_values = merge(local.default_helm_values, var.helm_values)
instances = [
for instance in var.materialize_instances : {
name = instance.name
namespace = instance.namespace
database_name = instance.database_name
metadata_backend_url = format(
"postgres://%s:%s@%s/%s?sslmode=require",
var.database_username,
var.database_password,
module.database.db_instance_endpoint,
coalesce(instance.database_name, instance.name)
)
persist_backend_url = format(
"s3://%s/%s-%s:serviceaccount:%s:%s",
module.storage.bucket_name,
var.environment,
instance.name,
coalesce(instance.namespace, var.operator_namespace),
instance.name
)
cpu_request = instance.cpu_request
memory_request = instance.memory_request
memory_limit = instance.memory_limit
}
]
# Common tags that apply to all resources
common_tags = merge(
var.tags,
{
Namespace = var.namespace
Environment = var.environment
ManagedBy = "terraform"
}
)
}
resource "aws_cloudwatch_log_group" "materialize" {
count = var.enable_monitoring ? 1 : 0
name = "/aws/${var.log_group_name_prefix}/${module.eks.cluster_name}/${var.environment}"
retention_in_days = var.metrics_retention_days
tags = var.tags
}
resource "aws_iam_user" "materialize" {
name = "${local.name_prefix}-mz-user"
}
resource "aws_iam_access_key" "materialize_user" {
user = aws_iam_user.materialize.name
}
resource "aws_iam_user_policy" "materialize_s3" {
name = "${local.name_prefix}-mz-s3-policy"
user = aws_iam_user.materialize.name
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:ListBucket"
]
Resource = [
module.storage.bucket_arn,
"${module.storage.bucket_arn}/*"
]
}
]
})
}
resource "aws_iam_role" "materialize_s3" {
name = "${local.name_prefix}-mz-role"
# Trust policy allowing EKS to assume this role
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
Federated = module.eks.oidc_provider_arn
}
Action = "sts:AssumeRoleWithWebIdentity"
Condition = {
StringLike = {
"${trimprefix(module.eks.cluster_oidc_issuer_url, "https://")}:sub" : "system:serviceaccount:*:*",
"${trimprefix(module.eks.cluster_oidc_issuer_url, "https://")}:aud" : "sts.amazonaws.com"
}
}
}
]
})
tags = local.common_tags
depends_on = [
module.eks
]
}
resource "aws_iam_role_policy" "materialize_s3" {
name = "${local.name_prefix}-mz-role-policy"
role = aws_iam_role.materialize_s3.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:ListBucket"
]
Resource = [
module.storage.bucket_arn,
"${module.storage.bucket_arn}/*"
]
}
]
})
}
locals {
name_prefix = "${var.namespace}-${var.environment}"
}