AWS Lambda:异步调用外部处理程序(初始化部分,调用lambda)

时间:2019-03-18 18:36:42

标签: asynchronous async-await aws-lambda initialization aws-secrets-manager

我想通过以下代码在lambda处理程序之外调用异步函数:

var client;
(async () => {
    var result =  await initSecrets("MyWebApi");
    var secret = JSON.parse(result.Payload);
    client= new MyWebApiClient(secret.API_KEY, secret.API_SECRET); 
});

async function initSecrets(secretName) {
    var input = {
    "secretName" : secretName
    };
    var result = await lambda.invoke({
       FunctionName: 'getSecrets',
       InvocationType: "RequestResponse",
       Payload: JSON.stringify(input)
    }).promise();
    return result;
}

exports.handler = async function (event, context) {

    var myReq = await client('Request');
    console.log(myReq);
};

“客户端”未初始化。如果在处理程序中执行相同的代码,则效果很好。 initSecrets包含getSecrets()的lambda调用,该调用调用AWS SecretsManager 有谁知道如何在处理程序外部正确调用异步函数以进行初始化?

非常感谢您的支持。

3 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,试图使next-js与aws-serverless-express一起使用。

我通过执行以下操作(使用打字稿,因此忽略了:any type bits)将其修复了

const appModule = require('./App');
let server: any = undefined;

appModule.then((expressApp: any) => {
  server = createServer(expressApp, null, binaryMimeTypes);
});

function waitForServer(event: any, context: any){
  setImmediate(() => {
    if(!server){
      waitForServer(event, context);
    }else{
      proxy(server, event, context);
    }
  });
}

exports.handler = (event: any, context: any) => {
  if(server){
    proxy(server, event, context);
  }else{
    waitForServer(event, context);
  }
}

对于您的代码,也许像

var client = undefined;

initSecrets("MyWebApi").then(result => {
    var secret = JSON.parse(result.Payload);
    client= new MyWebApiClient(secret.API_KEY, secret.API_SECRET)
})

function waitForClient(){
  setImmediate(() => {
    if(!client ){
      waitForClient();
    }else{
      client('Request')
    }
  });
}

exports.handler = async function (event, context) {
  if(client){
    client('Request')
  }else{
    waitForClient(event, context);
  }
};

答案 1 :(得分:0)

client在初始化之前被调用; client函数将在async函数完成之前被“导出”(并调用)。当您致电await client()时,客户仍然是undefined

编辑,尝试类似的操作

var client = async which => {
    var result =  await initSecrets("MyWebApi");
    var secret = JSON.parse(result.Payload);
    let api = new MyWebApiClient(secret.API_KEY, secret.API_SECRET); 
    return api(which) // assuming api class is returning a promise
}

async function initSecrets(secretName) {
    var input = {
    "secretName" : secretName
    };
    var result = await lambda.invoke({
       FunctionName: 'getSecrets',
       InvocationType: "RequestResponse",
       Payload: JSON.stringify(input)
    }).promise();
    return result;
}

exports.handler = async function (event, context) {

    var myReq = await client('Request');
    console.log(myReq);
};

答案 2 :(得分:0)

这也可以通过async / await给Node v8 +来解决

您可以像这样在模块中加载配置...

const fetch = require('node-fetch');

module.exports = async () => {

  const config = await fetch('https://cdn.jsdelivr.net/gh/GEOLYTIX/public/z2.json');

  return await config.json();

}

然后通过require /执行config模块在处理程序外部声明_config。您的处理程序必须是异步函数。 _config首先是一个承诺,您必须等待将其解析为配置对象。

const _config = require('./config')();

module.exports = async (req, res) => {

  const config = await _config;

  res.send(config);

}