-
Notifications
You must be signed in to change notification settings - Fork 50
/
main.tf
110 lines (100 loc) · 5.48 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
resource "aws_secretsmanager_secret" "sm" {
for_each = var.secrets
name = lookup(each.value, "name_prefix", null) == null && lookup(each.value, "name", null) == null ? each.key : (lookup(each.value, "name_prefix", null) == null && lookup(each.value, "name", null) != null ? each.value.name : null)
name_prefix = lookup(each.value, "name_prefix", null) != null ? lookup(each.value, "name_prefix") : null
description = lookup(each.value, "description", null)
kms_key_id = lookup(each.value, "kms_key_id", null)
policy = lookup(each.value, "policy", null)
force_overwrite_replica_secret = lookup(each.value, "force_overwrite_replica_secret", false)
recovery_window_in_days = lookup(each.value, "recovery_window_in_days", var.recovery_window_in_days)
tags = merge(var.tags, lookup(each.value, "tags", null))
dynamic "replica" {
for_each = lookup(each.value, "replica_regions", {})
content {
region = try(replica.value.region, replica.key)
kms_key_id = try(replica.value.kms_key_id, null)
}
}
}
resource "aws_secretsmanager_secret_version" "sm-sv" {
for_each = { for k, v in var.secrets : k => v if !var.unmanaged }
secret_id = aws_secretsmanager_secret.sm[each.key].arn
secret_string = lookup(each.value, "secret_string", null) != null ? lookup(each.value, "secret_string", null) : (lookup(each.value, "secret_key_value", null) != null ? jsonencode(lookup(each.value, "secret_key_value", {})) : null)
secret_binary = lookup(each.value, "secret_binary", null) != null ? base64encode(lookup(each.value, "secret_binary")) : null
version_stages = var.version_stages
depends_on = [aws_secretsmanager_secret.sm]
lifecycle {
ignore_changes = [
secret_id,
]
}
}
resource "aws_secretsmanager_secret_version" "sm-svu" {
for_each = { for k, v in var.secrets : k => v if var.unmanaged }
secret_id = aws_secretsmanager_secret.sm[each.key].arn
secret_string = lookup(each.value, "secret_string", null) != null ? lookup(each.value, "secret_string") : (lookup(each.value, "secret_key_value", null) != null ? jsonencode(lookup(each.value, "secret_key_value", {})) : null)
secret_binary = lookup(each.value, "secret_binary", null) != null ? base64encode(lookup(each.value, "secret_binary")) : null
version_stages = var.version_stages
depends_on = [aws_secretsmanager_secret.sm]
lifecycle {
ignore_changes = [
secret_string,
secret_binary,
secret_id,
]
}
}
# Rotate secrets
resource "aws_secretsmanager_secret" "rsm" {
for_each = var.rotate_secrets
name = lookup(each.value, "name_prefix", null) == null && lookup(each.value, "name", null) == null ? each.key : (lookup(each.value, "name_prefix", null) == null && lookup(each.value, "name", null) != null ? each.value.name : null)
name_prefix = lookup(each.value, "name_prefix", null) != null ? lookup(each.value, "name_prefix") : null
description = lookup(each.value, "description")
kms_key_id = lookup(each.value, "kms_key_id", null)
policy = lookup(each.value, "policy", null)
force_overwrite_replica_secret = lookup(each.value, "force_overwrite_replica_secret", false)
recovery_window_in_days = lookup(each.value, "recovery_window_in_days", var.recovery_window_in_days)
tags = merge(var.tags, lookup(each.value, "tags", null))
}
resource "aws_secretsmanager_secret_version" "rsm-sv" {
for_each = { for k, v in var.rotate_secrets : k => v if !var.unmanaged }
secret_id = aws_secretsmanager_secret.rsm[each.key].arn
secret_string = lookup(each.value, "secret_string", null) != null ? lookup(each.value, "secret_string") : (lookup(each.value, "secret_key_value", null) != null ? jsonencode(lookup(each.value, "secret_key_value", {})) : null)
secret_binary = lookup(each.value, "secret_binary", null) != null ? base64encode(lookup(each.value, "secret_binary")) : null
version_stages = var.version_stages
depends_on = [aws_secretsmanager_secret.rsm]
lifecycle {
ignore_changes = [
secret_id,
]
}
}
resource "aws_secretsmanager_secret_version" "rsm-svu" {
for_each = { for k, v in var.rotate_secrets : k => v if var.unmanaged }
secret_id = aws_secretsmanager_secret.rsm[each.key].arn
secret_string = lookup(each.value, "secret_string", null) != null ? lookup(each.value, "secret_string") : (lookup(each.value, "secret_key_value", null) != null ? jsonencode(lookup(each.value, "secret_key_value", {})) : null)
secret_binary = lookup(each.value, "secret_binary", null) != null ? base64encode(lookup(each.value, "secret_binary")) : null
version_stages = var.version_stages
depends_on = [aws_secretsmanager_secret.rsm]
lifecycle {
ignore_changes = [
secret_string,
secret_binary,
secret_id,
]
}
}
resource "aws_secretsmanager_secret_rotation" "rsm-sr" {
for_each = var.rotate_secrets
secret_id = aws_secretsmanager_secret.rsm[each.key].arn
rotation_lambda_arn = lookup(each.value, "rotation_lambda_arn")
rotation_rules {
automatically_after_days = lookup(each.value, "automatically_after_days", var.automatically_after_days)
}
depends_on = [aws_secretsmanager_secret.rsm]
lifecycle {
ignore_changes = [
secret_id,
]
}
}