【问题标题】:GCP - Create postgreSQL through terraform - with VPCGCP - 通过 terraform 创建 postgreSQL - 使用 VPC
【发布时间】:2021-05-24 00:29:30
【问题描述】:

在 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"
  }
}

【问题讨论】:

  • 有很多关于这个案例使用的教程。请试一试,如果您有任何具体问题,请在此处发布medium.com/swlh/…

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


【解决方案1】:

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

ip_configuration {
      ipv4_enabled    = false
      private_network = <VPC_FULL_PATH_NAME>
    }

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

【讨论】:

    【解决方案2】:

    根据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
      }
    }
    

    【讨论】:

    • 我关注了这个 git repo,它抛出了同样的错误。错误:错误,未能创建实例 example-postgres-public-3f6e41c6:googleapi:错误 400:无效请求:组织策略检查失败:此实例的外部 IP 违反了约束/sql.restrictPublicIp 强制执行
    • 这实际上并没有显示如何指定专用网络
    • 我从朋友那里得到了答案。 ip_configuration = { ipv4_enabled = false private_network = require_ssl = true }
    猜你喜欢
    • 2021-11-25
    • 2021-12-14
    • 2020-07-21
    • 2015-05-18
    • 2021-09-26
    • 1970-01-01
    • 2021-09-02
    • 2019-07-14
    • 1970-01-01
    相关资源
    最近更新 更多