AWS Lambda呼叫Lambda [英] AWS Lambda call Lambda

查看:108
本文介绍了AWS Lambda呼叫Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我拥有的另一个Lambda函数中调用.我设置了权限,这样就没问题了.

I'm trying to call one Lambda function from another one that I have. I set up my permissions so that is not problem.

我的问题是该函数不等待Invoke函数完成并一直返回NULL.

My problem is that the function doesn't wait for the Invoke function to complete and return NULL all the time.

这是我正在使用的代码:

Here is the code I'm using:

const AWS = require('aws-sdk');

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

    var lambda = new AWS.Lambda({region: 'us-east-1', apiVersion: '2015-03-31'});

    var params = {
        FunctionName: 'testFunction',
        InvocationType: 'RequestResponse'
    }
    lambda.invoke(params, function(err, data){
        console.log(err);
        console.log('here');
    }).promise().then(data=> { callback(null, {message:'done'}); });

};

{message:'done'}从未显示.建议我使用invokeAsync,但AWS不推荐使用该功能.

The {message:'done'} its never shown. I was recommended to use invokeAsync but that function is deprecated by AWS.

我知道问题在于该函数正在以同步方式运行lambda.invoke,因为如果我在lambda.invoke函数之外添加callback(null, {message:'done'});,那么我可以看到console.logs在工作.

I know the problem is that the function is running lambda.invoke as synchronously because if I add callback(null, {message:'done'}); outside of the lambda.invoke function then I can see the console.logs working.

有帮助吗?

推荐答案

TL; DR-删除第3行中的异步",它应该可以工作.

您的问题似乎是由 async 关键字引起的.我已经重新创建了该代码,并将其部署到Lambda上以在Node v8.10上运行(但是当然要使其指向我自己的lambda函数之一).

Your issue seems to be caused by the async keyword here. I have recreated this and deployed it to Lambda to run on Node v8.10 (but pointing it to invoke one of my own lambda functions of course).

无论如何,为什么在这里使用异步"? async 关键字声明定义了一个异步函数并返回一个AsyncFunction对象. AWS Lambda应该是一个函数,而不是AsyncFunction,并且您的空"结果可能只是Lambda立即放弃,因为它找不到常规函数.另外,async几乎完全与await一起使用(至少在我所见过的情况中,至少有99%用过),并且由于您的代码根本没有使用await,因此我看不出任何原因要么使用async.

Why are you using "async" here anyway? The async keyword declaration defines an asynchronous function and returns an AsyncFunction object. AWS Lambda is expected a function, not an AsyncFunction, and your "null" result is probably just Lambda immediately giving up because it can't find a regular function. Also, async is almost exclusively used with await (at least is was in 99% of the cases I've seen), and since your code isn't using await at all I don't see any reason to use async either.

这篇关于AWS Lambda呼叫Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆