-
Notifications
You must be signed in to change notification settings - Fork 0
/
rds.tf
155 lines (122 loc) · 4.03 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
##################################
# Configuration
##################################
locals {
rds_instance_class = "db.t3.small"
rds_db_name = "lakefs_db"
rds_identifier = "lakefs-rds" # Name in the console
}
##################################
# Subnet group
##################################
resource "aws_db_subnet_group" "lakefs" {
name = "lakefs"
subnet_ids = var.subnet_ids
tags = {
Name = "lakefs"
}
}
##################################
# Security Groups
##################################
resource "aws_security_group" "sg_rds" {
name = "Allow RDS access"
vpc_id = var.vpc_id
}
# Allow my IP to psql
resource "aws_security_group_rule" "allow_my_ip_5432" {
type = "ingress"
from_port = 5432
to_port = 5432
protocol = "tcp"
cidr_blocks = ["${chomp(data.http.myip.body)}/32"]
security_group_id = aws_security_group.sg_rds.id
}
# Allow EC2 in
resource "aws_security_group_rule" "allow_lakefs_ec2" {
type = "ingress"
from_port = 5432
to_port = 5432
protocol = "tcp"
security_group_id = aws_security_group.sg_rds.id
source_security_group_id = aws_security_group.sg_server.id
}
locals {
rds_security_groups = [
# Just in case we need to add more
aws_security_group.sg_rds.id
]
}
##################################
# RDS instance
##################################
resource "time_sleep" "wait_30_seconds" {
create_duration = "30s"
destroy_duration = "30s"
}
resource "aws_db_instance" "lakefs" {
count = var.rds_snapshot_id == "" ? 1 : 0
# !!!!!!!!!!!!!!!!!!!!!!! NB !!!!!!!!!!!!!!!!!!!!!!!!!
# Keep below parameters in sync on both aws_db_instance resources
identifier = local.rds_identifier
engine = "postgres"
instance_class = local.rds_instance_class
db_subnet_group_name = aws_db_subnet_group.lakefs.name
allocated_storage = 20
max_allocated_storage = 40
storage_encrypted = true
name = local.rds_db_name # deprecated but still in provider v3x
# db_name = "lakefs_db"
username = var.rds_admin_user
password = var.rds_admin_password
vpc_security_group_ids = local.rds_security_groups
#publicly_accessible = true
/*
Error: Error modifying DB Instance lakefs-rds: InvalidVPCNetworkStateFault: Cannot create a publicly accessible DBInstance.
The specified VPC does not support DNS resolution, DNS hostnames, or both. Update the VPC and then try again
*/
apply_immediately = true
skip_final_snapshot = true
tags = {
Name = "lakefs"
}
depends_on = [
# AWS Provider minor bug.
# Instance already exists error when RDS is destroyed and recreated immediately with same instance ID.
time_sleep.wait_30_seconds
]
}
data "aws_db_snapshot" "latest_snapshot" {
count = var.rds_snapshot_id == "" ? 0 : 1
db_snapshot_identifier = var.rds_snapshot_id
most_recent = true
}
# Use the latest snapshot to create an RDS instance.
resource "aws_db_instance" "lakefs_from_snapshot" {
count = var.rds_snapshot_id == "" ? 0 : 1
snapshot_identifier = data.aws_db_snapshot.latest_snapshot[0].id
# !!!!!!!!!!!!!!!!!!!!!!! NB !!!!!!!!!!!!!!!!!!!!!!!!!
# Keep below parameters in sync on both aws_db_instance resources
identifier = local.rds_identifier
engine = "postgres"
instance_class = local.rds_instance_class
db_subnet_group_name = aws_db_subnet_group.lakefs.name
allocated_storage = 20
max_allocated_storage = 40
storage_encrypted = true
name = local.rds_db_name # deprecated but still in provider v3x
# db_name = "lakefs_db"
username = var.rds_admin_user
password = var.rds_admin_password
vpc_security_group_ids = local.rds_security_groups
apply_immediately = true
skip_final_snapshot = true
tags = {
Name = "lakefs"
}
depends_on = [
# AWS Provider minor bug.
# Instance already exists error when RDS is destroyed and recreated immediately with same instance ID.
time_sleep.wait_30_seconds
]
}