Skip to content

Commit 7d2a5a8

Browse files
author
jslopes
committed
first commit
1 parent c2cb9de commit 7d2a5a8

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

main.tf

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
data "aws_partition" "current" {}
2+
data "aws_region" "current" {}
3+
data "aws_caller_identity" "current" {}
4+
5+
data "aws_iam_policy_document" "main" {
6+
count = var.create ? 1 : 0
7+
8+
statement {
9+
effect = "Allow"
10+
actions = [
11+
"secretsmanager:GetRandomPassword",
12+
"secretsmanager:CreateSecret",
13+
"secretsmanager:ListSecrets"
14+
]
15+
resources = [
16+
"*"
17+
]
18+
}
19+
statement {
20+
effect = "Allow"
21+
actions = [
22+
"secretsmanager:*",
23+
]
24+
resources = [
25+
aws_secretsmanager_secret.main.0.arn
26+
]
27+
}
28+
29+
}
30+
data "aws_iam_policy_document" "role_rds" {
31+
count = var.create ? 1 : 0
32+
33+
statement {
34+
effect = "Allow"
35+
principals = {
36+
type = "Service"
37+
identifiers = [ "rds.amazonaws.com" ]
38+
}
39+
actions = [ "sts:AssumeRole" ]
40+
}
41+
}
42+
resource "aws_iam_role" "role_rds" {
43+
count = var.create ? 1 : 0
44+
45+
name = "${var.db_proxy_name)}-SecretManagerRole"
46+
assume_role_policy = data.aws_iam_policy_document.role_rds.0.json
47+
48+
tags = merge(
49+
{
50+
"Name" = "${format("%s", var.db_proxy_name)}-SecretManager"
51+
},
52+
var.default_tags,
53+
)
54+
}
55+
resource "aws_iam_policy" "main" {
56+
count = var.create ? 1 : 0
57+
58+
name = "${var.db_proxy_name)}-SecretManagerPolicy"
59+
path = "/"
60+
policy = data.aws_iam_policy_document.main.0.json
61+
}
62+
resource "aws_iam_role_policy_attachment" "test-attach" {
63+
count = var.create ? 1 : 0
64+
65+
role = aws_iam_role.role_rd.0.name
66+
policy_arn = aws_iam_policy.main.0.arn
67+
}
68+
69+
## Secret Manager
70+
resource "aws_secretsmanager_secret" "main" {
71+
count = var.create ? 1 : 0
72+
73+
name_prefix = "${var.db_proxy_name}-secret"
74+
recovery_window_in_days = var.recovery_window_in_days
75+
tags = var.default_tags
76+
}
77+
resource "aws_secretsmanager_secret_version" "main" {
78+
count = var.create ? 1 : 0
79+
80+
secret_id = aws_secretsmanager_secret.main.id
81+
version_stages = var.version_stages
82+
secret_string = jsonencode(var.secret_string)
83+
84+
lifecycle {
85+
ignore_changes = [ "secret_string" ]
86+
}
87+
}
88+
89+
## RDS Proxy
90+
resource "aws_db_proxy" "main" {
91+
depends_on = [ aws_iam_policy_document.main ]
92+
count = var.create ? 1 : 0
93+
94+
name = var.db_proxy_name
95+
debug_logging = var.debug_logging
96+
engine_family = var.engine_family
97+
idle_client_timeout = var.idle_client_timeout
98+
require_tls = var.require_tls
99+
role_arn = aws_iam_role.example.arn
100+
vpc_security_group_ids = var.vpc_security_group_ids
101+
vpc_subnet_ids = var.vpc_subnet_ids
102+
103+
dynamic "auth" {
104+
for_each = var.auth
105+
content {
106+
auth_scheme = lookup(auth.value, "auth_scheme", "SECRETS")
107+
description = lookup(auth.value, "description", null)
108+
iam_auth = lookup(auth.value, "iam_auth", "DISABLED")
109+
secret_arn = lookup(auth.value, "secret_arn", null)
110+
username = lookup(auth.value, "username", null)
111+
}
112+
}
113+
114+
tags = var.default_tags
115+
}
116+
resource "aws_db_proxy_default_target_group" "example" {
117+
count = var.create ? length(var.connection_pool_config) : 0
118+
119+
db_proxy_name = aws_db_proxy.main.0.name
120+
121+
dynamic "connection_pool_config" {
122+
for_each = var.connection_pool_config
123+
content {
124+
connection_borrow_timeout = lookup(var.connection_pool_config.value, "connection_borrow_timeout", null)
125+
init_query = lookup(var.init_query.value, "connection_borrow_timeout", null)
126+
max_connections_percent = lookup(var.max_connections_percent.value, "connection_borrow_timeout", null)
127+
max_idle_connections_percent = lookup(var.max_idle_connections_percent.value, "connection_borrow_timeout", null)
128+
session_pinning_filters = lookup(var.session_pinning_filters.value, "connection_borrow_timeout", null)
129+
}
130+
}
131+
}

variables.tf

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
variable "create" {
2+
type = bool
3+
default = true
4+
}
5+
variable "db_proxy_name" {
6+
type = string
7+
}
8+
variable "debug_logging" {
9+
type = bool
10+
default = false
11+
}
12+
variable "engine_family" {
13+
type = string
14+
default = "MYSQL"
15+
}
16+
variable "idle_client_timeout" {
17+
type = number
18+
default = "1800"
19+
}
20+
variable "require_tls" {
21+
type = bool
22+
default = true
23+
}
24+
variable "vpc_security_group_ids" {
25+
type = list
26+
default = []
27+
}
28+
variable "vpc_subnet_ids" {
29+
type = list
30+
default = []
31+
}
32+
variable "auth" {
33+
type = any
34+
default = []
35+
}
36+
variable "default_tags" {
37+
type = list(map)
38+
default = {}
39+
}
40+
variable "recovery_window_in_days" {
41+
type = number
42+
default = "0"
43+
}
44+
variable "version_stages" {
45+
type = list
46+
default = ["AWSCURRENT"]
47+
}
48+
variable "secret_string" {
49+
type = any
50+
default = {}
51+
}
52+
variable "connection_pool_config" {
53+
type = any
54+
default = []
55+
}

0 commit comments

Comments
 (0)