Open farra opened 1 day ago
This function appears to give me the correct results.
def get_subnets_for_az2(vpc_id: pulumi.Input[str], target_az: str) -> pulumi.Output[list]:
"""
Get private subnets for a specific availability zone within a VPC.
Args:
vpc_id: The VPC ID to search in
target_az: Target availability zone
Returns:
pulumi.Output[list]: List of matching subnet IDs
"""
def get_matching_subnets(vid):
try:
# Query AWS for all subnets in the VPC and AZ
subnets = aws.ec2.get_subnets(
filters=[
{
"name": "vpc-id",
"values": [vid]
},
{
"name": "availability-zone",
"values": [target_az]
},
# Filter for private subnets by checking map_public_ip_on_launch
{
"name": "map-public-ip-on-launch",
"values": ["false"]
}
]
)
pulumi.log.info(f"Found {len(subnets.ids)} subnets in AZ {target_az}")
return subnets.ids
except Exception as e:
pulumi.log.error(f"Error finding subnets: {str(e)}")
return []
return pulumi.Output.from_input(vpc_id).apply(get_matching_subnets)
Hey @farra, I tried reproducing it with this minimal example but couldn't:
from typing import Sequence
import pulumi
import pulumi_awsx as awsx
import pulumi_aws as aws
def filter_subnets(subnets: Sequence['aws.ec2.Subnet'], desired_az: str):
def filter_subnet(subnet: 'aws.ec2.Subnet'):
return subnet.availability_zone.apply(lambda az: subnet if az == desired_az else None)
filtered_subnets = [filter_subnet(net) for net in subnets]
return pulumi.Output.all(*filtered_subnets).apply(lambda args: [net for net in args if net is not None])
vpc = awsx.ec2.Vpc("test-vpc", enable_dns_hostnames=True, cidr_block="10.0.0.0/16")
pulumi.export('selected_nets', vpc.subnets.apply(lambda subnets: filter_subnets(subnets, "us-west-2a")).apply(lambda subnets: [net.id for net in subnets]))
This exports the two subnets in the us-west-2a
AZ.
These are the versions I'm using:
CLI
Version 3.136.1
Go Version go1.23.2
Go Compiler gc
Plugins
KIND NAME VERSION
resource aws 6.59.0
resource awsx 2.17.0
resource docker 4.5.7
language python 3.136.1
What happened?
Context
The motivation here is to set capacity reservations in the launch template. Capacity reservations are specific to an availability zone. When I set the subnet as part of the managed node group, I need to know that I'm using the assigned availability zone.
Issues
Pseudocode
Putting Outputs aside, I intended to write a function something like this:
Current Function
The current function takes an aws.ec2.Vpc and a dictionary of configuration from the pulumi config. That part isn't particularly relevant. This code has been written with some help from Claude and ChatGPT. I added logging to understand what was going on.
Output
When running
pulumi up
, I get the following output:Yet, the AWS console reports that, for example,
subnet-049611835250f0802
is inus-west-2a
notus-west-2c
.Example
This project gives the same results for me:
Output
Output of
pulumi about
Additional context
No response
Contributing
Vote on this issue by adding a 👍 reaction. To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).