如何从另一个调用aws lamda函数

时间:2017-10-08 23:11:23

标签: javascript amazon-web-services aws-lambda

我正在尝试根据电子邮件触发器调用aws lamda函数。我得到了电子邮件触发器,它命中了我期望的if语句,但后来它没能完成lambda.invoke。

我错过了什么?我得到了大多数日志语句,但似乎不是

exports.handler = function(event, context) {
    var aws = require('aws-sdk');

    var lambda = new aws.Lambda({
        region: 'us-east-1'
    });

    var sesNotification = event.Records[0].ses;
    //console.log("SES Notification:\n", JSON.stringify(sesNotification, null, 2));
    var sub=sesNotification.mail.commonHeaders.subject;
        if(sub){
            if(sub.toLowerCase()=="startpipeline"){
                console.log("Starting Pipeline");
                lambda.invoke({
                              FunctionName: 'StartDevOpsServers',
                              Payload: JSON.stringify(event, null, 2), // pass params
                              InvocationType: 'Event' 
                            }, function(error, data) {
                              if (error) {

                                console.log("error",error,data);
                                context.done('error', error);
                              }
                              if(data.Payload){

                               console.log("succeed",data.Payload);
                               context.succeed(data.Payload)
                              }
                            });

            }else if(sub.toLowerCase()=="stoppipeline"){
                console.log("Stopping Pipeline");
                                lambda.invoke({
                              FunctionName: 'StopDevOpsServers',
                              Payload: JSON.stringify(event, null, 2) // pass params
                            }, function(error, data) {
                              if (error) {
                                context.done('error', error);
                              }
                              if(data.Payload){

                               context.succeed(data.Payload)
                              }
                            });
                    context.succeed();   

            }else{
                console.log('subjectnotRecognized')
            }
        }else{
            console.log("noSubJect")
        }

};

2 个答案:

答案 0 :(得分:0)

  1. 将aws-sdk文件保存在代码之上

      

    `var aws = require('aws-sdk');

    exports.handler = function(event, context) 
    {
    
        var lambda = new aws.Lambda({
            region: 'us-east-1'
        });
    
        var sesNotification = event.Records[0].ses;
        //console.log("SES Notification:\n", JSON.stringify(sesNotification, null, 2));
        var sub=sesNotification.mail.commonHeaders.subject;
            if(sub){
                if(sub.toLowerCase()=="startpipeline"){
                    console.log("Starting Pipeline");
                    lambda.invoke({
                                  FunctionName: 'StartDevOpsServers',
                                  Payload: JSON.stringify(event, null, 2), // pass params
                                  InvocationType: 'Event' 
                                }, function(error, data) {
                                  if (error) {
    
                                    console.log("error",error,data);
                                    context.done('error', error);
                                  }
                                  if(data.Payload){
    
                                   console.log("succeed",data.Payload);
                                   context.succeed(data.Payload)
                                  }
                                });
    
                }else if(sub.toLowerCase()=="stoppipeline"){
                    console.log("Stopping Pipeline");
                                    lambda.invoke({
                                  FunctionName: 'StopDevOpsServers',
                                  Payload: JSON.stringify(event, null, 2) // pass params
                                }, function(error, data) {
                                  if (error) {
                                    context.done('error', error);
                                  }
                                  if(data.Payload){
    
                                   context.succeed(data.Payload)
                                  }
                                });
                        context.succeed();   
    
                }else{
                    console.log('subjectnotRecognized')
                }
            }else{
                console.log("noSubJect")
            }
    
        };
    

答案 1 :(得分:-2)

听起来你想要步骤功能。它们允许您将不同的lambda函数链接在一起,并行执行等等。