-
Notifications
You must be signed in to change notification settings - Fork 0
/
rds.tf
65 lines (60 loc) · 2.24 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
# Create RDS subnet group
resource "aws_db_subnet_group" "RDS_subnet_grp" {
name = var.db_subnet_grp
subnet_ids = aws_subnet.private_db_subnets.*.id
}
# Create RDS instance
resource "aws_db_instance" "app_db" {
identifier = var.primary_rds_identifier
availability_zone = var.az[0]
allocated_storage = 10
engine = "mysql"
engine_version = "8.0.32"
instance_class = var.db_instance_type
storage_type = "gp2"
db_subnet_group_name = aws_db_subnet_group.RDS_subnet_grp.name
vpc_security_group_ids = [aws_security_group.db_server_sg.id]
db_name = var.database_name
username = var.database_user
password = random_password.db.result
skip_final_snapshot = true
backup_retention_period = 7
}
# Create RDS instance replica
resource "aws_db_instance" "app_db_replica" {
replicate_source_db = var.primary_rds_identifier
identifier = var.replica_rds_identifier
availability_zone = var.az[1]
allocated_storage = 10
engine = "mysql"
engine_version = "8.0.32"
instance_class = var.db_instance_type
storage_type = "gp2"
vpc_security_group_ids = [aws_security_group.db_server_sg.id]
skip_final_snapshot = true
depends_on = [aws_db_instance.app_db]
}
# Security group for database servers
resource "aws_security_group" "db_server_sg" {
name = "db-server-SG"
description = "Allow inbound SSH traffic for instances in database tier"
vpc_id = aws_vpc.project_vpc.id
}
# Give application servers access to database servers
resource "aws_security_group_rule" "db_server_mysql_rule" {
security_group_id = aws_security_group.db_server_sg.id
type = "ingress"
from_port = 3306
to_port = 3306
protocol = "tcp"
source_security_group_id = module.eks.node_security_group_id
}
# Allow outbound traffic
resource "aws_security_group_rule" "db_server_outbound_rule" {
security_group_id = aws_security_group.db_server_sg.id
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}