Skip to content

How do I get the CloudWatch log stream name of a SageMaker training job?

0

In CloudWatch, SageMaker training jobs are found in the log group: /aws/sagemaker/TrainingJobs. The log stream name has the format: "{sagemaker_training_job_name}/algo-1-..."

How can I programmatically get the full log stream name, from the training job name, using Python boto3 or the SageMaker Python SDK? It does not seem to be available from describe_training_job()

1 Answer
0
Accepted Answer

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:

  1. First, use the SageMaker API to get the training job name.

  2. Then, use the CloudWatch Logs API to list the log streams in the "/aws/sagemaker/TrainingJobs" log group.

  3. 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

answered a year ago
EXPERT
reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.