0

i am trying to invoke an aws lamda function based on an email trigger. i get the email trigger in and it hits the if statement i expect but then it fails to do the lambda.invoke.

what am i missing? i get to most of the log statements but do not seem

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")
        }

};
Matt617
  • 458
  • 2
  • 14
  • This post probably be helpful [https://stackoverflow.com/questions/31714788/can-an-aws-lambda-function-call-another](https://stackoverflow.com/questions/31714788/can-an-aws-lambda-function-call-another) – Kavindra Oct 08 '17 at 23:40
  • Did you happen to place your Lambda function inside your VPC? If so, you'll need a NAT Gateway in order for the Lambda function to access the AWS API (in order to invoke the other function). Or just remove it from the VPC. If that's not it then try posting the full log. – Mark B Oct 09 '17 at 00:48

2 Answers2

0
  1. keep your aws-sdk file on top of your code

    `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")
            }
    
        };
    
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
Bala Krishna
  • 47
  • 2
  • 8
-2

It sounds like you want Step Functions. They will allow you to chain different lambda functions together, do things in parallel, etc.

chris
  • 36,094
  • 53
  • 157
  • 237