You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级时Terraform销毁RDS集群内实例的问题咨询与解决方案寻求

How to Prevent Terraform from Destroying RDS Cluster Instances During Engine Version Upgrade

Let's break down why this is happening and fix it step by step:

The Root Cause

Your aws_rds_cluster_instance resources are set to inherit the engine_version directly from the cluster with engine_version = aws_rds_cluster.rds_mysql.engine_version. For Aurora clusters, AWS automatically syncs instance engine versions to match the cluster—you don't need to manage this attribute on the instance level.

Terraform's AWS provider marks engine_version on aws_rds_cluster_instance as a force-replacement attribute, meaning any change to it triggers a destroy-and-recreate cycle. When you update the cluster's version, Terraform sees the instance's version as changing and tries to replace the instance, even though AWS would handle this upgrade in-place automatically.

Fix 1: Remove Explicit Engine Version from Cluster Instances

First, delete the engine_version = aws_rds_cluster.rds_mysql.engine_version line from your aws_rds_cluster_instance resource. This lets AWS handle syncing the instance version to the cluster, instead of Terraform trying to manage it.

Fix 2: Adjust Lifecycle Rules for Instances

Keep the ignore_changes = [engine_version] rule to ensure Terraform doesn't try to track or modify this attribute on instances. Also, do not use create_before_destroy = true for RDS cluster instances—since instance identifiers must be unique in your AWS account, creating a new instance with the same name as the existing one will throw the DBInstanceAlreadyExists error you saw.

Modified Terraform Config

Here's the adjusted instance resource:

resource "aws_rds_cluster_instance" "cluster_instances" {
  count                 = var.engine_mode == "serverless" ? 0 : var.cluster_instance_count
  identifier            = "${var.cluster_identifier}-${count.index}"
  cluster_identifier    = aws_rds_cluster.rds_mysql.id
  instance_class        = var.instance_class
  engine                = var.engine
  # Remove the explicit engine_version line here
  db_subnet_group_name  = var.create_db_subnet_group == "true" ? aws_db_subnet_group.rds_subnet_group[0].id : var.db_subnet_group_name
  db_parameter_group_name = var.create_db_parameter_group == "true" ? aws_db_parameter_group.rds_instance_parameter_group[0].id : var.db_parameter_group
  apply_immediately     = var.apply_immediately
  auto_minor_version_upgrade = var.auto_minor_version_upgrade

  lifecycle {
    create_before_destroy = false # Keep this false to avoid duplicate instance errors
    ignore_changes       = [engine_version] # Ensure Terraform ignores this attribute
  }
}

For the cluster resource, make sure allow_major_version_upgrade is set correctly (you already have this as a variable, which is good) and apply_immediately is configured to your preference (note that major version upgrades might require downtime depending on your settings).

Verify the Fix

After making these changes, run terraform plan again. You should only see an in-place update for the aws_rds_cluster resource (changing engine_version), and no replacement actions for the cluster instances. AWS will handle upgrading the instances to match the cluster version automatically, just like it does in the console.

Why Your Previous Attempts Didn't Work

  • create_before_destroy = true: Failed because RDS instance identifiers are unique—you can't create a new instance with the same name as an existing one.
  • ignore_changes = [engine]: The issue wasn't with the engine attribute, it was with engine_version on the instance resource.

内容的提问来源于stack exchange,提问作者Maya Ray

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 21:39:08