-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrds.tf
94 lines (82 loc) · 2.33 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
# -------------------------------------------------------------------
# RDS
# -------------------------------------------------------------------
locals {
db_servers = toset(lookup(yamldecode(file("databases.yaml")), var.environment, []))
}
resource "aws_security_group" "uber_rds" {
name = "uber_rds"
description = "Allow access to Uber RDS"
vpc_id = aws_vpc.uber.id
ingress {
description = "Postgres"
from_port = 5432
to_port = 5432
protocol = "tcp"
cidr_blocks = [
aws_subnet.primary.cidr_block,
aws_subnet.secondary.cidr_block,
"0.0.0.0/0"
]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "Ubersystem RDS"
}
}
resource "random_password" "uber" {
length = 40
special = false
keepers = {
pass_version = 2
}
}
resource "aws_secretsmanager_secret" "db_password" {
name = "rds-postgres-passwd"
recovery_window_in_days = 0
}
resource "aws_secretsmanager_secret_version" "password" {
secret_id = aws_secretsmanager_secret.db_password.id
secret_string = random_password.uber.result
}
resource "aws_db_instance" "uber" {
allocated_storage = 100
storage_type = "gp3"
db_name = "uber"
engine = "postgres"
instance_class = var.rds_instance_size
identifier = "ubersystem"
username = "postgres"
password = aws_secretsmanager_secret_version.password.secret_string
skip_final_snapshot = true
multi_az = false
publicly_accessible = true
vpc_security_group_ids = [
aws_security_group.uber_rds.id
]
db_subnet_group_name = aws_db_subnet_group.uber.name
depends_on = [
aws_route_table_association.primary_route,
aws_route_table_association.secondary_route
]
}
provider "postgresql" {
host = aws_db_instance.uber.address
username = aws_db_instance.uber.username
password = aws_secretsmanager_secret_version.password.secret_string
superuser = false
}
module "postgres-db" {
source = "./modules/postgres-db"
for_each = local.db_servers
providers = {
postgresql = postgresql
}
name = each.key
}