首页 > 解决方案 > AWS XRay: Unable to write to /tmp/.aws-xray/initialized. Failed to signal SDK initialization

问题描述

I am trying to write a Lambda function to store data in dyanamodb and try to integrate with AWS Xray. Below is the code for Lambda function. I am getting the error

Unable to write to /tmp/.aws-xray/initialized. Failed to signal SDK initialization. Subsegment put_item discarded due to Lambda worker still initializing

I install Aws xray SDK package. Also, begin segment and end segment are included in the code. and also set an environment variable of LAMBDA_TASK_ROOT. Please give a solution to this error.

import json
import os
import configparser
import boto3
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch

patch(['boto3'])
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(os.environ['dynamodb_table'])

def lambda_handler(event, context):
    for i in event:
        item = {
            'key': i['key'],
            'search': i['search'],
        }
        put_item_into_dynamodb(item)

    response = {
         "statusCode": 200,
         "body": json.dumps(item)
    }
    return response

def put_item_into_dynamodb(item):
    try:
        xray_recorder.begin_subsegment('put_item')
        response = table.put_item(Item=item)
        status_code = response['ResponseMetadata']['HTTPStatusCode']
        xray_recorder.current_subsegment().put_annotation('put_response', status_code)
    finally:
        xray_recorder.end_subsegment()

Update-2 (second problem) : in this code AttributeError: 'NoneType' object has no attribute 'put_annotation' this error is coming .. i dont have any idea why this is coming..

def lambda_handler(event, context):

    val = (str(event['userId']),str(event['teamId']), str(event['searchScope']))
    key_table = "|".join(val)
    key = {
        'key': key_table
    }
    response = get_item_into_dynamodb(key)

    try:
        data = response['Item']
        for i in data['search']:
            keyword_list.append(i['searchText'])
            dict_of_keyword[i['searchText']] = i['dateTime']

        recent_sort = sorted(dict_of_keyword.items(), key=lambda x: x[1], reverse=True)
def get_item_into_dynamodb(key):
    try:
        xray_recorder.begin_segment('get_item')
        response = table.get_item(Key = key)
        status_code = response['ResponseMetadata']['HTTPStatusCode']
        xray_recorder.current_subsegment().put_annotation('get_response', status_code) #error is on this line
    finally:
        xray_recorder.end_subsegment()

    return response

标签: pythonamazon-web-servicesaws-lambdaaws-xray

解决方案


You cannot create segments within Lambda function. Lambda will create the segments in the outside wrapper which you cannot access within the function. Could you try to change from xray_recorder.begin_segment('get_item') to xray_recorder.begin_subsegment('get_item')?

Also the trace data generated outside of lambda handler will not be captured because during that time the lambda function is still initializing and no tracing context is available.


推荐阅读