3

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?

IgorAlves
  • 5,086
  • 10
  • 52
  • 83

2 Answers2

3

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);
Rahul Sharma
  • 5,562
  • 4
  • 24
  • 48
2

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);
ravi
  • 949
  • 11
  • 22
  • I did not use this `CreateDatabaseCommand` because the database already exists. If I use the code suggest by you I will have the following error.stack: `ConflictException: The database smaj56g already exists. ...` – IgorAlves Aug 16 '22 at 17:34
  • If I change the database name for `smaj56gx` it is created, no table associated and next lambda will throw the same previous error: `ConflictException: The database smaj56g already exists. ...` – IgorAlves Aug 16 '22 at 17:41
  • But it works if I use the following command `const command = new WriteRecordsCommand(params)`. Now I got the logic. I need to choose the desired command. – IgorAlves Aug 16 '22 at 17:48
  • Yes, you will need to use the appropriate command. I was just giving you an example – ravi Aug 16 '22 at 19:24