generated from clowdhaus/terraform-aws-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.tf
146 lines (113 loc) · 4.49 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
################################################################################
# Secret
################################################################################
resource "aws_secretsmanager_secret" "this" {
count = var.create ? 1 : 0
description = var.description
force_overwrite_replica_secret = var.force_overwrite_replica_secret
kms_key_id = var.kms_key_id
name = var.name
name_prefix = var.name_prefix
recovery_window_in_days = var.recovery_window_in_days
dynamic "replica" {
for_each = var.replica
content {
kms_key_id = try(replica.value.kms_key_id, null)
region = try(replica.value.region, replica.key)
}
}
tags = var.tags
}
################################################################################
# Policy
################################################################################
data "aws_iam_policy_document" "this" {
count = var.create && var.create_policy ? 1 : 0
source_policy_documents = var.source_policy_documents
override_policy_documents = var.override_policy_documents
dynamic "statement" {
for_each = var.policy_statements
content {
sid = try(statement.value.sid, null)
actions = try(statement.value.actions, null)
not_actions = try(statement.value.not_actions, null)
effect = try(statement.value.effect, null)
resources = try(statement.value.resources, null)
not_resources = try(statement.value.not_resources, null)
dynamic "principals" {
for_each = try(statement.value.principals, [])
content {
type = principals.value.type
identifiers = principals.value.identifiers
}
}
dynamic "not_principals" {
for_each = try(statement.value.not_principals, [])
content {
type = not_principals.value.type
identifiers = not_principals.value.identifiers
}
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
values = condition.value.values
variable = condition.value.variable
}
}
}
}
}
resource "aws_secretsmanager_secret_policy" "this" {
count = var.create && var.create_policy ? 1 : 0
secret_arn = aws_secretsmanager_secret.this[0].arn
policy = data.aws_iam_policy_document.this[0].json
block_public_policy = var.block_public_policy
}
################################################################################
# Version
################################################################################
resource "aws_secretsmanager_secret_version" "this" {
count = var.create && !(var.enable_rotation || var.ignore_secret_changes) ? 1 : 0
secret_id = aws_secretsmanager_secret.this[0].id
secret_string = var.create_random_password ? random_password.this[0].result : var.secret_string
secret_binary = var.secret_binary
version_stages = var.version_stages
}
resource "aws_secretsmanager_secret_version" "ignore_changes" {
count = var.create && (var.enable_rotation || var.ignore_secret_changes) ? 1 : 0
secret_id = aws_secretsmanager_secret.this[0].id
secret_string = var.create_random_password ? random_password.this[0].result : var.secret_string
secret_binary = var.secret_binary
version_stages = var.version_stages
lifecycle {
ignore_changes = [
secret_string,
secret_binary,
version_stages,
]
}
}
resource "random_password" "this" {
count = var.create && var.create_random_password ? 1 : 0
length = var.random_password_length
special = true
override_special = var.random_password_override_special
}
################################################################################
# Rotation
################################################################################
resource "aws_secretsmanager_secret_rotation" "this" {
count = var.create && var.enable_rotation ? 1 : 0
rotation_lambda_arn = var.rotation_lambda_arn
dynamic "rotation_rules" {
for_each = [var.rotation_rules]
content {
automatically_after_days = try(rotation_rules.value.automatically_after_days, null)
duration = try(rotation_rules.value.duration, null)
schedule_expression = try(rotation_rules.value.schedule_expression, null)
}
}
secret_id = aws_secretsmanager_secret.this[0].id
}