能否使用Terraform数据源获取私有网络中NAT网关的公网in列表?
获取子网ids列表的示例显示为here,但它是以aws_subnet_ids数据源为前提的,该数据源首先返回一个列表。
我们每个私有子网都有NAT网关。我没有办法获取私有网络中的NAT网关列表,然后从该列表中获取公网IP。
有没有人需要和/或解决这个问题?
发布于 2021-05-07 20:59:25
这个变通方法适用于我,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") ]
}发布于 2019-09-18 10:09:02
试试这个:
data "aws_vpc" "selected" {
cidr_block= "[your_cidr_block]"
}
data "aws_nat_gateway" "default" {
vpc_id = "${aws_vpc.selected.id}"
}
output "nat_ip" {
count = "{length(aws_nat_gateway.default.ids)}"
value = "The NAT Gateway number ${count.index} have the Public IP: + ${aws_nat_gateway.default.*.public_ip}"
}发布于 2021-12-02 14:59:43
试试这段代码。
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
}https://stackoverflow.com/questions/57983539
复制相似问题