如何通过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:
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
- For notebook instances:
Next, fetch all instance types available in your target Availability Zone:
Replaceus-east-1awith 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 textFinally, 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
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-typesfor Studio notebooks).
内容的提问来源于stack exchange,提问作者Himanshu Raj

