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();
  • 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 – Mrk Fldig Dec 23 '18 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. –  Dec 23 '18 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. – Mrk Fldig Dec 23 '18 at 11:41
  • Thanks, Mrk. I think I'll be needing help creating a deployment package. Haven't tried that before. :-( –  Dec 23 '18 at 12:53
  • So really short answer is here https://stackoverflow.com/questions/34437900/how-to-load-npm-modules-in-aws-lambda - 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. – Mrk Fldig Dec 23 '18 at 19:24

3 Answers3

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.

Prabhakar Reddy
  • 4,628
  • 18
  • 36
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)

Frank
  • 399
  • 4
  • 16
  • 2
    Exactly my case too. So versions are fine now but you need to instantiate a different class. – wiktus239 Sep 04 '19 at 09:04
  • 1
    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. – wiktus239 Sep 04 '19 at 09:07
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.

Schof
  • 123
  • 2
  • 3
  • 1
    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 Sep 01 '19 at 15:35
  • @wiktus239 see my answer for possible solution – Frank Sep 03 '19 at 16:38