code: def _configure_subnet(config):
      ec2 = _resource("ec2", config)
  
      # If head or worker security group is specified, filter down to subnets
      # belonging to the same VPC as the security group.
      sg_ids = []
      for node_type in config["available_node_types"].values():
          node_config = node_type["node_config"]
          sg_ids.extend(node_config.get("SecurityGroupIds", []))
      if sg_ids:
          vpc_id_of_sg = _get_vpc_id_of_sg(sg_ids, config)
      else:
          vpc_id_of_sg = None
  
      # map from node type key -> source of SubnetIds field
      subnet_src_info = {}
      _set_config_info(subnet_src=subnet_src_info)
      all_subnets = list(ec2.subnets.all())
      # separate node types with and without user-specified subnets
      node_types_subnets = []
      node_types_no_subnets = []
      for key, node_type in config["available_node_types"].items():
          if "SubnetIds" in node_type["node_config"]:
              node_types_subnets.append((key, node_type))
          else:
              node_types_no_subnets.append((key, node_type))
  
      vpc_id = None
  
      # iterate over node types with user-specified subnets first...
      for key, node_type in node_types_subnets:
          node_config = node_type["node_config"]
          user_subnets = _get_subnets_or_die(
              ec2, tuple(node_config["SubnetIds"])
          )
          subnet_ids, vpc_id = _usable_subnet_ids(
              user_subnets,
              all_subnets,
              azs=config["provider"].get("availability_zone"),
              vpc_id_of_sg=vpc_id_of_sg,
              use_internal_ips=config["provider"].get(
                  "use_internal_ips", False
              ),
              node_type_key=key,
          )
          subnet_src_info[key] = "config"
  
      # lock-in a good VPC shared by the last set of user-specified subnets...
      if vpc_id and not vpc_id_of_sg:
          vpc_id_of_sg = vpc_id
  
      # iterate over node types without user-specified subnets last...
      for key, node_type in node_types_no_subnets:
          node_config = node_type["node_config"]
          subnet_ids, vpc_id = _usable_subnet_ids(
              None,
              all_subnets,
              azs=config["provider"].get("availability_zone"),
              vpc_id_of_sg=vpc_id_of_sg,
              use_internal_ips=config["provider"].get(
                  "use_internal_ips", False
              ),
              node_type_key=key,
          )
          subnet_src_info[key] = "default"
          node_config["SubnetIds"] = subnet_ids
  
      return config
  
imports: import copy
  import itertools
  import json
  import logging
  import os
  import time
  from collections import Counter
  from functools import lru_cache, partial
  from typing import Any, Dict, List, Optional, Set, Tuple
  import boto3
  import botocore
  from packaging.version import Version
  from ray.autoscaler._private.aws.cloudwatch.cloudwatch_helper import CloudwatchHelper
  from ray.autoscaler._private.aws.utils import LazyDefaultDict, handle_boto_error, resource_cache
  from ray.autoscaler._private.cli_logger import cf, cli_logger
  from ray.autoscaler._private.event_system import CreateClusterEvent, global_event_system
  from ray.autoscaler._private.providers import _PROVIDER_PRETTY_NAMES
  from ray.autoscaler._private.util import check_legacy_fields
  from ray.autoscaler.tags import NODE_TYPE_LEGACY_HEAD, NODE_TYPE_LEGACY_WORKER
label_desc: Use image node_id 12 for calling a global function with example usage: _configure_subnet(config) and returns: config