想要使用terraform在gcp中的一个单独的vpc上创建一个具有私有和公共ip的云sql实例



我尝试使用terraform在单独的vpc中配置一个具有私有和公共ip的云sql实例。可以从单独的vpc在该实例上分配私有ip,但不能同时分配公共ip。

这是我的密码。

resource "google_compute_global_address" "private_ip_address" {
provider = google-beta
name          = "private-ip-address"
purpose       = "VPC_PEERING"
address_type  = "INTERNAL"
prefix_length = 16
network       = "${var.vpc_self_link}"
}
resource "google_service_networking_connection" "private_vpc_connection" {
provider = google-beta
network                 = "${var.vpc_self_link}"
service                 = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
}

# create database instance
resource "google_sql_database_instance" "instance" {
name = "test-${var.project_id}"
region = "us-central1"
database_version = "${var.db_version}"
depends_on = [google_service_networking_connection.private_vpc_connection]
settings {
tier = "${var.db_tier}"
activation_policy = "${var.db_activation_policy}"
disk_autoresize = "${var.db_disk_autoresize}"
disk_size = "${var.db_disk_size}"
disk_type = "${var.db_disk_type}"
pricing_plan = "${var.db_pricing_plan}"
database_flags {
name  = "slow_query_log"
value = "on"
}
ip_configuration {
ipv4_enabled = "false"
private_network = "projects/${var.project_id}/global/networks/${var.vpc_name}"
}
}
}

但当我试图传递下面的参数-来分配公共ip时,由于private_network标志,它给出了错误。

ipv4_enabled = "true"

请让我知道如何从自定义或单独的vpc(而不是默认的(中解决私有和公共ip的问题。

根据文档,您不能

ipv4_enabled-(可选(是否应为此云SQL实例分配一个公共ipv4地址。必须启用ipv4_enabled,或者必须配置private_network。

打开功能请求。

这个问题很老了,已经做了很多更新,到那时你可能已经解决了,尽管如此,你只想确认以下两种情况都可以使用公共和私有IP,并且可以从头开始创建资源,或者修改以前只使用公共IP的现有实例。

terraform {
required_providers {
google = {
source  = "hashicorp/google"
version = "3.5.0"
}
}
backend "gcs" {
bucket  = "<BUCKET>"
prefix  = "<PREFIX>"
}
}
provider "google" {
project = var.project
region  = var.region
zone    = var.zone
}

### VPC
resource "google_compute_network" "private_network" {
name                    = "private-network"
auto_create_subnetworks = "false"
}
resource "google_compute_global_address" "private_ip_address" {
name          = "private-ip-address"
purpose       = "VPC_PEERING"
address_type  = "INTERNAL"
prefix_length = 16
network       = google_compute_network.private_network.id
}
resource "google_service_networking_connection" "private_vpc_connection" {
network                 = google_compute_network.private_network.id
service                 = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
}

### INSTANCE
resource "google_sql_database_instance" "instance" {
name             = "<INSTANCE>"
region           = var.region
database_version = "MYSQL_5_7"
depends_on = [google_service_networking_connection.private_vpc_connection]
settings {
tier = "db-f1-micro"
ip_configuration {
ipv4_enabled    = true
private_network = google_compute_network.private_network.id
authorized_networks {
name = "default"
value = "0.0.0.0/0"
}
}
}
}

### DATABASE
resource "google_sql_database" "database" {
name     = "tf-db"
instance = google_sql_database_instance.instance.name
}

### USER
resource "google_sql_user" "users" {
name     = var.sql_user
password = var.sql_pw
instance = google_sql_database_instance.instance.name
}

相关内容

  • 没有找到相关文章

最新更新