gpt4 book ai didi

google-cloud-platform - GCP 上的 Terraform 共享 VPC - 静态内部 IP 地址

转载 作者:行者123 更新时间:2023-12-04 01:11:31 29 4
gpt4 key购买 nike

我正在尝试编写自动化程序以在 GCP 上的共享 VPC 中部署实例。我有一个主机网络项目和一个服务项目。我可以在宿主项目(资源“google_compute_address”“internal”)中创建静态内部 IP 地址资源,我在其中指定了 VPC 宿主项目 (NET_HUB_PROJ),但在创建实例时无法使用它。我收到以下错误:

google_compute_instance.compute: Error creating instance: googleapi: 
Error 400: Invalid value for field
'resource.networkInterfaces[0].networkIP': '10.128.0.10'. IP address
'projects/prototype-network-hub/regions/us-central1/addresses/bh-int-
ip' (10.128.0.10) is reserved by another project., invalid

我的计算模块:

data "google_compute_image" "image" {
name = "${var.IMAGE_NAME}"
project = "${var.IMAGE_PROJECT}"
}

resource "google_compute_address" "internal" {
name = "${var.NAME}-int-ip"
address_type = "INTERNAL"
address = "${var.PRIVATE_IP}"
subnetwork = "${var.NET_HUB_SUBNETWORK}"
region = "${var.NET_HUB_REGION}"
project = "${var.NET_HUB_PROJ}"
}

resource "google_compute_address" "external" {
count = "${var.EXT_IP_CREATE ? 1 : 0}"
name = "${var.NAME}-ext-ip"
address_type = "EXTERNAL"
region = "${var.REGION}"
}

resource "google_compute_instance" "compute" {
depends_on = ["google_compute_address.external"]
name = "${var.NAME}"
machine_type = "${var.MACHINE_TYPE}"
zone = "${var.ZONE}"
can_ip_forward = "${var.CAN_IP_FORWARD}"
deletion_protection ="${var.DELETION_PROTECTION}"
allow_stopping_for_update = "${var.ALLOW_STOPPING_FOR_UPDATE}"
tags = ["allow-ssh"]
metadata = {
"network" = "${var.NETWORK}"
"env" = "${var.ENV}"
"role" = "${var.ROLE}"
"region" = "${var.REGION}"
"zone" = "${var.ZONE}"
}
labels = {
"network" = "${var.NETWORK}"
"env" = "${var.ENV}"
"role" = "${var.ROLE}"
"region" = "${var.REGION}"
"zone" = "${var.ZONE}"
}
boot_disk {
device_name = "${var.NAME}"
auto_delete = "${var.BOOT_DISK_AUTO_DELETE}"
initialize_params {
size = "${var.BOOT_DISK_SIZE}"
type = "${var.BOOT_DISK_TYPE}"
image = "${data.google_compute_image.image.self_link}"
}
}

network_interface {
network_ip = "${google_compute_address.internal.address}"
subnetwork_project = "${var.NET_HUB_PROJ}"
subnetwork = "projects/prototype-network-hub/regions/us-central1/subnetworks/custom"
access_config {
nat_ip = "${element(concat(google_compute_address.external.*.address, list("")), 0)}"
}
}
service_account {
scopes = ["service-control", "service-management", "logging-write", "monitoring-write", "storage-ro", "https://www.googleapis.com/auth/trace.append" ]
}
}

最终目标是实现以下目标: enter image description here

最佳答案

编辑(新答案):根据 GCP 文档,如果您要在不同项目的共享 VPC 上保留内部 IP,则静态内部 IP 必须属于服务项目(而不是代码中的主机网络项目)。看这里: https://cloud.google.com/vpc/docs/provisioning-shared-vpc#reserve_internal_ip

鉴于 shared-vpc 不太可能在您的 TF 代码库中找到,您必须使用 data 来获取 self_link 用于 google_compute_address 的子网。类似于以下内容:

data "google_compute_subnetwork" "subnet" {
name = "${var.NET_HUB_SUBNETWORK}"
project = "${var.NET_HUB_PROJ}"
region = "${var.NET_HUB_REGION}"
}

resource "google_compute_address" "internal" {
name = "${var.NAME}-int-ip"
address_type = "INTERNAL"
address = "${var.PRIVATE_IP}"
subnetwork = "${data.google_compute_subnetwork.subnet.self_link}"
}

这应该在您的服务项目下创建资源,但地址位于指定的子网内。

当您部署您的实例时,您应该会在分配实例的 VM 实例选项卡上的 internal_ip 列下看到它被引用。

(后代的旧答案):不幸的是,google_compute_address 不包含像 google_compute_instance 这样的 subnetwork_project。解决此问题的方法是为 google_compute_address 中的 subnetwork 字段提供完整的 URL。类似于以下内容:

resource "google_compute_address" "internal" {
name = "${var.NAME}-int-ip"
address_type = "INTERNAL"
address = "${var.PRIVATE_IP}"
subnetwork = "https://www.googleapis.com/compute/v1/projects/${var.NET_HUB_PROJ}/regions/${var.NET_HUB_REGION}/subnetworks/${var.NET_HUB_SUBNETWORK}"
}

关于google-cloud-platform - GCP 上的 Terraform 共享 VPC - 静态内部 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55362914/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com