|
| 1 | +# variables for Coder Workspace Reference |
| 2 | +variable "workspace_name" { |
| 3 | + type = string |
| 4 | + default = "awsragproto" |
| 5 | +} |
| 6 | + |
| 7 | +variable "eks_cluster_name" { |
| 8 | + description = "Name of the EKS cluster" |
| 9 | + type = string |
| 10 | + default = "coder-aws-cluster" |
| 11 | +} |
| 12 | + |
| 13 | +#Variables for Aurora PostgreSQL Serverless v2 |
| 14 | + |
| 15 | +variable "database_name" { |
| 16 | + description = "Name of the database to be created" |
| 17 | + type = string |
| 18 | + default = "mydb" |
| 19 | +} |
| 20 | +variable "db_master_username" { |
| 21 | + description = "Master username for the database" |
| 22 | + type = string |
| 23 | + default = "dbadmin" |
| 24 | +} |
| 25 | +variable "db_master_password" { |
| 26 | + description = "Master password for the database" |
| 27 | + type = string |
| 28 | + default = "YourStrongPasswordHere1" # Consider using AWS Secrets Manager for production |
| 29 | +} |
| 30 | + |
| 31 | +# Get EKS cluster info |
| 32 | +data "aws_eks_cluster" "current" { |
| 33 | + name = var.eks_cluster_name # Add this variable |
| 34 | +} |
| 35 | + |
| 36 | +# Use EKS VPC |
| 37 | +data "aws_vpc" "existing_vpc" { |
| 38 | + id = data.aws_eks_cluster.current.vpc_config[0].vpc_id |
| 39 | +} |
| 40 | + |
| 41 | +# Get private subnets from EKS |
| 42 | +data "aws_subnets" "private" { |
| 43 | + filter { |
| 44 | + name = "vpc-id" |
| 45 | + values = [data.aws_vpc.existing_vpc.id] |
| 46 | + } |
| 47 | + |
| 48 | + tags = { |
| 49 | + "kubernetes.io/role/internal-elb" = "1" |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +# Create a subnet group for Aurora instances using existing subnets |
| 54 | +resource "aws_db_subnet_group" "awsrag_aurora_subnet_group" { |
| 55 | + name = "${var.workspace_name}-sgrp" |
| 56 | + subnet_ids = data.aws_subnets.private.ids |
| 57 | + |
| 58 | + tags = { |
| 59 | + Name = "${var.workspace_name}-sgrp" |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +# Create security group for Aurora instances |
| 64 | +resource "aws_security_group" "awsrag_aurora_sg" { |
| 65 | + name = "${var.workspace_name}-sg" |
| 66 | + description = "Security group for Aurora PostgreSQL instances" |
| 67 | + vpc_id = data.aws_vpc.existing_vpc.id |
| 68 | + |
| 69 | + ingress { |
| 70 | + from_port = 5432 |
| 71 | + to_port = 5432 |
| 72 | + protocol = "tcp" |
| 73 | + cidr_blocks = ["0.0.0.0/0"] # Allow public access not restricted to the VPC |
| 74 | + } |
| 75 | + |
| 76 | + egress { |
| 77 | + from_port = 0 |
| 78 | + to_port = 0 |
| 79 | + protocol = "-1" |
| 80 | + cidr_blocks = ["0.0.0.0/0"] |
| 81 | + } |
| 82 | + |
| 83 | + tags = { |
| 84 | + Name = "${var.workspace_name}-sg" |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +# First Aurora PostgreSQL Serverless v2 instance |
| 89 | +resource "aws_rds_cluster" "awsrag_aurora_postgres_1" { |
| 90 | + cluster_identifier = "${var.workspace_name}-pgvector01" |
| 91 | + engine = "aurora-postgresql" |
| 92 | + engine_mode = "provisioned" |
| 93 | + engine_version = "16.6" |
| 94 | + database_name = var.database_name |
| 95 | + master_username = var.db_master_username |
| 96 | + master_password = var.db_master_password # Use AWS Secrets Manager in production |
| 97 | + db_subnet_group_name = aws_db_subnet_group.awsrag_aurora_subnet_group.name |
| 98 | + vpc_security_group_ids = [aws_security_group.awsrag_aurora_sg.id] |
| 99 | + skip_final_snapshot = true |
| 100 | + |
| 101 | + serverlessv2_scaling_configuration { |
| 102 | + min_capacity = 0.5 |
| 103 | + max_capacity = 1.0 |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +# Primary DB instance for the Aurora PostgreSQL cluster |
| 108 | +resource "aws_rds_cluster_instance" "awsrag_aurora_primary" { |
| 109 | + cluster_identifier = aws_rds_cluster.awsrag_aurora_postgres_1.id |
| 110 | + instance_class = "db.serverless" |
| 111 | + engine = "aurora-postgresql" |
| 112 | + engine_version = "16.6" |
| 113 | + db_subnet_group_name = aws_db_subnet_group.awsrag_aurora_subnet_group.name |
| 114 | + identifier = "${var.workspace_name}-primary" |
| 115 | +} |
| 116 | + |
| 117 | +# Outputs |
| 118 | +output "aurora_postgres_1_endpoint" { |
| 119 | + value = aws_rds_cluster.awsrag_aurora_postgres_1.endpoint |
| 120 | +} |
| 121 | + |
| 122 | +output "aurora_postgres_1_reader_endpoint" { |
| 123 | + value = aws_rds_cluster.awsrag_aurora_postgres_1.reader_endpoint |
| 124 | +} |
0 commit comments