Skip to content

Commit

Permalink
feat(terraform-rds): init rds files for terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
neilscallywag committed Apr 18, 2024
1 parent 050734c commit 5912ff0
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
40 changes: 40 additions & 0 deletions terraform/modules/rds-postgresql/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# output "aurora_cluster_arn" {
# value = aws_rds_cluster.aurora_cluster.arn
# }

# output "aurora_cluster_primary_endpoint_address" {
# value = aws_rds_cluster.aurora_cluster.endpoint
# }

# output "aurora_cluster_reader_endpoint_address" {
# value = aws_rds_cluster.aurora_cluster.reader_endpoint
# }

# output "aurora_cluster_instance_arn_1" {
# value = aws_rds_cluster_instance.aurora_cluster_instance_replica_1.arn
# }

# output "aurora_cluster_instance_arn_2" {
# value = aws_rds_cluster_instance.aurora_cluster_instance_replica_2.arn
# }

# # Debugging purposes
# # Connect to this specific replica
# # output "aurora_cluster_instance_1_endpoint_address" {
# # value = aws_rds_cluster_instance.aurora_cluster_instance_replica_1.endpoint
# # }

# # output "aurora_cluster_instance_2_endpoint_address" {
# # value = aws_rds_cluster_instance.aurora_cluster_instance_replica_2.endpoint
# # }


output "postgres_instance_arn" {
value = aws_db_instance.postgresql_master.arn
description = "The Amazon Resource Name (ARN) of the PostgreSQL instance."
}

output "postgres_instance_endpoint_address" {
value = aws_db_instance.postgresql_master.address
description = "The connection endpoint for the PostgreSQL database instance."
}
95 changes: 95 additions & 0 deletions terraform/modules/rds-postgresql/rds-postgresql.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
variable "project_name" {}
variable "environment" {}

variable "aws_vpc_id" {}

# variable "eks_cluster_security_group_id" {}

variable "database_private_subnet_1_id" {}
# variable "database_private_subnet_2_id" {}

# variable "availability_zone_1" {}
# variable "availability_zone_2" {}

# variable "app_domain_zone_id" {}

data "aws_secretsmanager_secret" "postgres_credentials" {
name = "rds_postgres_credentials"
}

data "aws_secretsmanager_secret_version" "current_postgres_credentials" {
secret_id = data.aws_secretsmanager_secret.postgres_credentials.id
}

resource "aws_db_subnet_group" "private_db_subnet_group" {
name = "${var.project_name}-private-db-subnet-group-${var.environment}"
# subnet_ids = [var.database_private_subnet_1_id, var.database_private_subnet_2_id]
subnet_ids = [var.database_private_subnet_1_id]



tags = {
Name = "${var.project_name}-private-db-subnet-group-${var.environment}"
Environment = var.environment
}
}

resource "aws_db_instance" "postgresql_master" {
identifier = "${var.project_name}-postgresql-${var.environment}"
instance_class = "db.t3.micro"
allocated_storage = 20
max_allocated_storage = 1000
engine = "postgres"
engine_version = "16.1"
username = jsondecode(data.aws_secretsmanager_secret_version.current_postgres_credentials.secret_string)["postgresql_username"]
password = jsondecode(data.aws_secretsmanager_secret_version.current_postgres_credentials.secret_string)["postgresql_password"]
db_subnet_group_name = aws_db_subnet_group.private_db_subnet_group.name
vpc_security_group_ids = [aws_security_group.postgres_sg.id]
skip_final_snapshot = true
multi_az = false

tags = {
Environment = var.environment
}
}

resource "aws_security_group" "postgres_sg" {
name = "${var.project_name}-postgres-sg-${var.environment}"
description = "Security group for PostgreSQL"
vpc_id = var.aws_vpc_id

# ingress {
# from_port = 5432
# to_port = 5432
# protocol = "tcp"
# security_groups = [ var.eks_cluster_security_group_id ]
# }

# ingress {
# from_port = 5432
# to_port = 5432
# protocol = "tcp"
# security_groups = [ "sg-0287d0f475a97bc39" ]
# }
ingress {
from_port = 5432
to_port = 5432
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

# resource "aws_route53_record" "postgres_endpoint_cname" {
# zone_id = var.app_domain_zone_id
# name = "postgres-primary.eduhelper.info"
# type = "CNAME"
# ttl = "300"
# records = [aws_db_instance.postgresql_master.address]
# }

0 comments on commit 5912ff0

Please sign in to comment.