skip to Main Content

I have the following lambda function in NodeJs 14.x using AWS SDK V3 for a timestream insertion process:

'use strict'
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-timestream-write/index.html
const { TimestreamWriteClient } = require("@aws-sdk/client-timestream-write")

const client = new TimestreamWriteClient({ region: process.env.region  })


module.exports.fnPostElectricityTimestream = async event => {
  try {
    console.log('🚀 START fnPostElectricityTimestream')

    const jsonBody = event 
    const topic = jsonBody.topic
    const arrTopic = topic.split('/')
    let dbName = arrTopic[4]
    dbName = 'smaj56g' //Test

    const currentTime = Date.now().toString() // Unix time in milliseconds get jsonBody.e_timestamp
    const e_timestamp = (jsonBody.e_timestamp)*1000

    const dimensions = [{
        'Name': 'n',
        'Value': 'v' 
    }]
    const e_ch_1 = {
        'Dimensions':dimensions,
        'MeasureName': 'e_ch_1',
        'MeasureValue': '[1,2,3]',
        'MeasureValueType': 'VARCHAR',
        'Time': currentTime
    }  
    const records = [e_ch_1]
    const params = {
        DatabaseName: dbName,
        TableName:'e_ch_1_v_w',
        Records: records
    }
    const data = await client.send(params);
    console.log('data', data)

    return {
        message: ''
    }
  } catch (error) {
    console.log('🚀 fnPostElectricityTimestream - error.stack:', error.stack)
    return {
        message: error.stack
    }
  }
}

When I run the lambda this is the message I am getting:

2022-08-12T14:58:39.496Z    e578a391-06b4-48a9-9f9d-9440a373c19e    INFO    🚀 fnPostElectricityTimestream - error.stack: TypeError: command.resolveMiddleware is not a function
    at TimestreamWriteClient.send (/var/task/node_modules/@aws-sdk/smithy-client/dist-cjs/client.js:13:33)
    at Runtime.module.exports.fnPostElectricityTimestream [as handler] (/var/task/src/ElectricityTimestream/fnPostElectricityTimestream.js:38:31)
    at Runtime.handleOnceNonStreaming (/var/runtime/Runtime.js:73:25)

There is something with const data = await client.send(params).
I am following the asyncawait code in this documentation.

How to solve this issue?

2

Answers


  1. you need to create a command before calling send.

    For example:

    import { TimestreamWriteClient, CreateDatabaseCommand } from "@aws-sdk/client-timestream-write";
    
    const params = {
      DatabaseName: dbName,
      TableName:'e_ch_1_v_w',
      Records: records 
    } 
    
    const command = new CreateDatabaseCommand(params);
    const data = await client.send(command);
    
    Login or Signup to reply.
  2. Your current insertion code is wrong. In order to write the records in the TimeStream, you need to use the WriteRecordsCommand command. Refer to the doc for a better understanding. Sample code:

    import { TimestreamWriteClient, WriteRecordsCommand } from "@aws-sdk/client-timestream-write";
    
    const client = new TimestreamWriteClient({ region: "REGION" });  //your AWS region
    const params = {
      DatabaseName: dbName,       //your database
      TableName: tableName,       //your table name
      Records: records            //records you want to insert
    } 
    
    const command = new WriteRecordsCommand(params);
    const data = await client.send(command);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search