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

AWS X-Ray集成Lambda复制S3文件时遇SegmentNotFoundException求助

解决Lambda集成X-Ray时的SegmentNotFoundException错误

你遇到的这个错误,核心问题出在Lambda handler函数外部初始化了被X-Ray patch过的boto3客户端。Lambda的执行逻辑里,handler外面的代码会在冷启动阶段运行,这时候X-Ray的根Segment还没被创建(Segment是在handler执行时由X-Ray的Lambda集成自动初始化的),所以当你在handler外创建被patch的boto3实例时,X-Ray SDK找不到可用的Segment上下文,自然就抛出了这个异常。

下面是具体的修复方案和修改后的代码:

关键修复点

  • 将boto3客户端/资源的初始化代码移到lambda_handler函数内部,确保它们在X-Ray Segment存在的上下文里被创建
  • 移除不必要的手动Segment操作(Lambda集成X-Ray时会自动创建根Segment,不需要手动调用begin/end)
  • 保持X-Ray的patch操作在handler外是没问题的,因为patch只是修改boto3的行为,不会立即触发X-Ray的Segment检查

修改后的完整代码

import boto3
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch

# 这里的patch操作可以保留在handler外,只是修改boto3的行为,不会触发Segment检查
patch(['boto3'])

SourceBucket = 'bucket1'
DestBucket = 'bucket2'

def lambda_handler(event, context):
    # 把boto3客户端和资源的初始化移到handler内部,此时X-Ray根Segment已存在
    client = boto3.client('s3')
    s3 = boto3.resource('s3')
    list1 = []
    
    response = client.list_objects(Bucket=SourceBucket)
    if 'Contents' in response:
        for item in response['Contents']:
            list1.append(item['Key'])
    
    # 注意:你原代码里调用了put_object_into_s3()但没定义这个函数,记得补充或者移除
    # put_object_into_s3()
    
    for name in list1:
        copy_source = {
            'Bucket': SourceBucket,
            'Key': name
        }
        response = s3.meta.client.copy(copy_source, DestBucket, name)

额外注意事项

  • 原代码里有个未定义的函数put_object_into_s3(),记得要么实现这个函数,要么把调用移除,否则会引发新的错误
  • 确保你的Lambda执行角色已经有X-Ray的相关权限(比如xray:PutTraceSegments和xray:PutTelemetryRecords),否则X-Ray无法上报追踪数据
  • 如果需要手动创建子Segment来细化追踪,可以在handler内部使用xray_recorder.begin_subsegment()和xray_recorder.end_subsegment(),比如:
    def lambda_handler(event, context):
        # ... 其他代码
        with xray_recorder.in_subsegment('copy_s3_objects') as subsegment:
            try:
                # 执行S3复制操作
                response = s3.meta.client.copy(copy_source, DestBucket, name)
                subsegment.put_annotation('copied_key', name)
            except Exception as e:
                subsegment.add_exception(e)
    

内容的提问来源于stack exchange,提问作者Prakshi Yadav

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:00:23