我有一個運行 python 腳本的 lambda 函式,該腳本基本上從一個 dynamodb 表中讀取資料并寫入另一個 dynamodb 表。第一次執行腳本時,它向我拋出了這個錯誤 -
Lambda function (abc-Service-295-DummyFunction) returned error: ({"errorMessage": "Unable to marshal response: Object of type ClientError is not JSON serializable", "errorType": "Runtime.MarshalError", "stackTrace": []})
我發現了一些與此相關的 stackoverflow 答案,并且大多數情況下都在說錯誤可能來自 Datetime 欄位。源表確實有一些日期時間形式的屬性,但我不確定我的腳本如何避免這個錯誤
這是我的代碼 -
import pprint
import boto3
from boto3.dynamodb.conditions import Key, Attr
def dynamo_bulk_reader(event):
dynamodb = boto3.resource('dynamodb', region_name=event['TargetRegion'])
table = dynamodb.Table(event['EnvironmentId'] '-abc-dynamo')
print("Exporting items from: " str(table))
data = []
response = table.query(
IndexName = 'idx-RecType-SubsId',
KeyConditionExpression = Key('RecType').eq('CreditNote') & Key('SubsId').begins_with(str(event['BuisnessUnitId']) '_'),
FilterExpression = Attr('Split').eq(0)
)
for i in range(len(response['Items'])):
if(abs(response['Items'][i]['CNAttr']['RemBal']) > 0):
data.append(response['Items'][i])
print("Finished exporting: " str(len(data)) " items.")
print("Data Exported: ")
pprint.pprint(data)
return data
def dynamo_bulk_writer(event):
dynamodb = boto3.resource('dynamodb', region_name=event['TargetRegion'])
table = dynamodb.Table(event['EnvironmentId'] '-xyz-dynamo2')
print("Importing items into: " str(table))
for table_item in dynamo_bulk_reader(event):
with table.batch_writer() as batch:
response = batch.put_item(
Item = {
'SubsId' : table_item['SubsId'],
'ItemId' : table_item['ItemId'],
'RecType' : table_item['RecType'],
'BuId' : int(table_item['SubsId'].split("_")[0])
}
)
print("Finished importing items...")
def lambda_handler(event,context):
try:
print("Event Received", event)
dynamo_bulk_writer(event)
except Exception as e:
return e
有人可以幫我嗎?我怎樣才能擺脫這個錯誤?
uj5u.com熱心網友回復:
您對 DynamoDB 的一個 API 呼叫回傳了一個錯誤,但您沒有捕獲它,而是嘗試決議它導致例外。嘗試在您發出的任何 API 請求中實作錯誤處理:
try:
response = table.query(
IndexName = 'idx-RecType-SubsId',
KeyConditionExpression = Key('RecType').eq('CreditNote') & Key('SubsId').begins_with(str(event['BuisnessUnitId']) '_'),
FilterExpression = Attr('Split').eq(0)
)
except Exception as e:
print(e)
將相同的 try/except 塊添加到您的所有請求中,這樣您就可以看到真正的失敗原因。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/516355.html
標籤:Python亚马逊网络服务aws-lambda亚马逊-dynamodb博托3
下一篇:Github操作-部署到AmazonECS:'task-definition.json:Unexpectedtoken?inJSONatposition0'錯誤
