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

AWS SageMaker标注任务Lambda事件为空问题求助

Troubleshooting Empty Event in SageMaker Annotation Consolidation Lambda

Let's break down why your Lambda is receiving an empty event dictionary and how to fix it—this is usually a configuration or trigger issue rather than a code problem, since you've already set up the necessary permissions.

1. You're Testing the Lambda Manually (Not Triggered by SageMaker)

The Annotation Consolidation Lambda is only invoked automatically by SageMaker when there are completed annotations to consolidate. If you're using the Lambda console's "Test" feature with a blank or custom test event, you'll get an empty event because SageMaker hasn't passed any data to it.

Fix:

  • Wait until your private workforce has submitted at least one annotation for the labeling job. SageMaker will trigger your Lambda once annotations are ready for consolidation.
  • If you want to test locally, use the example event structure you provided to create a test event in the Lambda console.

2. Lambda Resource Policy Doesn't Allow SageMaker to Invoke It

Even with AmazonSageMakerFullAccess on your Lambda role, you need to explicitly grant SageMaker permission to call your Lambda function. Without this, SageMaker can't send the event data.

Fix:

Add this resource policy to your Lambda function (under Configuration > Permissions > Resource policy > Add permission):

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "sagemaker.amazonaws.com"
      },
      "Action": "lambda:InvokeFunction",
      "Resource": "arn:aws:lambda:us-east-1:YOUR_ACCOUNT_ID:function:YOUR_CUSTOM_LAMBDA_NAME"
    }
  ]
}

Replace placeholders with your actual account ID and Lambda name.

3. Mismatched Regions or Incorrect Lambda ARN

Double-check that:

  • Your labeling job and Lambda function are in the same AWS region (e.g., both in us-east-1). Cross-region invocations won't work here.
  • The AnnotationConsolidationLambdaArn in your create_labeling_job code is 100% correct—no typos in the ARN string.

4. Outdated Python Runtime

Python 3.6 is no longer supported by AWS Lambda (it reached end-of-life in 2021). Using an unsupported runtime can cause compatibility issues where SageMaker can't properly pass event data to your function.

Fix:

Update your Lambda's runtime to Python 3.8 or later (under Configuration > Runtime settings > Edit).

5. Pre-Human Task Lambda Is Failing

If your PRE-BoundingBox Lambda is throwing errors, it can halt the entire labeling workflow before annotations reach the consolidation step. SageMaker won't trigger your post-annotation Lambda if the pre-task step fails.

Fix:

Check the CloudWatch logs for your pre-task Lambda to ensure it's returning the expected response format for SageMaker.

Robust Lambda Code with Error Handling

To avoid crashes from missing keys and get better debug info, modify your Lambda code to use safe access and error handling:

import json
import boto3
from urllib.parse import urlparse

def get_s3_client(role_arn, kms_key_id):
    sts_client = boto3.client('sts')
    assumed_role = sts_client.assume_role(
        RoleArn=role_arn,
        RoleSessionName='SageMakerConsolidationSession'
    )
    return boto3.client(
        's3',
        aws_access_key_id=assumed_role['Credentials']['AccessKeyId'],
        aws_secret_access_key=assumed_role['Credentials']['SecretAccessKey'],
        aws_session_token=assumed_role['Credentials']['SessionToken']
    )

def consolidate_annotations(labeling_job_arn, payload, label_attr, s3_client):
    # Add your consolidation logic here
    # Example: Fetch annotations from payload['s3Uri'], process, return results
    return {"consolidatedLabel": "example_result"}

def lambda_handler(event, context):
    print("Received event:", json.dumps(event, indent=2))
    
    # Handle empty event case
    if not event:
        print("Warning: Empty event received. This Lambda should be triggered by SageMaker.")
        return {
            "statusCode": 400,
            "body": json.dumps("Empty event - Lambda must be invoked by SageMaker Annotation Consolidation")
        }
    
    try:
        # Safely extract event fields
        labeling_job_arn = event.get("labelingJobArn")
        label_attr = event.get("labelAttributeName")
        payload = event.get("payload")
        role_arn = event.get("roleArn")
        
        # Validate critical fields
        if not all([labeling_job_arn, label_attr, payload, role_arn]) or "s3Uri" not in payload:
            raise ValueError("Missing required fields in event")
        
        parsed_s3_uri = urlparse(payload["s3Uri"])
        print("Parsed annotation S3 URI:", parsed_s3_uri)
        
        label_categories = event.get("labelCategories", "None provided")
        print("Label categories:", label_categories)
        
        # Initialize S3 client and run consolidation
        s3_client = get_s3_client(role_arn, event.get("kmsKeyId"))
        consolidation_result = consolidate_annotations(labeling_job_arn, payload, label_attr, s3_client)
        
        return {
            "statusCode": 200,
            "body": json.dumps(consolidation_result)
        }
    
    except Exception as e:
        print(f"Consolidation failed: {str(e)}")
        return {
            "statusCode": 500,
            "body": json.dumps(f"Error during annotation consolidation: {str(e)}")
        }

Final Debug Step

After applying these fixes, check your Lambda's CloudWatch logs (under Monitor > Logs > Log groups) once annotations are submitted. You'll see the full event payload from SageMaker if everything is configured correctly.

内容的提问来源于stack exchange,提问作者José R. Sánchez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:42:11