gpt4 book ai didi

terraform - 获取使用 for_each 创建的子网 ID 列表

转载 作者:行者123 更新时间:2023-12-05 01:52:19 25 4
gpt4 key购买 nike

我如何获取使用 for_each 创建的子网 ID 列表(需要在我的脚本底部):

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}

provider "aws" {
region = "eu-west-1"
}

data "aws_availability_zones" "azs" {
state = "available"
}

locals {
az_names = data.aws_availability_zones.azs.names
}

variable "vpc_cidr" {
default = "10.0.0.0/16"
}

resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
}

resource "aws_subnet" "private" {
for_each = {for idx, az_name in local.az_names: idx => az_name}
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(var.vpc_cidr, 8, each.key)
availability_zone = local.az_names[each.key]
}

module "eks" {
source = "terraform-aws-modules/eks/aws"

# I need to get the list of subnet ids (aws_subnet.private) here
subnet_ids = []
}

最佳答案

虽然使用 for 是正确的,但更短的表达式是使用 values功能与 splat运算符(operator):

subnet_ids = values(aws_subnet.private)[*].id

关于terraform - 获取使用 for_each 创建的子网 ID 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71648634/

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