Skip to content

Commit cc5aea8

Browse files
author
jslopes
committed
add suporte para secret manager
1 parent b2136e2 commit cc5aea8

File tree

1 file changed

+103
-3
lines changed

1 file changed

+103
-3
lines changed

main.tf

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,100 @@
1+
#
2+
# IAM Policy Document
3+
#
4+
5+
data "aws_region" "current" {}
6+
data "aws_caller_identity" "current" {}
7+
8+
data "aws_iam_policy_document" "main" {
9+
count = var.create ? 1 : 0
10+
11+
statement {
12+
effect = "Allow"
13+
actions = [
14+
"secretsmanager:GetRandomPassword",
15+
"secretsmanager:CreateSecret",
16+
"secretsmanager:ListSecrets"
17+
]
18+
resources = [
19+
"*"
20+
]
21+
}
22+
statement {
23+
effect = "Allow"
24+
actions = [
25+
"secretsmanager:*",
26+
]
27+
resources = [
28+
aws_secretsmanager_secret.main.0.arn
29+
]
30+
}
31+
32+
}
33+
data "aws_iam_policy_document" "role_rds" {
34+
count = var.create ? 1 : 0
35+
36+
statement {
37+
effect = "Allow"
38+
principals {
39+
type = "Service"
40+
identifiers = [ "rds.amazonaws.com" ]
41+
}
42+
actions = [ "sts:AssumeRole" ]
43+
}
44+
}
45+
resource "aws_iam_role" "role_rds" {
46+
count = var.create ? 1 : 0
47+
48+
name = "${var.db_proxy_name}-SecretManagerRole"
49+
assume_role_policy = data.aws_iam_policy_document.role_rds.0.json
50+
51+
tags = merge(
52+
{
53+
"Name" = "${format("%s", var.db_proxy_name)}-SecretManager"
54+
},
55+
var.default_tags,
56+
)
57+
}
58+
resource "aws_iam_policy" "main" {
59+
count = var.create ? 1 : 0
60+
61+
name = "${var.db_proxy_name}-SecretManagerPolicy"
62+
path = "/"
63+
policy = data.aws_iam_policy_document.main.0.json
64+
}
65+
resource "aws_iam_role_policy_attachment" "role_rds" {
66+
count = var.create ? 1 : 0
67+
68+
role = aws_iam_role.role_rds.0.name
69+
policy_arn = aws_iam_policy.main.0.arn
70+
}
71+
72+
#
73+
# Secret Manager
74+
#
75+
76+
resource "aws_secretsmanager_secret" "main" {
77+
count = var.create ? length(var.secretsmanager) 1 : 0
78+
79+
name_prefix = "${var.db_proxy_name}-secret"
80+
recovery_window_in_days = length(var.secretsmanager[count.index], "recovery_window_in_days", null )
81+
82+
tags = length(var.secretsmanager[count.index], "tags", null )
83+
}
84+
85+
resource "aws_secretsmanager_secret_version" "main" {
86+
count = var.create ? length(var.secretsmanager) 1 : 0
87+
88+
secret_id = aws_secretsmanager_secret.main.0.id
89+
90+
version_stages = length(var.secretsmanager[count.index], "version_stages", null )
91+
secret_string = jsonencode(length(var.secretsmanager[count.index], "secret_string", null ))
92+
93+
lifecycle {
94+
ignore_changes = [ secret_string ]
95+
}
96+
}
97+
198
#
299
# RDS Proxy
3100
#
@@ -16,11 +113,14 @@ resource "aws_db_proxy" "main" {
16113

17114
dynamic "auth" {
18115
for_each = var.auth
116+
19117
content {
20-
auth_scheme = lookup(auth.value, "auth_scheme", "SECRETS")
21-
description = lookup(auth.value, "description", null)
22-
iam_auth = lookup(auth.value, "iam_auth", "DISABLED")
118+
119+
auth_scheme = lookup(auth.value, "auth_scheme", "SECRETS" )
120+
description = lookup(auth.value, "description", null )
121+
iam_auth = lookup(auth.value, "iam_auth", "DISABLED" )
23122
secret_arn = aws_secretsmanager_secret.main.0.arn
123+
24124
}
25125
}
26126

0 commit comments

Comments
 (0)