can't call transactWrite on AWS DynamoDB DocumentClient from lambda

Viewed 2029

I have a lambda function (node.js) that needs to perform a transaction on two DynamoDB tables.

let ddb = new AWS.DynamoDB.DocumentClient();

  ddb.transactWrite(params, function(err, data) {
    if (err) {
      ...
    }
    else {
      ...
    }
  });

when I try to use it I receive the error:

ddb.transactWrite is not a function

I also tried to use AWS.DynamoDB.transactWriteItems() with the same result

3 Answers

Update September 2019

The Lambda Runtime Environments have been updated at some point in the last few months - these are now the most recent versions of the SDK per Runtime environment (that include the SDK) according to the documentation:

  • nodejs10.x - AWS SDK 2.488.0
  • nodejs8.10 - AWS SDK 2.488.0
  • python3.7 - boto3-1.9.42 botocore-1.12.42
  • python3.6 - boto3-1.7.74 botocore-1.10.74
  • ruby2.5 - AWS SDK 3.0.1

Old Post

From the documentation (emphasis mine):

The underlying AWS Lambda execution environment includes the following software and libraries.

Operating system – Amazon Linux

AMI – amzn-ami-hvm-2017.03.1.20170812-x86_64-gp2

Linux kernel – 4.14.77-70.59.amzn1.x86_64

AWS SDK for JavaScript – 2.290.0

SDK for Python (Boto 3) – 3-1.7.74 botocore-1.10.74

The most recent version of the aws-sdk at the time of this writing ist 2.382 - the default version in the Lambda Execution Environment is 5 months old.

I just verified that and the documentation is up to date:

console.log("AWS SDK Version " + AWS.VERSION) // AWS SDK Version 2.290.0

DynamoDB Transactions have been announced in November 2018, which is ~ 3 months ago.

Most likely transactWrite is not yet implemented in that version - you'll probably need to include your own.

Like pointed out by Maurice, the problem is that Lambda does not use the last version of aws-sdk (at least not in the moment I am writing).

To use the last version:

  • download the lambda deployment package;
  • install last version of the the aws-sdk with npm install aws-sdk;
  • re-upload the package inside lambda;

At this point, when you import aws-sdk with require('aws-sdk');, lambda uses the last version of the sdk.

Note: the aws-sdk package increases the package size of you lambda to more than 3MB, so you will not be able to edit the code in the inline code editor. Best option is to edit the code locally and upload it using the CLI.

Update Your Runtime

The transactWrite function was added in a later version of the AWS SDK. To access the updated AWS SDK, switch your Lambda function to a newer runtime.

AWS does not update the AWS SDK version for old runtimes. For example, if you are using the outdated nodejs8.10 runtime, you only have access to version 2.290.0 of the AWS SDK which does not support transactWrite. If you switch your runtime to nodejs10.x, you get version 2.437.0 (at the time of this post) which does support DynamoDB transactions.

View the complete list of SDK versions available in each runtime here: AWS Lambda Runtimes

How To Update Your Runtime

To update your runtime, click the "Runtime" dropdown in the "Function Code" section of your Lambda function and select an updated runtime.

Update Lambda runtime

Related