https://pulumi.com logo
#python
Title
# python
b

breezy-painter-29573

03/28/2022, 3:01 AM
Hi everyone. With the code snippet below I am trying to achieve creation of aws subnets with specific CIDR addresses blocks in each available Availability Zone.
Copy code
for i, az in enumerate(azs):
            subnet = aws.ec2.Subnet(
                resource_name=f"public-{i}",
                vpc_id=vpc.id,
                availability_zone=az,
                cidr_block=vpc.cidr_block.apply(lambda cidr_block: ip_network(cidr_block).subnets(new_prefix=27)[i]),
                tags=tags,
            )
            subnets.append(subnet)
but it fails with the
TypeError: 'generator' object is not subscriptable
. Which, probably makes sense. I just can’t find a way to do it in a for loop. Thank you in advance! P.S.
ip_network
is a function for
netaddr
.
nvm.. ip_network().subnet() returns iterator which has to be converted to list before taking any index from it
m

modern-evening-83482

07/08/2022, 5:35 AM
Copy code
private_subnets = []
for subnet in range(1, number_of_azs + 1):
    ec2_subnet = aws.ec2.Subnet(
        f"{env}-ec2Subnet-{subnet}-{suffix}",
        availability_zone=available_azs.names[subnet - 1],
        cidr_block=f"10.27.{subnet}.0/24",
        vpc_id=vpc.id,
        map_public_ip_on_launch=False,
        tags={
            "Type": "Private",
            "Name": f"{env}-ec2Subnet-{subnet}-private-{suffix}",
        },
        opts=pulumi.ResourceOptions(provider=provider),
    )
    private_subnets.append(ec2_subnet)
will this work? I need to get the subnet ids at the end..However I am still getting each element as type Output...
2 Views