如何使用 lambda 创建 HarperDB table

How to create a HarperDB table with lambda

我写了下面的 Node.js 代码来在 HarperDB Cloud. This code executes perfectly when executed locally. I moved the same code to AWS Lambda 上创建一个 table,唉,代码执行没有任何错误,但是 table 没有得到已创建。

我怀疑我调用 lambda 函数的方式有问题,但我无法找出问题所在。我该如何解决?


exports.handler = async (event,) => {

  var https = require('follow-redirects').https;
  var fs = require('fs');

  var options = {
    'method': 'POST',
    'hostname': 'MyInstanceName-MyAccount.harperdbcloud.com',
    'path': '/',
    'headers': {
      'Authorization': 'Basic MyAuthoriztionCode',
      'Content-Type': 'application/json'
    },
    'maxRedirects': 20
  };

  var req = https.request(options, function (res) {
    var chunks = [];

    res.on("data", function (chunk) {
      chunks.push(chunk);
    });

    res.on("end", function (chunk) {
      var body = Buffer.concat(chunks);
      console.log(body.toString());
    });

    res.on("error", function (error) {
      console.error(error);
    });
  });

  var postData = JSON.stringify({
    "operation": "create_table",
    "schema": "MySchema",
    "table": "NewTable",
    "hash_attribute": "Id"
  });

  req.write(postData);
  req.end();

  const response = {
      statusCode: 200,
      body: JSON.stringify('Table created successfully!'),
  };

  return response;

};

/// AWS Lambda Function (Node.js code) ends here'''

我相信这是因为您在异步 lambda 处理程序中使用了回调。

尝试将 lambda 转换为回调

exports.handler = (event, context, callback) => {}

或使用 async/await 请求库。