Skip to content

API GATEWAY +LAMBDA +DAX+DYNAMO

0

HOW CAN WE WRITE LAMBDA CODE FOR LAMBDA WHICH IS USING DAX FOR FETCHING ID IN V3 SDK ? PLEASE PROVIDE ANY REFERENCE OR SAMPLE CODE IN JAVASCRIPT OR TYPESCRIPT

asked 2 years ago452 views
3 Answers
2

Update - Mar 24, 2025

The Amazon DynamoDB Accelerator (DAX) SDK for JavaScript version 3 is now available.


You cannot use Javascript V3 to read/write to DAX as that functionality does not yet exist. On JS V3, you can only handle DAX control plane operations for now. There is no ETA for when dataplane actions will be available for V3 but you can keep up to date here: https://aws.amazon.com/new/

AWS
EXPERT
answered 2 years ago
0

I had to do this method to get dax working in nodejs on lambda

  1. create a lambda layer with amazon-dax-client
From your workspace/nodelayer-dax directory
 $mkdir nodejs
 $cd nodejs
 $npm init -y
 $npm install aws-sdk --save-prod
 $npm install amazon-dax-client --save-prod
 $cd .. -> here you zip the nodejs directory into a zip file which is the layer code (most likely will be needed to upload to S3)
  1. in AWS lambda - add a new layer for the node version you are using and specify the S3 location where you uploaded the zip file
  2. In the Lambda code you include dax client as such : where you need the require (or import) for the lambda layer package (for some reason amazon itself doesnt provide this version of the sdk and dax client out of the box in lambda)
// DAX setup Start
var AWS = require("aws-sdk");
const AmazonDaxClient = require('amazon-dax-client');

const DAX_SETTINGS_CACHE_ENDPT = 'daxs://your-dax-cluster.abcd123.dax-clusters.us-west-2.amazonaws.com'; // whatever cluster endpt you setup
const DDB_TABLE_NAME = 'yourddbtable';
const TABLE_RECORD_PKEY = 'partitionKeyInDDBTable';

var dax = new AmazonDaxClient({endpoints: [DAX_SETTINGS_CACHE_ENDPT], region: AWS_REGION}) // region must match
var daxClient = new AWS.DynamoDB.DocumentClient({service: dax });
// DAX setup end
...
// dax client usage (use whichever read method appropriate - getitem/query)
async function getDaxData() {
  var params = {
    TableName: DDB_TABLE_NAME,
    ExpressionAttributeNames: {"#pkcolumn": "pkcolumnVal"},
    ExpressionAttributeValues: {
      ':pkcolumnVal': TABLE_RECORD_PKEY
    }
  };
  // this needs permissions for DDB to get 
  var cachedDAXData = await daxClient.get(params);
  if (cachedDAXData.Count > 0) {
    return cachedDAXData.Items[0].value;
  } else {
    console.log('DAX data is empty: ' + simpleStringify(cachedDAXData));
  }
}

if above doesn't work - then I recommend creating a tiny EC2 instance and install node on it using yum/ubuntu apt get and writing an express js node application that has the direct import of the AWS dax modules without needing to deal with lambda (yeah it is nt serverless but it actually performs faster than lambda - just that you have to manually scale the cluster if you need scaling)

answered 6 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.