AWS Lambda异步/等待样板

时间:2018-12-11 11:05:26

标签: async-await aws-lambda

有没有一个示例如何在AWS Lambda函数中完全使用async / await?最lambda示例以以下内容开头:

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

现在我尝试使用:

module.exports.hello = async (event, context) => {

但是现在我必须在此lambda函数中调用lambda函数。

我可以只写:

'use strict';
const AWS = require('aws-sdk');
const lambda = new AWS.Lambda({
  region: 'my-region'
});
let params = {
  FunctionName: process.env.lambdafunc, /* required */
  Payload: "",
  InvocationType: "Event"
};

module.exports.hello = async (event, context) => {

  /** HERE COMES SOME CODE AND BUSINESS LOGIC  
   * ...
   * ...
  */

  params.Payload = new Buffer(JSON.stringify(MYJSONDATA));

  data = await lambda.invokeAsync(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
  });

  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Go Serverless v1.0! Your function executed successfully!',
      data: data
    }),
  };
};

0 个答案:

没有答案