【问题标题】:List of public IPs of NAT Gateways in VPCVPC内NAT网关公网IP列表
【发布时间】:2021-12-02 22:59:43
【问题描述】:

是否可以使用 Terraform 数据源获取 VPC 中 NAT 网关的公共 IP 列表?

here 显示了一个获取子网 ID 列表的示例,但它基于 aws_subnet_ids 数据源,它返回一个列表开始。

每个私有子网都有 NAT 网关。我没有找到一种方法来获取 vpc 中的 NAT 网关列表,然后从该列表中获取公共 IP。

有人需要和/或解决了这个问题吗?

【问题讨论】:

  • nat_gateway_id = "${aws_nat_gateway.NATGW.id}"?

标签: terraform terraform-provider-aws


【解决方案1】:

这个解决方法对我有用 https://github.com/hashicorp/terraform-provider-aws/issues/7575

我的代码示例

data "aws_nat_gateway" "nat_gw" {
 for_each = toset(module.vpc.public_subnets)
 subnet_id = each.value
}

获取 NAT 的公共 IP 以添加为安全组的源

resource "aws_security_group_rule" "allow_https"{
 type                     = "ingress"
 security_group_id        = module.sg.id
 from_port                = "443"
 to_port                  = "443"
 protocol                 = "tcp"
 cidr_blocks              = [ for v in data.aws_nat_gateway.nat_gw : format("${v.public_ip}%s", "/32") ]
}

【讨论】:

    【解决方案2】:

    试试这个代码。

    terraform {
      required_providers {
        shell = {
          source = "scottwinkler/shell"
          version = "1.7.10"
        }
      }
    }
    
    provider "shell" {
      # Configuration options
    }
    
    data "shell_script" "nat_gateways" {
      lifecycle_commands {
        read = <<-EOF
          aws ec2 describe-nat-gateways --region ${var.region}
        EOF
      }
    }
    
    locals {
      nat_gw_ips = flatten([
        for elem in jsondecode(data.shell_script.nat_gateways.output.NatGateways):
          format("${elem.NatGatewayAddresses[0].PublicIp}%s", "/32")
      ])
    }
    output "natgwips" {
      value = local.nat_gw_ips
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-06
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 2021-12-01
      相关资源
      最近更新 更多