如何通过terraform设置GCP CloudSQL

时间:2016-12-08 07:11:28

标签: terraform

我想通过terraform设置GCP CloudSQL。我正在读这篇文章。 (https://www.terraform.io/docs/providers/google/r/sql_database_instance.html

我用这样的代码执行“terraform plan”。

resource "google_sql_database_instance" "master" {
    name             = "terraform-master"
    region           = "asia-northeasteast1"
    database_version = "MYSQL_5_6"
    project          = "test-141901"

    settings {
        tier = "db-f1-micro"
        replication_type = "SYNCHRONOUS"
        backup_configuration {
            enabled = true
            start_time = "17:00"
        }
        ip_configuration {
            ipv4_enabled = true
        }
        database_flags {
                name = "slow_query_log"
                value = "on"
                name = "character_set_server"
                value = "utf8mb4"
        }
    }
}

但我无法设置 MULTIPLE database_flags

settings.0.database_flags.#:                  "1"
settings.0.database_flags.0.name:             "character_set_server"
settings.0.database_flags.0.value:            "utf8mb4"

如何使用多个database_flags设置CloudSQL?我无法理解该文件中的“子列表支持”。

2 个答案:

答案 0 :(得分:2)

您应该可以使用多个database_flags块来执行此操作:

resource "google_sql_database_instance" "master" {
  name             = "terraform-master"
  region           = "us-central1"
  database_version = "MYSQL_5_6"
  project          = "test-project"

  settings {
    tier             = "db-f1-micro"
    replication_type = "SYNCHRONOUS"

    backup_configuration {
      enabled    = true
      start_time = "17:00"
    }

    ip_configuration {
      ipv4_enabled = true
    }

    database_flags {
      name  = "slow_query_log"
      value = "on"
    }

    database_flags {
      name  = "character_set_server"
      value = "utf8mb4"
    }
  }
}

以下是terraform plan的输出,上面的tf:

+ google_sql_database_instance.master
    database_version:                             "MYSQL_5_6"
    ip_address.#:                                 "<computed>"
    name:                                         "terraform-master"
    project:                                      "test-project"
    region:                                       "us-central1"
    self_link:                                    "<computed>"
    settings.#:                                   "1"
    settings.0.backup_configuration.#:            "1"
    settings.0.backup_configuration.0.enabled:    "true"
    settings.0.backup_configuration.0.start_time: "17:00"
    settings.0.database_flags.#:                  "2"
    settings.0.database_flags.0.name:             "slow_query_log"
    settings.0.database_flags.0.value:            "on"
    settings.0.database_flags.1.name:             "character_set_server"
    settings.0.database_flags.1.value:            "utf8mb4"
    settings.0.ip_configuration.#:                "1"
    settings.0.ip_configuration.0.ipv4_enabled:   "true"
    settings.0.replication_type:                  "SYNCHRONOUS"
    settings.0.tier:                              "db-f1-micro"
    settings.0.version:                           "<computed>"

答案 1 :(得分:0)

我想详细说明这个答案,因为我需要根据输入生成 database_flags块。假设您有一个变量

variable "database-flags" {
  type    = "map"
  default = {
    character_set_server = "utf8mb4"
    slow_query_log = "on"
  }
}

使用terraform v0.12.X可以写为

resource "google_sql_database_instance" "master" {
  name             = "terraform-master"
  region           = "us-central1"
  database_version = "MYSQL_5_6"
  project          = "test-project"

  settings {
    tier             = "db-f1-micro"
    replication_type = "SYNCHRONOUS"

    backup_configuration {
      enabled    = true
      start_time = "17:00"
    }

    ip_configuration {
      ipv4_enabled = true
    }

    dynamic "database_flags" {
      iterator = flag
      for_each = var.database-flags

      content {
        name = flag.key
        value = flag.value
      }
    }
  }
}

使用上述模式,您可以将代码的数据库创建部分用作模块,并让使用者决定应设置哪些标志