12

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();
5
  • Hey Chester, Could you post a full code sample for the function in question please. Also Lambda doesn't use anything out of the box, you need to add it to your package.json, install it and deploy node_modules with your function. I reckon you're not creating the deployment with node_modules Commented Dec 23, 2018 at 10:01
  • Hey @MrkFldig, I code it in Lambda and didn't use a deployment package. I am trying now to create the function via deployment package but I'm really curious why it's not working if I code it in Lambda directly. I patterned the code from AWS' example on how to use transactWriteItems. Commented Dec 23, 2018 at 10:16
  • Ahh gotcha so yeah the answer below may apply, I can't actually check it, give me a couple of hours and I'll post an example for ya. Commented Dec 23, 2018 at 11:41
  • Thanks, Mrk. I think I'll be needing help creating a deployment package. Haven't tried that before. :-( Commented Dec 23, 2018 at 12:53
  • So really short answer is here stackoverflow.com/questions/34437900/… - I am however going to create a longer for you that'll be a more sustainable solution you can use locally, give me about 24 hours. Commented Dec 23, 2018 at 19:24

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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)

2 Comments

Exactly my case too. So versions are fine now but you need to instantiate a different class.
Worth to mention you then need to use a different, typed syntax. So if you have queries now written for the DocumentClient the change wouldn't be as easy as substituting dynamoDB. You'll need to rewrite the queries too.
1

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.

2 Comments

Hi! Can you please tell me where you've read about the environment update? Frankly I still am getting the 'dynamodb.transactWriteItems is not a function` error event when switched to Node 10X
@wiktus239 see my answer for possible solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.