|
15 | 15 | from types_aiobotocore_ec2.literals import InstanceStateNameType, InstanceTypeType |
16 | 16 | from types_aiobotocore_ec2.type_defs import ( |
17 | 17 | FilterTypeDef, |
18 | | - SubnetTypeDef, |
19 | 18 | TagTypeDef, |
20 | 19 | ) |
21 | 20 |
|
|
37 | 36 | check_max_number_of_instances_not_exceeded, |
38 | 37 | compose_user_data, |
39 | 38 | ec2_instance_data_from_aws_instance, |
| 39 | + get_subnet_capacity, |
40 | 40 | ) |
41 | 41 |
|
42 | 42 | _logger = logging.getLogger(__name__) |
@@ -168,24 +168,9 @@ async def launch_instances( |
168 | 168 | # and avoid trying to launch instances in subnets that are already full |
169 | 169 | # and also allows to circumvent a moto bug that does not raise |
170 | 170 | # InsufficientInstanceCapacity when a subnet is full |
171 | | - subnets = await self.client.describe_subnets( |
172 | | - SubnetIds=instance_config.subnet_ids |
| 171 | + subnet_id_to_available_ips = await get_subnet_capacity( |
| 172 | + self, subnet_ids=instance_config.subnet_ids |
173 | 173 | ) |
174 | | - assert "Subnets" in subnets # nosec |
175 | | - subnet_id_to_subnet_map: dict[str, SubnetTypeDef] = { |
176 | | - subnet[ |
177 | | - "SubnetId" |
178 | | - ]: subnet # pyright: ignore[reportTypedDictNotRequiredAccess] |
179 | | - for subnet in subnets["Subnets"] |
180 | | - } |
181 | | - # preserve the order of instance_config.subnet_ids |
182 | | - |
183 | | - subnet_id_to_available_ips: dict[str, int] = { |
184 | | - subnet_id: subnet_id_to_subnet_map[subnet_id][ |
185 | | - "AvailableIpAddressCount" |
186 | | - ] # pyright: ignore[reportTypedDictNotRequiredAccess] |
187 | | - for subnet_id in instance_config.subnet_ids |
188 | | - } |
189 | 174 |
|
190 | 175 | total_available_ips = sum(subnet_id_to_available_ips.values()) |
191 | 176 | if total_available_ips < min_number_of_instances: |
|
0 commit comments