Terraform:设置对等连接时使用 exportCustomRoutes

Terraform:设置对等连接时使用 exportCustomRoutes

我正在尝试在两个 VPC 网络之间建立对等连接。

一个网络(中转网络)配置了静态路由,我希望将这些路由传播到对等网络。

以下是一个例子:

...

data "google_compute_network" "transit-network" {
  project       = var.transit_project_id
  name = var.transit_network
}

resource "google_compute_network_peering" "to-transit" {
  name = "${var.project}-transit"
  network = "${google_compute_network.vpc_network.self_link}"
  peer_network = data.google_compute_network.transit-network.self_link
}

resource "google_compute_network_peering" "from-transit" {
  name = "transit-${var.project}"
  network = data.google_compute_network.transit-network.self_link
  peer_network = "${google_compute_network.vpc_network.self_link}"
}

...

我知道这只是通过 GCP 控制台完成的,但是,我找不到通过 terraform 完成此操作的方法。

从 API 来看,通过使用字段peering.exchangeSubnetRoutes

编辑:我已经使用提供程序找到了解决方案google-beta

resource "google_compute_network_peering" "to-transit" {
  name = "${var.project}-transit"
  provider = "google-beta"
  network = "${google_compute_network.vpc_network.self_link}"
  peer_network = data.google_compute_network.transit-network.self_link
  import_custom_routes = true
}

resource "google_compute_network_peering" "from-transit" {
  name = "transit-${var.project}"
  provider = "google-beta"
  network = data.google_compute_network.transit-network.self_link
  peer_network = "${google_compute_network.vpc_network.self_link}"
  export_custom_routes = true
}

使用beta提供程序我可以指定import_custom_routesexport_custom_routes

答案1

谢谢!我在我的项目上检查了您的解决方案,它对我有用,只是语法略有改变:

resource "google_compute_network_peering" "vpc-network-to-transit-network" {
  provider             = google-beta
  name                 = "vpc-network-to-transit-network"
  network              = google_compute_network.vpc-network.self_link
  peer_network         = google_compute_network.transit-network.self_link 
  import_custom_routes = true
}

resource "google_compute_network_peering" "transit-network-to-vpc-network" {
  provider             = google-beta
  name                 = "transit-network-to-vpc-network"
  network              = google_compute_network.transit-network.self_link
  peer_network         = google_compute_network.vpc-network.self_link
  export_custom_routes = true
}

$ gcloud compute networks peerings list
NAME                            NETWORK          PEER_PROJECT  PEER_NETWORK     PEER_MTU  IMPORT_CUSTOM_ROUTES  EXPORT_CUSTOM_ROUTES  STATE   STATE_DETAILS
transit-network-to-vpc-network  transit-network  test-prj      vpc-network                False                 True                  ACTIVE  [2020-11-13T02:05:12.111-08:00]: Connected.
vpc-network-to-transit-network  vpc-network      test-prj      transit-network            True                  False                 ACTIVE  [2020-11-13T02:05:29.387-08:00]: Connected.

我使用了 Terraform 的当前版本:

$ terraform version
Terraform v0.13.5
+ provider registry.terraform.io/hashicorp/google v3.5.0
+ provider registry.terraform.io/hashicorp/google-beta v3.47.0

相关内容