How to create an Aurora replica of a PostgreSQL RDS database using Terraform?

Viewed 1056

I've been tasked with updating our Postgres database to Aurora Postgres, and the first step is throwing me off. We use Terraform to manage our infrastructure, have a primary and a single replica of the database. So far, I've run into many problems using Terraform to do what is really easy in the console.

Desired outcome: run terraform apply and have a freshly created Aurora Postgres replica show up and be readable by our applications.

Code (I left a bunch of potentially useless info, but wanted to make it as easy as possible to understand):

resource "aws_db_instance" "rds_postgres" {
  identifier                   = "postgres-database"
  allocated_storage            = "250"
  engine                       = "postgres"
  engine_version               = "11.6"
  instance_class               = "db.t3.large"
  name                         = "prod_db"
  username                     = "admin"
  password                     = "p@ssw0rd"
  db_subnet_group_name         = "subnet-group-name"
  vpc_security_group_ids       = ["sg-xxxxxxx"]
  storage_encrypted            = true
}

resource "aws_db_instance" "rds_postgres_r1" {
  identifier                   = "postgres-database-r1"
  allocated_storage            = "250"
  engine                       = "postgres"
  engine_version               = "11.6"
  instance_class               = "db.t3.large"
  name                         = "prod_db"
  username                     = "admin"
  vpc_security_group_ids       = ["sg-xxxxxxx"]
  replicate_source_db          = "${aws_db_instance.rds_postgres.identifier}"
  storage_encrypted            = true
}

resource "aws_rds_cluster" "default" {
  cluster_identifier            = "postgres-aurora-cluster"
  database_name                 = "prod_db"
  master_username               = "admin"
  master_password               = "p@ssw0rd"
  storage_encrypted             = true
  kms_key_id                    = "alias/kms/rds"
  vpc_security_group_ids        = ["sg-xxxxxxx"]
  db_subnet_group_name          = "subnet-group-name"
  engine                        = "aurora-postgresql"
  engine_version                = "11.6"
  replication_source_identifier = "${aws_db_instance.rds_postgres.arn}"

  lifecycle {
    ignore_changes = [
      "id",
      "kms_key_id",
      "cluster_identifier"
    ]
  }
}

resource "aws_rds_cluster_instance" "default" {
  identifier                      = "postgres-aurora-1"
  cluster_identifier              = "${aws_rds_cluster.default.id}"
  instance_class                  = "db.t3.large"
  db_subnet_group_name            = "subnet-group-name"
  publicly_accessible             = false
  engine                          = "aurora-postgresql"
  engine_version                  = "11.6"

  lifecycle {
    ignore_changes = [
      "identifier",
      "cluster_identifier"
    ]
  }
}

Terraform apply will faithfully create a new cluster and single instance initially labeled as a Reader class, and replicate data. However, after the replication is complete, the new aurora instance swaps to Writer.

What am I doing wrong? My next steps are promoting this new Aurora cluster to the primary DB, scaling up readers, and cutting the original Postgres primary out completely, but I am blocked. I'm not a Terraform wizard.

0 Answers
Related