- Newest
- Most votes
- Most comments
To programmatically get the full CloudWatch log stream name for a SageMaker training job using Python boto3, you'll need to use a combination of the SageMaker and CloudWatch Logs APIs. Here's a step-by-step approach:
-
First, use the SageMaker API to get the training job name.
-
Then, use the CloudWatch Logs API to list the log streams in the "/aws/sagemaker/TrainingJobs" log group.
-
Filter the log streams to find the one that starts with your training job name.
Here's a Python code snippet that demonstrates this process:
import boto3 sagemaker_client = boto3.client('sagemaker') logs_client = boto3.client('logs') def get_training_job_log_stream(training_job_name): # List log streams in the SageMaker training jobs log group response = logs_client.describe_log_streams( logGroupName='/aws/sagemaker/TrainingJobs', logStreamNamePrefix=training_job_name ) # Find the log stream that matches the training job name for log_stream in response['logStreams']: if log_stream['logStreamName'].startswith(training_job_name): return log_stream['logStreamName'] return None # Usage training_job_name = 'your-training-job-name' log_stream_name = get_training_job_log_stream(training_job_name) if log_stream_name: print(f"Log stream name: {log_stream_name}") else: print("Log stream not found")
This approach works because SageMaker training job log streams follow the naming convention you mentioned: "{sagemaker_training_job_name}/algo-1-...". By using the training job name as a prefix, we can find the correct log stream.
Note that this method may need to handle pagination if there are many log streams in the log group. Also, ensure that your IAM permissions allow access to both SageMaker and CloudWatch Logs APIs.
Sources
Log groups and streams that Amazon SageMaker AI sends to Amazon CloudWatch Logs - Amazon SageMaker AI
Inference Pipeline Logs and Metrics - Amazon SageMaker AI
Relevant content
- asked 2 years ago
