-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrds.tf
101 lines (88 loc) · 3.11 KB
/
rds.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
resource "aws_rds_cluster" "this" {
cluster_identifier_prefix = "${var.id}-"
final_snapshot_identifier = "${var.id}-${formatdate("YYYYMMDDhhmmss", timestamp())}"
copy_tags_to_snapshot = true
engine = "aurora"
engine_mode = "serverless"
database_name = "metabase"
master_username = "root"
master_password = random_string.this.result
backup_retention_period = 5 # days
snapshot_identifier = var.snapshot_identifier
vpc_security_group_ids = [aws_security_group.rds.id]
db_subnet_group_name = aws_db_subnet_group.this.id
db_cluster_parameter_group_name = var.db_cluster_parameter_group_name
deletion_protection = var.protection
enable_http_endpoint = true
tags = var.tags
scaling_configuration {
auto_pause = var.auto_pause
min_capacity = 1
max_capacity = var.max_capacity
}
lifecycle {
create_before_destroy = true
ignore_changes = [snapshot_identifier, final_snapshot_identifier]
}
}
resource "random_string" "this" {
length = 32
special = false
lifecycle {
create_before_destroy = true
}
}
resource "aws_ssm_parameter" "this" {
name = var.id
description = "RDS password"
type = "SecureString"
value = random_string.this.result
tags = var.tags
}
resource "aws_secretsmanager_secret" "this" {
name = "rds-db-credentials/${aws_rds_cluster.this.cluster_resource_id}/${var.id}"
description = "RDS credentials for use in query editor"
recovery_window_in_days = 0
tags = var.tags
}
locals {
secret = {
dbInstanceIdentifier = aws_rds_cluster.this.cluster_identifier
engine = aws_rds_cluster.this.engine
dbname = aws_rds_cluster.this.database_name
host = aws_rds_cluster.this.endpoint
port = aws_rds_cluster.this.port
resourceId = aws_rds_cluster.this.cluster_resource_id
username = aws_rds_cluster.this.master_username
password = random_string.this.result
}
}
resource "aws_secretsmanager_secret_version" "this" {
secret_id = aws_secretsmanager_secret.this.id
secret_string = jsonencode(local.secret)
}
resource "aws_db_subnet_group" "this" {
name_prefix = "${var.id}-"
subnet_ids = tolist(var.private_subnet_ids)
tags = var.tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group" "rds" {
name_prefix = "${var.id}-rds-"
vpc_id = var.vpc_id
tags = var.tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "rds_ingress_ecs" {
description = "ECS"
type = "ingress"
from_port = aws_rds_cluster.this.port
to_port = aws_rds_cluster.this.port
protocol = "tcp"
security_group_id = aws_security_group.rds.id
source_security_group_id = aws_security_group.ecs.id
}