【发布时间】:2019-05-03 08:16:58
【问题描述】:
我正在尝试编写一个 Lambda 函数来将数据存储在 dyanamodb 中并尝试与 AWS Xray 集成。下面是 Lambda 函数的代码。我收到了错误
无法写入 /tmp/.aws-xray/initialized。未能向 SDK 发出信号 初始化。由于 Lambda 工作程序而丢弃了子段 put_item 仍在初始化
我安装了 Aws xray SDK 包。此外,代码中还包含开始段和结束段。并设置LAMBDA_TASK_ROOT的环境变量。 请给出此错误的解决方案。
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(第二个问题):在这段代码中 AttributeError: 'NoneType' object has no attribute 'put_annotation' 这个错误来了..我不知道为什么会这样..
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
【问题讨论】:
-
hii joey 欢迎来到 stackoverflow,您可以在这里发布完整代码
-
请检查我更新了
-
cloudwatch 是否说明该错误发生在哪一行?
-
现在我在本地运行,这个错误来了。
-
当我刚刚打补丁时,无法写入 /tmp/.aws-xray/initialized。未能发出 SDK 初始化信号 显示此错误,
标签: python amazon-web-services aws-lambda aws-xray