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+ }
0 commit comments