hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.66k stars 9.04k forks source link

Unable to create multi AZ aurora serverless v2 by terraform #26248

Open ShahbazKlarna opened 1 year ago

ShahbazKlarna commented 1 year ago

Community Note

Description

I'm following terraform documentation to create Aurora serverless v2 https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/rds_cluster#rds-serverless-v2-cluster

In terraform documentation, they have not mentioned how to create Aurora serverless v2 with multi AZ (read replica in other region for failover). Although, in aws console, I can create multi AZ Aurora serverless v2.

Might be, the documentation is not clear or the functionality is not supported by terraform yet. Any input if there is a way to create Aurora serverless v2 with multi AZ using terraform?

Affected Resource(s)

MrMikeFloyd commented 1 year ago

@ShahbazKlarna I have the same issue as you and played around with terraform for a bit to find an automated way of ensuring my Aurora Serverless v2 is multi-az (my requirement being auto-failover as well). Interestingly, specifying the availability zones to use in the cluster resource and then specifying a count >1 in the cluster instance resource seems to do the trick:

resource "aws_rds_cluster" "cluster-test-multiaz" {
  cluster_identifier        = "test-multiaz"
  engine                    = "aurora-postgresql"
  engine_mode               = "provisioned"
  engine_version            = "13.6"
  availability_zones        = ["eu-central-1a", "eu-central-1b", "eu-central-1c"]
  ...
}

resource "aws_rds_cluster_instance" "instance-test-multiaz" {
  count              = 2
  instance_class     = "db.serverless"
  ...
}

Applying the above will provision a reader instance alongside the writer instance in a different az. However, as count is a terraform internal argument, I have no idea what this actually does in aws. I tested the above in a number of ways and the standby instance always ended up being provisioned in a different az as the writer. However I haven't been able to verify if it is guaranteed to be provisioned in a different az, and if using count for this is a good idea.

Would the terraform folks be able to confirm? That would be lovely 😸

ljluestc commented 10 months ago
resource "aws_rds_cluster" "cluster-multiaz" {
  cluster_identifier = "multiaz-cluster"
  engine            = "aurora-postgresql"
  engine_mode       = "serverless"
  availability_zones = ["us-east-1a", "us-east-1b"]  # Specify your desired AZs here
  database_name     = "mydb"
  master_username   = "admin"
  master_password   = "admin12345"  # Replace with your password

  scaling_configuration {
    auto_pause           = true
    max_capacity         = 32
    min_capacity         = 2
    seconds_until_auto_pause = 300
    timeout_action       = "ForceApplyCapacityChange"
  }
}

resource "aws_rds_cluster_instance" "instance-multiaz" {
  count              = 2  # Create two instances, one in each AZ
  cluster_identifier = aws_rds_cluster.cluster-multiaz.id
  instance_class     = "db.r5.large"  # Specify your desired instance class
}

# Output the cluster endpoint
output "cluster_endpoint" {
  value = aws_rds_cluster.cluster-multiaz.endpoint
}