0

I am trying to write a Lambda function to copy files from one s3 bucket to another integrated with AWS Xray. Below is the code for Lambda function. I am getting the error

aws_xray_sdk.core.exceptions.exceptions.SegmentNotFoundException: cannot find the current segment/subsegment, please make sure you have a segment open

I have included the Aws xray SDK in my deployment package. Also, begin segment and end segment are included in the code. Please give a solution to this error.

import boto3
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch
patch(['boto3'])

client = boto3.client('s3')
s3 = boto3.resource('s3')
SourceBucket = 'bucket1'
DestBucket = 'bucket2'
list1=[];

def lambda_handler(event, context):
    response = client.list_objects(Bucket=SourceBucket)
    if 'Contents' in response:
        for item in response['Contents']:
            list1.append(item['Key']);
        put_object_into_s3()
        for name in list1:
            copy_source = {
                'Bucket': SourceBucket,
                'Key': name
            }
            response = s3.meta.client.copy(copy_source, DestBucket, name)
Dawny33
  • 10,543
  • 21
  • 82
  • 134
Prakshi Yadav
  • 119
  • 1
  • 2
  • 7

3 Answers3

4

the context management for a Lambda environment would never throw a SegmentNotFoundException. If there is no active segment/subsegment in thread local storage, it constructs a segment based on environment variables set in Lambda container. See https://github.com/aws/aws-xray-sdk-python/blob/master/aws_xray_sdk/core/lambda_launcher.py#L79

The lambda context management will be used when an environment variable LAMBDA_TASK_ROOT is set. Are you using some tool to run your Lambda function locally or have you enabled your Lambda function's active tracing?

haotian465
  • 679
  • 3
  • 4
  • the same issue I am facing right now, I am using pycharm to test it locally and active tracing is enabled in my lambda function. then what is the solution to this issue. – Jessica Nov 30 '18 at 05:07
  • 1
    @Jessica If you just want to test non-XRay code you can set `AWS_XRAY_CONTEXT_MISSING` to `LOG_ERROR` so the SDK doesn't complain on context missing and simply give up capturing wrapped functions. This will run much less code path than mimic lambda behaviors. Ideally it would be better for the lambda local testing tool to be X-Ray friendly. Are you using https://github.com/awslabs/aws-sam-cli? There is already an open issue for this feature https://github.com/awslabs/aws-sam-cli/issues/217 – haotian465 Dec 04 '18 at 01:09
2

If you have any top-level code (almost always a bad idea) that calls xray_recorder.configure then it will clear out the segment that Lambda creates, but not create a new & valid segment. If you also have environment variable AWS_XRAY_CONTEXT_MISSING set to RUNTIME_ERROR - which it often is by default - then you will get these exceptions.

Our logs were littered with ERROR log messages (with AWS_XRAY_CONTEXT_MISSING=LOG_ERROR) until we found and removed the un-necessary calls to xray_recorder.configure.

HumbleEngineer
  • 161
  • 1
  • 4
0

Not sure but I have similar problem and just add this line of code

xray_recorder.begin_segment("Test Segment")
Kerisnarendra
  • 877
  • 9
  • 14