Skip to content

Commit

Permalink
chore(backend): fix iac for the database (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
tericcabrel authored Sep 1, 2024
1 parent 49838a0 commit 2d2d8c3
Showing 1 changed file with 58 additions and 20 deletions.
78 changes: 58 additions & 20 deletions apps/backend/_infra/prod/storage/database.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,51 @@ data "doppler_secrets" "prod" {
}

resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
}

resource "aws_subnet" "public" {
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
}

resource "aws_route_table" "main" {
vpc_id = aws_vpc.main.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
}

resource "aws_subnet" "rds_subnet0" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "${var.aws_region}a"
map_public_ip_on_launch = true
}

resource "aws_subnet" "rds_subnet1" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24"
availability_zone = "${var.aws_region}b"
map_public_ip_on_launch = true
}

resource "aws_route_table_association" "subnet0_association" {
subnet_id = aws_subnet.rds_subnet0.id
route_table_id = aws_route_table.main.id
}

resource "aws_route_table_association" "subnet1_association" {
subnet_id = aws_subnet.rds_subnet1.id
route_table_id = aws_route_table.main.id
}

resource "aws_db_subnet_group" "default" {
name = "snipcode-prod-subnet-group"
subnet_ids = [aws_subnet.public.id]
subnet_ids = [aws_subnet.rds_subnet0.id, aws_subnet.rds_subnet1.id]

tags = {
Name = "Snipcode Prod subnet group"
Expand Down Expand Up @@ -52,25 +84,31 @@ resource "aws_security_group" "rds_sg" {
}

resource "aws_db_instance" "database" {
identifier = "${var.project_name}-backend-${var.environment}"
allocated_storage = 20
engine = "mysql"
engine_version = "8.0.39"
instance_class = "db.t3.micro"
db_name = data.doppler_secrets.prod.map.DATABASE_NAME
username = data.doppler_secrets.prod.map.ADMIN_USER
password = data.doppler_secrets.prod.map.ADMIN_PASSWORD
db_subnet_group_name = aws_db_subnet_group.default.name
vpc_security_group_ids = [aws_security_group.rds_sg.id]
multi_az = false
publicly_accessible = true
performance_insights_enabled = true
performance_insights_retention_period = 7 ## 7 days to stay in the free tier
skip_final_snapshot = true
allow_major_version_upgrade = false
auto_minor_version_upgrade = true
identifier = "${var.project_name}-backend-${var.environment}"
allocated_storage = 20
engine = "mysql"
engine_version = "8.0.39"
instance_class = "db.t3.micro"
db_name = data.doppler_secrets.prod.map.DATABASE_NAME
username = data.doppler_secrets.prod.map.ADMIN_USER
password = data.doppler_secrets.prod.map.ADMIN_PASSWORD
port = data.doppler_secrets.prod.map.PORT
db_subnet_group_name = aws_db_subnet_group.default.name
vpc_security_group_ids = [aws_security_group.rds_sg.id]
multi_az = false
publicly_accessible = true
skip_final_snapshot = true
allow_major_version_upgrade = false
auto_minor_version_upgrade = true
# performance_insights_enabled = true
# performance_insights_retention_period = 7

lifecycle {
prevent_destroy = true
}

tags = {
Name = "Snipcode Prod RDS Instance"
}
}

0 comments on commit 2d2d8c3

Please sign in to comment.