GCP - 通过 terraform 创建 postgreSQL - 使用 VPC

时间:2021-02-21 16:06:52

标签: postgresql google-cloud-platform terraform terraform-provider-gcp

在 GCP 云中,我尝试通过 terraform 创建 PostgreSQL。我的组织政策不允许为公共 IP 创建。我必须使用私有 ip 或 VPC。我已经创建了 VPC,想用它来创建 postgresql。

这是我试过的代码。我不知道在哪里给 VPC。

resource "google_sql_database_instance" "master" {
  database_version = "POSTGRES_9_6"
  region = "europe-west1"
  settings {
    tier = "db-f1-micro"
    availability_type = "ZONAL"
  }
}

2 个答案:

答案 0 :(得分:1)

正如@gopalakrishnan 提到的,这样做的方法是将 ip_configuration 添加到模板中:

ip_configuration {
      ipv4_enabled    = false
      private_network = <VPC_FULL_PATH_NAME>
    }

可以在此 Medium Article 或 Terraform docs 中找到完整示例。

答案 1 :(得分:-1)

根据此github,您可以使用 Terraform 创建 Postgres Clod SQL 数据库。

/**
 * Copyright 2019 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

provider "google" {
  version = "~> 3.22"
}

provider "google-beta" {
  version = "~> 3.5"
}

provider "null" {
  version = "~> 2.1"
}

provider "random" {
  version = "~> 2.2"
}

module "postgresql-db" {
  source               = "../../modules/postgresql"
  name                 = var.db_name
  random_instance_name = true
  database_version     = "POSTGRES_9_6"
  project_id           = var.project_id
  zone                 = "us-central1-c"
  region               = "us-central1"
  tier                 = "db-f1-micro"

  deletion_protection = false

  ip_configuration = {
    ipv4_enabled        = true
    private_network     = null
    require_ssl         = true
    authorized_networks = var.authorized_networks
  }
}