Issues with Subnets in my secondary instance

Viewed 51

Please have some grace because I just started learning AWS. I am trying to launch a PostgreSQL DB from Terraform and I keep running into this subnet error

Error: error creating RDS cluster: DBSubnetGroupNotFoundFault: DB subnet group 'cse-cr' does not exist.
│   status code: 404, request id: 6461e755-8118-41ee-8baf-5678e28c39aa
│ 
│   with aws_rds_cluster.secondary,
│   on maindb.tf line 67, in resource "aws_rds_cluster" "secondary":
│   67: resource "aws_rds_cluster" "secondary" {

I tried adding a second resource section specifically for the secondary cluster/instance but that also did not work. What am I doing wrong...?

Here is my code:

provider "aws" {
  alias  = "primary"
  region = "us-east-2"
}

provider "aws" {
  alias  = "secondary"
  region = "us-east-1"
}

resource "aws_rds_global_cluster" "example" {
  global_cluster_identifier = "global-test"
  engine                    = "aurora-postgresql"
  engine_version            = "13.4"
  database_name             = "example_db"
}

variable "database_name" {
  description = "What should RDS name the initial db?"
  default     = "variableWithSomeDefault"
}


data "aws_subnet" "subnet1" {
  id = "subnet-numhere"
}

data "aws_subnet" "subnet2" {
  id = "subnet-numhere"
}

resource "aws_db_subnet_group" "default" {
  name        = "cse-cr"
  description = "Private subnets for RDS instance"
  subnet_ids  = [data.aws_subnet.subnet1.id, data.aws_subnet.subnet2.id]
}


resource "aws_rds_cluster" "primary" {
  provider                  = aws.primary
  engine                    = "aurora-postgresql"
  engine_version            = "13.4"
  cluster_identifier        = "test-primary-cluster"
  master_username           = "username"
  master_password           = "somepass123"
  database_name             = var.database_name
  global_cluster_identifier = aws_rds_global_cluster.example.global_cluster_identifier
  db_subnet_group_name      = "cse-cr"
  skip_final_snapshot       = true
}

resource "aws_rds_cluster_instance" "primary" {
  provider           = aws.primary
  engine             = "aurora-postgresql"
  engine_version     = "13.4"
  identifier         = "primaryinstancetest"
  cluster_identifier = "${aws_rds_cluster.primary.cluster_identifier}"
  instance_class     = "db.r5.large"
  db_subnet_group_name = "cse-cr"
}



resource "aws_rds_cluster" "secondary" {
  provider                  = aws.secondary
  engine                    = "aurora-postgresql"
  engine_version            = "13.4"
  cluster_identifier        = "test-secondary-cluster"
  global_cluster_identifier = aws_rds_global_cluster.example.global_cluster_identifier
  skip_final_snapshot       = true
  db_subnet_group_name      = "cse-cr"
  

  depends_on = [
    aws_rds_cluster_instance.primary
  ]
}

resource "aws_rds_cluster_instance" "secondary" {
  provider             = aws.secondary
  engine               = "aurora-postgresql"
  engine_version       = "13.4"
  identifier           = "secondaryinstancetest"
  cluster_identifier   = "${aws_rds_cluster.secondary.cluster_identifier}"
  instance_class       = "db.r5.large"
  db_subnet_group_name = "cse-cr"
}
1 Answers

Terraform tries to create the RDS cluster (aws_rds_cluster.secondary) before the DB subnet group was (aws_db_subnet_group.default) was provisioned. You have to have a dependency between them in order for Terraform to be able to detect the provisioning order. To solve this, I recommend referencing the aws_db_subnet_group.default.name whenever it asks for the subnet group name. For example:

resource "aws_rds_cluster_instance" "primary" {
  provider           = aws.primary
  engine             = "aurora-postgresql"
  engine_version     = "13.4"
  identifier         = "primaryinstancetest"
  cluster_identifier = aws_rds_cluster.primary.cluster_identifier
  instance_class     = "db.r5.large"
  db_subnet_group_name = aws_db_subnet_group.default.name # there is no need to hardcode "cse-cr" here
}

Or another example in the secondary cluster:

resource "aws_rds_cluster_instance" "secondary" {
  provider             = aws.primary
  engine               = "aurora-postgresql"
  engine_version       = "13.4"
  identifier           = "secondaryinstancetest"
  cluster_identifier   = aws_rds_cluster.secondary.cluster_identifier
  instance_class       = "db.r5.large"
  db_subnet_group_name = aws_rds_cluster.primary.cluster_identifier
}

Please note, I removed the secondary provider from the second cluster. You can not have a subnet (or a VPC) which spans to 2 different regions!

Related