CDKTF 迭代 AWS 资源

huangapple go评论88阅读模式
英文:

CDKTF Iterator On AWS Resources

问题

我已经查看了CDKTF TerraformIterator的源代码以及AWS子网,尝试找到一种在设置for_each时迭代子网的方法。由于迭代器需要一个字符串列表,我不确定如何做到这一点,因为我没有看到从该类返回子网ID的任何内容。以下是一些Python伪代码:

public_subnets = Subnet(self, 'default-public',
        for_each = public_subnet_iterator,
        availability_zone_id = public_subnet_iterator.get_string('availability_zone'),
        cidr_block = public_subnet_iterator.get_string('CIDR'),
        vpc_id = default_vpc.id,
        tags = Tags.commons_with_name('default-public')
)

route_table = RouteTable(self, 'public-subnets',
        vpc_id = default_vpc.id,
        RouteTableRoute(
            cidr_block = '0.0.0.0/0',
            gateway_id = default_gateway.id
        ),
        tags = Tags.commons_with_name('public-subnet-routing')
)

routable_subnets = TerraformIterator.fromList(public_subnets.?????)
RouteTableAssociation(self, 'public-subnet-routes',
    for_each = routable_subnets,
    etc...
)

如何迭代子网以生成这些路由表关联?

英文:

I've gone through the source code of CDKTF TerraformIterator, and the AWS Subnets to try and find a way to iterate over Subnet when setting for_each. I'm not sure how to do this as the iterator requires a list of strings and I don't see anything that would return subnet ids from that class. Here is some Python pseudo code:

public_subnets = Subnet(self, 'default-public',
        for_each = public_subnet_iterator,
        availability_zone_id = public_subnet_iterator.get_string('availability_zone'),
        cidr_block = public_subnet_iterator.get_string('CIDR'),
        vpc_id = default_vpc.id
        tags = Tags.commons_with_name('default-public')
)

route_table = RouteTable(self, 'public-subnets',
        vpc_id = default_vpc.id,
        RouteTableRoute(
            cidr_block = '0.0.0.0/0',
            gateway_id = default_gateway.id
        ),
        tags = Tags.commons_with_name('public-subnet-routing')
)

routable_subnets = TerraformIterator.fromList(public_subnets.?????)
RouteTableAssociation(self, 'public-subnet-routes',
    for_each = routable_subnets,
    etc...
)

How can I iterate over the subnets to generate these route table associations?

答案1

得分: 1

这在CDKTF目前不受支持,但在即将推出的版本0.16中可能会发生改变。请参阅 https://github.com/hashicorp/terraform-cdk/issues/2024https://github.com/hashicorp/terraform-cdk/pull/2739 以获取更多上下文信息。在它受支持之前,您需要使用 逃生舱 来使其工作,或者如果 publicSubnetIterator 使用静态列表,您还可以使用 for 循环。

英文:

This is currently not supported in CDKTF, but will likely change in the upcoming version 0.16. See https://github.com/hashicorp/terraform-cdk/issues/2024 and https://github.com/hashicorp/terraform-cdk/pull/2739 for more context. Until it's supported you would need to use an escape hatch to make it work or if the publicSubnetIterator is using a static list you could also use a for loop.

huangapple
  • 本文由 发表于 2023年3月12日 06:30:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75709963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定