GCP Terraform cloud_router 模块——如何在 nat 配置中引用单个子网的 self_link?

发布于 2025-01-17 05:28:53 字数 1857 浏览 4 评论 0原文

我已经为此苦苦挣扎了几个小时,但无法使其发挥作用。 我在 main.tf 中使用外部模块来部署具有公共和私有子网的 GCP VPC,并且我想为其中一个子网(私有子网)配置 NAT。

我的 VPC 配置如下:

module "vpc" {
    source = "github.com/terraform-google-modules/terraform-google-network"
    project_id   = var.project_id
    network_name = var.network_name
    routing_mode = "REGIONAL"

    subnets = [
        {
            subnet_name           = var.pub_subnet
            subnet_ip             = var.pub_cidr
            subnet_region         = var.region
        },
        {
            subnet_name           = var.priv_subnet
            subnet_ip             = var.priv_cidr
            subnet_region         = var.region
            subnet_private_access = true
            subnet_flow_logs      = true
        },
    ]
    routes = [
        {
            name                   = "egress-internet"
            description            = "route through IGW to access internet"
            destination_range      = "0.0.0.0/0"
            tags                   = "egress-inet"
            next_hop_internet      = "true"
        },
    ]
}

在 VPC 模块块之后,我有一个“cloud_router”块,旨在为私有子网配置 NAT,但我无法获得正确的“名称”值。从我读到的文档来看,这是在寻找子网的 self_link 。我怎样才能让它发挥作用?

    module "cloud_router" {
  source  = "terraform-google-modules/cloud-router/google"
  version = "~> 0.4"

  name    = var.cloud_router
  project = var.project_id
  region  = var.region
  network = module.vpc.network_self_link
  nats = [{
      name = var.cloud_nat
      source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS"
      subnetwork = {
          name = "${module.vpc.subnets_self_links[0]}"
          // <self_link>
          // {{API base url}}/projects/{{your project}}/{{location type}}/{{location}}/{{resource type}}/{{name}}
          source_ip_ranges_to_nat = ["ALL_IP_RANGES"]
      }
  }]
}

I have been struggling with this for hours and can't make it work.
I am using external modules in my main.tf to deploy a GCP VPC with a public and private subnet, and I want to configure NAT for one of the subnets (the private one).

My VPC is configured like this:

module "vpc" {
    source = "github.com/terraform-google-modules/terraform-google-network"
    project_id   = var.project_id
    network_name = var.network_name
    routing_mode = "REGIONAL"

    subnets = [
        {
            subnet_name           = var.pub_subnet
            subnet_ip             = var.pub_cidr
            subnet_region         = var.region
        },
        {
            subnet_name           = var.priv_subnet
            subnet_ip             = var.priv_cidr
            subnet_region         = var.region
            subnet_private_access = true
            subnet_flow_logs      = true
        },
    ]
    routes = [
        {
            name                   = "egress-internet"
            description            = "route through IGW to access internet"
            destination_range      = "0.0.0.0/0"
            tags                   = "egress-inet"
            next_hop_internet      = "true"
        },
    ]
}

Ater the VPC module block, I have a "cloud_router" block that is meant to configure NAT for the private subnet, but I cannot get the "name" value correct. From the docs I read, this is looking for the self_link of the subnetwork. How can I get this working?

    module "cloud_router" {
  source  = "terraform-google-modules/cloud-router/google"
  version = "~> 0.4"

  name    = var.cloud_router
  project = var.project_id
  region  = var.region
  network = module.vpc.network_self_link
  nats = [{
      name = var.cloud_nat
      source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS"
      subnetwork = {
          name = "${module.vpc.subnets_self_links[0]}"
          // <self_link>
          // {{API base url}}/projects/{{your project}}/{{location type}}/{{location}}/{{resource type}}/{{name}}
          source_ip_ranges_to_nat = ["ALL_IP_RANGES"]
      }
  }]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2025-01-24 05:28:53

通过将 nat 配置移动到“资源”定义中而不是将其嵌套在“cloud_router”模块中,可以解决此问题:

resource "google_compute_router_nat" "nat_manual" {
  name   = var.cloud_nat
  router = module.cloud_router.router.name
  region = module.cloud_router.router.region

  nat_ip_allocate_option = "AUTO_ONLY"
  //nat_ips                = google_compute_address.address.*.self_link

  source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS"
  subnetwork {
    name                    = "${module.vpc.subnets_self_links[0]}"
    source_ip_ranges_to_nat = ["ALL_IP_RANGES"]
  }
}

This was resolved by moving the nat configuration into a 'resource' definition instead of nesting it inside the 'cloud_router' module:

resource "google_compute_router_nat" "nat_manual" {
  name   = var.cloud_nat
  router = module.cloud_router.router.name
  region = module.cloud_router.router.region

  nat_ip_allocate_option = "AUTO_ONLY"
  //nat_ips                = google_compute_address.address.*.self_link

  source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS"
  subnetwork {
    name                    = "${module.vpc.subnets_self_links[0]}"
    source_ip_ranges_to_nat = ["ALL_IP_RANGES"]
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文