问题
I have successfully tested dynamodb.transactWriteItems using VS Code (node js) but when I moved my code to Lambda, it always throws the Type Error: dynamodb.transactWriteItems is not a function. Note that I am NOT using documentClient so declaring dynamodb = new AWS.DynamoDB()
is not the solution.
How can I check the AWS-SDK used by Lambda (my npm aws-sdk is v2.372.0) and how do I make use of the proper AWS-SDK version on Lambda if this is the root cause of the issue?
data = await dynamodb.transactWriteItems({
ReturnConsumedCapacity: "INDEXES",
ReturnItemCollectionMetrics: "SIZE",
TransactItems: [
{
Put: {
TableName: envVarPOTableName,
Item: {
"poNumber": {S: poNumber},
"supplierName": {S: event.supplierName},
"poStatus" : {S: "Created"},
"rmItemsArr": {L: [
{ M:{
"type": {S:event.rmItemObj.type},
"description": {S:event.rmItemObj.description}
},
}
]}
}
}
},
{
Update: {
TableName: envVarRMTableName,
Key:{
"type": {S: event.rmItemObj.type},
"description": {S: event.rmItemObj.description}
},
UpdateExpression: "set #pnA = list_append(#pnA, :vals)",
ExpressionAttributeNames: {
"#pnA" : "poNumbersArr"
},
ExpressionAttributeValues:{
":vals" : {L:[{S:poNumber}]}
},
ReturnValuesOnConditionCheckFailure: "ALL_OLD"
}
}
]
}).promise();
回答1:
The issue is that AWS lambda currently supports AWS SDK for JavaScript – 2.290.0 Ref. DynamoDB transactions are implemented from version 2.365.0 Ref. To solve this you can try including the latest version of JavaScript SDK in your Lambda deployment package Ref.
回答2:
I managed to initially double confuse myself on this, so thought I'd share in case anyone else did the same thing...
My problem was I was using:
const dynamoDB = new AWS.DynamoDB.DocumentClient()
and trying to call .transactWriteItems
which isn't valid. If you're using the DocumentClient, you need to use .transactWrite
instead.
To use .transactWriteItems
your dynamodb has to be set like const dynamoDB = new AWS.DynamoDB()
As @schof said above, the latest lambda aws sdks will support this f() https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html (up to 2.488.0 as of this writing for both 10.x and 8.10)
回答3:
Good news - the new Lambda execution environments apparently have the latest SDK. My understanding from reading that blog post is that Node 10X lambdas are automatically using the new environments already. I tested today with a lambda function that used the new Node 10X runtime and I no longer needed to bundle my own copy of the AWS SDK.
Also, apparently as of tomorrow, new Lambda functions (regardless of Node runtime) will have the new Lambda environment so presumably those will work as well.
来源:https://stackoverflow.com/questions/53902670/dynamodb-transactwriteitems-is-not-a-function-error-on-lambda-but-not-when-using