terraform-google-modules / terraform-google-sql-db

Creates a Cloud SQL database instance
https://registry.terraform.io/modules/terraform-google-modules/sql-db/google
Apache License 2.0
263 stars 422 forks source link

bug: Invalid Index on disabled default db and additional_databases #624

Closed Dariusch closed 1 month ago

Dariusch commented 1 month ago

TL;DR

The latest v21 release introduced a bug regarding a value lookup, Invalid index.

Expected behavior

No response

Observed behavior

│ Error: Invalid index
│
│   on .terraform/modules/postgresql/modules/postgresql/main.tf line 47, in locals:
│   47:   database_name = var.enable_default_db ? google_sql_database.default[0].name : (length(local.databases) > 0 ? google_sql_database.additional_databases[0].name : "")
│     ├────────────────
│     │ google_sql_database.additional_databases is object with 10 attributes
│
│ The given key does not identify an element in this collection value. An object only supports looking up attributes by name, not by numeric index.
╵

Terraform Configuration

module "postgresql" {
  source  = "GoogleCloudPlatform/sql-db/google//modules/postgresql"
  version = "21.0.0"

  ...

  enable_default_db = false

  additional_databases = [{ name = "any_db", charset = "UTF8", collation = "en_US.UTF8" }]

  ...

Terraform Version

Terraform v1.9.3
on darwin_arm64
+ provider registry.terraform.io/hashicorp/google v5.39.0
+ provider registry.terraform.io/hashicorp/google-beta v5.39.0
...

Additional information

The condition was introduced in https://github.com/terraform-google-modules/terraform-google-sql-db/pull/612/files#diff-5af99345140d405f1b9683608683cd8e45b6cce368ba0446706651ca3a92f041R47

joaoluchetti commented 1 month ago

+1 We are unable to update to v21.0.0 due to this.

cwillison94 commented 1 month ago

+1 We are also experiencing this.

imrannayer commented 1 month ago

@q2w your PR #612 is causing this error. Can you plz check and send a fix?

imrannayer commented 1 month ago

Fixed in #625