-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
executable file
·443 lines (351 loc) · 13.6 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
locals {
enabled = coalesce(var.enabled, module.this.enabled, true)
name = coalesce(var.name, module.this.name, "teleport-cluster-${random_string.teleport_cluster_random_suffix.result}")
aws_account_id = var.aws_account_id != "" ? var.aws_account_id : try(data.aws_caller_identity.current[0].account_id, "")
aws_region_name = var.aws_region_name != "" ? var.aws_region_name : try(data.aws_region.current[0].name, "")
aws_kv_namespace = trim(coalesce(var.aws_kv_namespace, "teleport-cluster/${module.teleport_cluster_label.id}"), "/")
teleport_cluster_name = join("-", [module.teleport_cluster_label.name, module.teleport_cluster_label.stage, module.teleport_cluster_label.environment])
teleport_runtime_version_major = split(".", var.teleport_runtime_version)[0]
teleport_image_name = local.teleport_runtime_version_major >= 15 ? "teleport-oss-${var.teleport_runtime_version}-x86-64-*" : "gravitational-teleport-ami-oss-${var.teleport_runtime_version}"
teleport_image_id = try(data.aws_ami.official_image[0].id, "")
teleport_letsencrypt_email = var.teleport_letsencrypt_email
teleport_setup_mode = var.teleport_setup_mode
teleport_experimental_mode = var.teleport_experimental_mode
teleport_aws_account_id = local.teleport_runtime_version_major >= 15 ? "146628656107" : "126027368216" # gravitational teleport's aws account id for ami filtering - https://goteleport.com/docs/deploy-a-cluster/deployments/aws-starter-cluster-terraform/
artifacts_bucket_name = coalesce(var.artifacts_bucket_name, local.teleport_bucket_name)
logs_bucket_name = coalesce(var.logs_bucket_name, local.teleport_bucket_name)
teleport_bucket_name = module.s3_bucket.bucket_id
is_teleport_and_logs_bucket_same = local.artifacts_bucket_name == local.logs_bucket_name
instance_config = {
auth = merge({ sizes = ["t3.micro", "t3a.micro"], count = 1 }, lookup(var.instance_config, "auth", {}))
node = merge({ sizes = ["t3.micro", "t3a.micro"], count = 1 }, lookup(var.instance_config, "node", {}))
proxy = merge({ sizes = ["t3.micro", "t3a.micro"], count = 1 }, lookup(var.instance_config, "proxy", {}))
}
}
data "aws_caller_identity" "current" {
count = module.this.enabled && var.aws_account_id == "" ? 1 : 0
}
data "aws_region" "current" {
count = module.this.enabled && var.aws_region_name == "" ? 1 : 0
}
# ================================================================= teleport ===
module "teleport_cluster_label" {
source = "cloudposse/label/null"
version = "0.25.0"
enabled = local.enabled
name = local.name
context = module.this.context
}
# only appliable if name variable was not set
resource "random_string" "teleport_cluster_random_suffix" {
length = 6
special = false
upper = false
}
# ================================================================== cluster ===
module "auth_servers" {
source = "./modules/teleport-node"
instance_sizes = local.instance_config.auth.sizes
instance_count = local.instance_config.auth.count
teleport_cluster_name = local.teleport_cluster_name
teleport_image_id = local.teleport_image_id
teleport_letsencrypt_email = local.teleport_letsencrypt_email
teleport_node_type = "auth"
teleport_setup_mode = local.teleport_setup_mode
teleport_bucket_name = module.s3_bucket.bucket_id
teleport_ddb_table_events_name = aws_dynamodb_table.events[0].name
teleport_ddb_table_locks_name = aws_dynamodb_table.locks[0].name
teleport_ddb_table_state_name = aws_dynamodb_table.state[0].name
teleport_security_group_ids = compact([module.security_group.id])
experimental = local.teleport_experimental_mode
dns_parent_zone_id = var.dns_parent_zone_id
dns_parent_zone_name = var.dns_parent_zone_name
artifacts_bucket_name = local.artifacts_bucket_name
logs_bucket_name = local.logs_bucket_name
vpc_id = var.vpc_id
vpc_private_subnet_ids = var.vpc_private_subnet_ids
vpc_public_subnet_ids = var.vpc_public_subnet_ids
aws_account_id = local.aws_account_id
aws_kv_namespace = local.aws_kv_namespace
aws_region_name = local.aws_region_name
context = module.teleport_cluster_label.context
}
module "proxy_servers" {
source = "./modules/teleport-node"
instance_sizes = local.instance_config.proxy.sizes
instance_count = local.instance_config.proxy.count
teleport_cluster_name = local.teleport_cluster_name
teleport_image_id = local.teleport_image_id
teleport_letsencrypt_email = local.teleport_letsencrypt_email
teleport_node_type = "proxy"
teleport_setup_mode = local.teleport_setup_mode
teleport_auth_address = module.auth_servers.lb_dns_name
teleport_bucket_name = module.s3_bucket.bucket_id
teleport_ddb_table_events_name = aws_dynamodb_table.events[0].name
teleport_ddb_table_locks_name = aws_dynamodb_table.locks[0].name
teleport_ddb_table_state_name = aws_dynamodb_table.state[0].name
teleport_security_group_ids = compact([module.security_group.id])
experimental = local.teleport_experimental_mode
dns_parent_zone_id = var.dns_parent_zone_id
dns_parent_zone_name = var.dns_parent_zone_name
artifacts_bucket_name = local.artifacts_bucket_name # todo - create bucket with module
logs_bucket_name = local.logs_bucket_name
vpc_id = var.vpc_id
vpc_private_subnet_ids = var.vpc_private_subnet_ids
vpc_public_subnet_ids = var.vpc_public_subnet_ids
aws_account_id = local.aws_account_id
aws_kv_namespace = local.aws_kv_namespace
aws_region_name = local.aws_region_name
context = module.teleport_cluster_label.context
}
module "node_servers" {
source = "./modules/teleport-node"
instance_sizes = local.instance_config.node.sizes
instance_count = local.instance_config.node.count
teleport_cluster_name = local.teleport_cluster_name
teleport_image_id = local.teleport_image_id
teleport_letsencrypt_email = local.teleport_letsencrypt_email
teleport_node_type = "node"
teleport_setup_mode = local.teleport_setup_mode
teleport_auth_address = module.auth_servers.lb_dns_name
teleport_bucket_name = module.s3_bucket.bucket_id
teleport_ddb_table_events_name = aws_dynamodb_table.events[0].name
teleport_ddb_table_locks_name = aws_dynamodb_table.locks[0].name
teleport_ddb_table_state_name = aws_dynamodb_table.state[0].name
teleport_security_group_ids = compact([module.security_group.id])
experimental = local.teleport_experimental_mode
dns_parent_zone_id = var.dns_parent_zone_id
dns_parent_zone_name = var.dns_parent_zone_name
artifacts_bucket_name = local.artifacts_bucket_name # todo - create bucket with module
logs_bucket_name = local.logs_bucket_name
vpc_id = var.vpc_id
vpc_private_subnet_ids = var.vpc_private_subnet_ids
vpc_public_subnet_ids = var.vpc_public_subnet_ids
aws_account_id = local.aws_account_id
aws_kv_namespace = local.aws_kv_namespace
aws_region_name = local.aws_region_name
context = module.teleport_cluster_label.context
}
# ========================================================= cluster-resource ===
# ---------------------------------------------------------------------- ddb ---
resource "aws_dynamodb_table" "state" {
count = local.enabled ? 1 : 0
name = "${module.teleport_cluster_label.id}-state"
billing_mode = "PAY_PER_REQUEST"
hash_key = "HashKey"
range_key = "FullPath"
stream_enabled = "true"
stream_view_type = "NEW_IMAGE"
server_side_encryption {
enabled = true
}
lifecycle {
ignore_changes = [
read_capacity,
write_capacity,
]
}
attribute {
name = "HashKey"
type = "S"
}
attribute {
name = "FullPath"
type = "S"
}
ttl {
attribute_name = "Expires"
enabled = true
}
tags = module.teleport_cluster_label.tags
}
resource "aws_dynamodb_table" "events" {
count = local.enabled ? 1 : 0
name = "${module.teleport_cluster_label.id}-events"
billing_mode = "PAY_PER_REQUEST"
hash_key = "SessionID"
range_key = "EventIndex"
point_in_time_recovery {
enabled = true
}
server_side_encryption {
enabled = true
}
global_secondary_index {
name = "timesearchV2"
hash_key = "CreatedAtDate"
range_key = "CreatedAt"
write_capacity = 10
read_capacity = 10
projection_type = "ALL"
}
lifecycle {
ignore_changes = all
}
attribute {
name = "SessionID"
type = "S"
}
attribute {
name = "EventIndex"
type = "N"
}
attribute {
name = "CreatedAtDate"
type = "S"
}
attribute {
name = "CreatedAt"
type = "N"
}
ttl {
attribute_name = "Expires"
enabled = true
}
tags = module.teleport_cluster_label.tags
}
resource "aws_dynamodb_table" "locks" {
count = local.enabled ? 1 : 0
name = "${module.teleport_cluster_label.id}-locks"
billing_mode = "PAY_PER_REQUEST"
hash_key = "Lock"
lifecycle {
ignore_changes = [
read_capacity,
write_capacity,
]
}
attribute {
name = "Lock"
type = "S"
}
ttl {
attribute_name = "Expires"
enabled = true
}
tags = merge(module.teleport_cluster_label.tags, {
TeleportCluster = local.teleport_cluster_name
})
}
# ----------------------------------------------------------------------- s3 ---
module "s3_bucket" {
source = "cloudposse/s3-bucket/aws"
version = "3.1.2"
acl = "private"
block_public_policy = true
force_destroy = local.teleport_experimental_mode
sse_algorithm = "AES256"
allow_ssl_requests_only = true
source_policy_documents = [data.aws_iam_policy_document.bucket.json]
logging = {
bucket_name = local.logs_bucket_name
prefix = "access/s3/${module.teleport_cluster_label.id}"
}
lifecycle_configuration_rules = [{
enabled = true
id = "transition-old-versions"
abort_incomplete_multipart_upload_days = 5
filter_and = null
expiration = null
transition = []
noncurrent_version_expiration = null
noncurrent_version_transition = [
{
newer_noncurrent_versions = 2
noncurrent_days = 30
storage_class = "STANDARD_IA"
},
{
newer_noncurrent_versions = 2
noncurrent_days = 60
storage_class = "GLACIER"
}
]
}]
context = module.teleport_cluster_label.context
}
data "aws_iam_policy_document" "bucket" {
dynamic "statement" {
for_each = local.is_teleport_and_logs_bucket_same ? [true] : []
content {
sid = "AWSLogDeliveryWrite"
effect = "Allow"
actions = ["s3:PutObject"]
resources = ["arn:aws:s3:::${local.logs_bucket_name}/AWSLogs/${local.aws_account_id}/*"]
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
}
dynamic "statement" {
for_each = local.is_teleport_and_logs_bucket_same ? [true] : []
content {
sid = "AWSLogDeliveryAclCheck"
effect = "Allow"
actions = ["s3:GetBucketAcl"]
resources = ["arn:aws:s3:::${local.logs_bucket_name}"]
principals {
type = "Service"
identifiers = ["delivery.logs.amazonaws.com"]
}
}
}
}
# ----------------------------------------------------------- security-group ---
module "security_group" {
source = "cloudposse/security-group/aws"
version = "2.2.0"
vpc_id = var.vpc_id
create_before_destroy = false
preserve_security_group_id = true
rules = [{
key = "gropu-egress"
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
description = "allow all group egress"
cidr_blocks = []
ipv6_cidr_blocks = []
source_security_group_id = null
self = true
}, {
key = "group-ingress"
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
description = "allow all group ingress"
cidr_blocks = []
ipv6_cidr_blocks = []
source_security_group_id = null
self = true
}]
tags = merge(module.teleport_cluster_label.tags, { Name = module.teleport_cluster_label.id })
context = module.teleport_cluster_label.context
}
# ================================================================== uploads ===
resource "aws_s3_object" "image_files" {
for_each = toset(local.enabled ? fileset("${path.module}/assets/image-files", "*") : [])
bucket = local.artifacts_bucket_name
key = "${local.aws_kv_namespace}/image/files/bin/${each.key}"
source = "${path.module}/assets/image-files/${each.key}"
etag = filemd5("${path.module}/assets/image-files/${each.key}")
tags = module.teleport_cluster_label.tags
}
# =================================================================== lookup ===
data "aws_ami" "official_image" {
count = module.teleport_cluster_label.enabled ? 1 : 0
most_recent = true
owners = [local.teleport_aws_account_id]
filter {
name = "name"
values = [local.teleport_image_name]
}
}