You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过API获取AWS特定可用区支持的SageMaker实例类型

Hey, here's how you can pull the list of SageMaker-supported instance types for a specific AWS Availability Zone programmatically using APIs. I'll walk you through both AWS CLI and Python (boto3) methods since those are the most practical approaches:

1. Using AWS CLI

If you prefer working from the command line, follow these steps:

  • First, get all instance types SageMaker supports (pick the command matching your use case):

    • For notebook instances:
      aws sagemaker list-notebook-instance-types --query 'NotebookInstanceTypes' --output text
      
    • For training instances:
      aws sagemaker list-training-instance-types --query 'TrainingInstanceTypes' --output text
      
    • For inference instances:
      aws sagemaker list-inference-instance-types --query 'InferenceInstanceTypes' --output text
      
  • Next, fetch all instance types available in your target Availability Zone:
    Replace us-east-1a with your actual AZ in this command:

    aws ec2 describe-instance-type-offerings \
      --location-type availability-zone \
      --filters Name=location,Values=us-east-1a \
      --query 'InstanceTypeOfferings[*].InstanceType' \
      --output text
    
  • Finally, find the overlap (instance types that are supported by both SageMaker and the target AZ) using a tool like grep:

    # Example for training instances (adjust file names if using other categories)
    aws sagemaker list-training-instance-types --query 'TrainingInstanceTypes' --output text | tr '\t' '\n' > sm-training-types.txt
    aws ec2 describe-instance-type-offerings --location-type availability-zone --filters Name=location,Values=us-east-1a --query 'InstanceTypeOfferings[*].InstanceType' --output text | tr '\t' '\n' > az-types.txt
    grep -Fxf sm-training-types.txt az-types.txt
    
2. Using Python SDK (boto3)

For automation or code-based workflows, here's a Python script that handles the heavy lifting:

import boto3

def get_sagemaker_az_supported_instances(region, az, instance_category="notebook"):
    # Initialize AWS clients
    sagemaker_client = boto3.client('sagemaker', region_name=region)
    ec2_client = boto3.client('ec2', region_name=region)
    
    # Fetch SageMaker-supported instance types based on category
    if instance_category == "notebook":
        sm_response = sagemaker_client.list_notebook_instance_types()
        sm_instance_types = set(sm_response['NotebookInstanceTypes'])
    elif instance_category == "training":
        sm_response = sagemaker_client.list_training_instance_types()
        sm_instance_types = set(sm_response['TrainingInstanceTypes'])
    elif instance_category == "inference":
        sm_response = sagemaker_client.list_inference_instance_types()
        sm_instance_types = set(sm_response['InferenceInstanceTypes'])
    else:
        raise ValueError("Instance category must be 'notebook', 'training', or 'inference'")
    
    # Fetch instance types available in the target AZ
    ec2_response = ec2_client.describe_instance_type_offerings(
        LocationType='availability-zone',
        Filters=[{'Name': 'location', 'Values': [az]}]
    )
    az_instance_types = set([item['InstanceType'] for item in ec2_response['InstanceTypeOfferings']])
    
    # Return the intersection (types supported by both)
    return sorted(list(sm_instance_types & az_instance_types))

# Example usage
if __name__ == "__main__":
    target_region = "us-east-1"
    target_az = "us-east-1a"
    # Change to "training" or "inference" as needed
    supported_instances = get_sagemaker_az_supported_instances(target_region, target_az, "training")
    
    print(f"SageMaker training instances supported in {target_az}:")
    for instance in supported_instances:
        print(f"- {instance}")

Quick Notes:

  • Ensure your AWS credentials are set up correctly (via aws configure, environment variables, or IAM roles if running on AWS infrastructure like EC2).
  • These APIs return real-time data, so you'll always get the latest supported instance types (unlike static documentation).
  • If you need instance types for a specific SageMaker feature (like SageMaker Studio), use the corresponding API (e.g., list-studio-notebook-instance-types for Studio notebooks).

内容的提问来源于stack exchange,提问作者Himanshu Raj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 21:37:43