如何通过编程获取指定区域的SageMaker ml.*系列实例类型列表?
获取指定区域SageMaker支持的实例类型
因为SageMaker的专属实例类型(如ml.t3.medium、ml.m5.xlarge等)不属于EC2的标准实例分类体系,所以无法通过EC2的describe-instance-types命令查询。你可以通过以下编程方式获取指定区域内SageMaker支持的实例类型:
方法一:使用AWS CLI
直接调用SageMaker的CLI命令list-supported-instance-types,并指定目标区域:
aws sagemaker list-supported-instance-types --region us-east-1
该命令会返回该区域内SageMaker支持的所有实例类型列表。如果需要筛选特定类型(比如只看ml.t3.*),可以结合jq进行过滤:
aws sagemaker list-supported-instance-types --region us-east-1 | jq '.InstanceTypes[] | select(startswith("ml.t3."))'
如果需要获取更详细的实例属性(比如网络配置),可以先拿到实例类型列表,再逐个调用EC2的describe-instance-types(注意部分SageMaker实例可能对应EC2实例的变种,需要匹配对应EC2类型),例如:
# 先获取SageMaker实例类型列表 sagemaker_types=$(aws sagemaker list-supported-instance-types --region us-east-1 | jq -r '.InstanceTypes[]') # 遍历查询每个类型的EC2属性 for type in $sagemaker_types; do aws ec2 describe-instance-types --instance-types "$type" --query "InstanceTypes[].{Type: InstanceType, MaxENI: NetworkInfo.MaximumNetworkInterfaces, IPv4addr: NetworkInfo.Ipv4AddressesPerInterface}" --output table done
方法二:使用AWS SDK(以Python boto3为例)
通过SageMaker客户端调用list_supported_instance_types方法,指定区域:
import boto3 def get_sagemaker_instance_types(region): sagemaker_client = boto3.client('sagemaker', region_name=region) response = sagemaker_client.list_supported_instance_types() return response['InstanceTypes'] # 示例:获取us-east-1区域的实例类型 instance_types = get_sagemaker_instance_types('us-east-1') for instance_type in instance_types: print(instance_type)
如果需要获取实例的详细网络配置,可以结合EC2客户端查询:
import boto3 def get_sagemaker_instance_details(region): sagemaker_client = boto3.client('sagemaker', region_name=region) ec2_client = boto3.client('ec2', region_name=region) sagemaker_types = sagemaker_client.list_supported_instance_types()['InstanceTypes'] details = [] for instance_type in sagemaker_types: try: ec2_response = ec2_client.describe_instance_types(InstanceTypes=[instance_type]) instance_info = ec2_response['InstanceTypes'][0] details.append({ 'Type': instance_info['InstanceType'], 'MaxENI': instance_info['NetworkInfo']['MaximumNetworkInterfaces'], 'IPv4addr': instance_info['NetworkInfo']['Ipv4AddressesPerInterface'] }) except Exception as e: # 处理部分SageMaker实例无对应EC2公开信息的情况 details.append({ 'Type': instance_type, 'Error': str(e) }) return details # 调用示例 instance_details = get_sagemaker_instance_details('us-east-1') for detail in instance_details: print(detail)
注意事项
- 不同区域支持的SageMaker实例类型可能存在差异,务必指定目标区域。
- 部分SageMaker专用实例(如部分训练优化实例)可能没有对应的EC2公开实例信息,此时EC2的查询会返回错误,需要做异常处理。
内容的提问来源于stack exchange,提问作者RubenLaguna
相关产品推荐
相关产品推荐

