Search code examples
amazon-web-servicesterraformamazon-rdsamazon-aurora

Upgrading AWS RDS aurora from serverless v1 to serverless v2 using terraform


I'm upgrading AWS rds aurora postgress cluster from serverless v1 to serverless v2 using terraform.

The steps that I followed:

  1. Creating snapshot of the serverless v1 cluster
  2. Creating provisioned db cluster from that snapshot
  3. Creating instance for provisioned db
  4. Creating snapshot of provisioned db cluster
  5. Finally creating the serverless v2 from the provisioned snapshot

Below is my terraform config for serverless v2:

 resource "aws_rds_cluster" "aurora_rds_serverless_v2" {
   cluster_identifier = "aurora-v2-serverless-cluster-new-197789"
   engine = local.env_config.rds_engine
   engine_version = local.env_config.rds_engine_version
   backup_retention_period = 14
   engine_mode = "serverless"
   snapshot_identifier= aws_db_cluster_snapshot.aurora_rds_provisioned_db_snapshot.id
   apply_immediately = true
   skip_final_snapshot = true
   enable_http_endpoint         = true
   timeouts {
     create = "120m"
   }
   scaling_configuration {
     auto_pause = true
     min_capacity = 2
     max_capacity = 4
   }
 }

My question is do I need to create separate terraform resource to define instance for serverless v2 or does it auto scale?

Something like this:

resource "aws_rds_cluster_instance" "aurora_serverless_instance" {
  count = 2 # Example: Two instances (one writer, one reader)
  cluster_identifier = aws_rds_cluster.aurora_rds_provisioned_db_v2.id
  instance_class = "db.r5.large"
  apply_immediately = true
}

Solution

  • Aurora Serverless v2 does not auto-scale the number of instances. The capacity of the serverless instance itself is auto-scaled. Serverless v2 uses cluster mode "provisioned" not "serverless", then you configure individual instances in the cluster to be either provisioned, or serverless instances, via the instance_class attribute. There's a complete example of this on the rds_cluster resource documentation page, here.

    resource "aws_rds_cluster" "example" {
      cluster_identifier = "example"
      engine             = "aurora-postgresql"
      engine_mode        = "provisioned"
      engine_version     = "13.6"
      database_name      = "test"
      master_username    = "test"
      master_password    = "must_be_eight_characters"
      storage_encrypted  = true
    
      serverlessv2_scaling_configuration {
        max_capacity = 1.0
        min_capacity = 0.5
      }
    }
    
    resource "aws_rds_cluster_instance" "example" {
      cluster_identifier = aws_rds_cluster.example.id
      instance_class     = "db.serverless"
      engine             = aws_rds_cluster.example.engine
      engine_version     = aws_rds_cluster.example.engine_version
    }