invoke aws lambda from another lambda asynchronously

后端 未结 5 1999
小蘑菇
小蘑菇 2020-12-09 03:24

I need to invoke aws lambda from another lambda asynchronously. i have a working code for synchronous calls.

exports.handler = (event, context, callback) =&g         


        
5条回答
  •  囚心锁ツ
    2020-12-09 04:19

    I wanted a similar solution as above. Though step functions are now recommended when using multiple lambda functions over lambda.invoke , I used the following code snippet to asynchronously call two other lambda functions from my base lambda function.

    var AWS = require('aws-sdk');
    AWS.config.region = 'ap-southeast-1';
    var lambda = new AWS.Lambda();
    
    exports.handler = async(event) => {
       await invokeLambda(event);
       
       const response = {
            statusCode: 200,
            body: JSON.stringify('success'),
       };
       
       return response;
    };
    
    //Invoke Multiple Lambda functions
      async function invokeLambda(event) {
        const function1 = {
            FunctionName: 'dev-test-async-lambda-1',
            InvocationType: 'Event',
            Payload: JSON.stringify(event)
        };
    
        const function2 = {
            FunctionName: 'dev-test-async-lambda-2',
            InvocationType: 'Event',
            Payload: JSON.stringify(event)
        };
        
        await lambda.invoke(function1).promise();
        await lambda.invoke(function2).promise();
    
    }
    
            
    

    Let me know if I can improve this.

提交回复
热议问题