Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[google_sql_database_instance] If using CMEK on SQL instance with Replicas in the same region, the instance is recreated every terraform run #11269

Assignees
Labels

Comments

@jmymy
Copy link

jmymy commented Mar 14, 2022

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request.
  • Please do not leave +1 or me too comments, they generate extra noise for issue followers and do not help prioritize the request.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.
  • If an issue is assigned to the modular-magician user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If an issue is assigned to a user, that user is claiming responsibility for the issue. If an issue is assigned to hashibot, a community member has claimed the issue already.

Terraform Version

Terraform v1.1.7
on darwin_arm64
+ provider registry.terraform.io/hashicorp/external v2.2.0
+ provider registry.terraform.io/hashicorp/google v4.13.0
+ provider registry.terraform.io/hashicorp/google-beta v4.13.0
+ provider registry.terraform.io/hashicorp/kubernetes v2.8.0
+ provider registry.terraform.io/hashicorp/null v3.1.0
+ provider registry.terraform.io/hashicorp/random v3.1.0
+ provider registry.terraform.io/hashicorp/tfe v0.28.1
+ provider registry.terraform.io/hashicorp/time v0.7.2

Affected Resource(s)

  • google_sql_database_instance

Terraform Configuration Files

resource "google_sql_database_instance" "replicas" {
  provider             = google-beta
  for_each             = local.replicas
  project              = var.project_id
  name                 = "${local.master_instance_name}-replica${var.read_replica_name_suffix}${each.value.name}"
  database_version     = var.database_version
  region               = join("-", slice(split("-", lookup(each.value, "zone", var.zone)), 0, 2))
  master_instance_name = google_sql_database_instance.default.name
  deletion_protection  = var.read_replica_deletion_protection
  encryption_key_name  = (join("-", slice(split("-", lookup(each.value, "zone", var.zone)), 0, 2))) == var.region ? null : each.value.encryption_key_name

  replica_configuration {
    failover_target = false
  }

  settings {
    tier              = lookup(each.value, "tier", var.tier)
    activation_policy = "ALWAYS"

    dynamic "ip_configuration" {
      for_each = [lookup(each.value, "ip_configuration", {})]
      content {
        ipv4_enabled       = lookup(ip_configuration.value, "ipv4_enabled", null)
        private_network    = lookup(ip_configuration.value, "private_network", null)
        require_ssl        = lookup(ip_configuration.value, "require_ssl", null)
        allocated_ip_range = lookup(ip_configuration.value, "allocated_ip_range", null)

        dynamic "authorized_networks" {
          for_each = lookup(ip_configuration.value, "authorized_networks", [])
          content {
            expiration_time = lookup(authorized_networks.value, "expiration_time", null)
            name            = lookup(authorized_networks.value, "name", null)
            value           = lookup(authorized_networks.value, "value", null)
          }
        }
      }
    }
    dynamic "insights_config" {
      for_each = var.insights_config != null ? [var.insights_config] : []

      content {
        query_insights_enabled  = true
        query_string_length     = lookup(insights_config.value, "query_string_length", 1024)
        record_application_tags = lookup(insights_config.value, "record_application_tags", false)
        record_client_address   = lookup(insights_config.value, "record_client_address", false)
      }
    }

    disk_autoresize = lookup(each.value, "disk_autoresize", var.disk_autoresize)
    disk_size       = lookup(each.value, "disk_size", var.disk_size)
    disk_type       = lookup(each.value, "disk_type", var.disk_type)
    pricing_plan    = "PER_USE"
    user_labels     = lookup(each.value, "user_labels", var.user_labels)

    dynamic "database_flags" {
      for_each = lookup(each.value, "database_flags", [])
      content {
        name  = lookup(database_flags.value, "name", null)
        value = lookup(database_flags.value, "value", null)
      }
    }

    location_preference {
      zone = lookup(each.value, "zone", var.zone)
    }

  }

  depends_on = [google_sql_database_instance.default]

  lifecycle {
    ignore_changes = [
      settings[0].disk_size,
      settings[0].maintenance_window,
      encryption_key_name,
    ]
  }

  timeouts {
    create = var.create_timeout
    update = var.update_timeout
    delete = var.delete_timeout
  }
}

Debug Output

Panic Output

Expected Behavior

The replica instance remains unchanged and does not need to be recreated

Actual Behavior

encryption_key_name is required to be null for replica in same region. If you provide encryption_key_name anything other than null value for replica in same region you will receive following error.

Error: Error, failed to create instance instance-name-1: googleapi: Error 400: Invalid request: Disk encryption config should not be specified for replica that is in the same region of primary.., invalid

However, when you create with a null encryption_key_name, the google API returns the encryption_key_name for the main instance, now on the replica instance. this causes a drift on the state, and forces the replica to be recreated every terraform run

Steps to Reproduce

Create a sql database and replica with a CMEK. the replica must be in the same region as the main instance.

  1. terraform apply

Important Factoids

References

@jmymy jmymy added the bug label Mar 14, 2022
@jmymy jmymy changed the title If using CMEK on Postgres with Replicas, the instance is recreated every terraform run [google_sql_database_instance] If using CMEK on Postgres with Replicas, the instance is recreated every terraform run Mar 14, 2022
@jmymy jmymy changed the title [google_sql_database_instance] If using CMEK on Postgres with Replicas, the instance is recreated every terraform run [google_sql_database_instance] If using CMEK on SQL instance with Replicas in the same region, the instance is recreated every terraform run Mar 14, 2022
@megan07 megan07 self-assigned this Mar 15, 2022
@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.