0
votes

I'm new to Angular and Node and have also been playing about with AWS recently. I like the concept of serverless computing so am attempting to write an AngularJS app which will invoke Node.JS microservices hosted on AWS Lambda.

I've got as far as invoking my Lambda function through an Angular factory and getting a successful response. I can output the Lambda payload I've defined in my Lambda function to the console from within the Angular factory after calling the lambda.invoke() method, but I can't get the data into my controller to actually use it. I just get an "undefined" error.

I've been reading around it for hours, on this site and others and my inexperience with Angular is making this very hard to work through myself, so any help is appreciated. I feel like the answer must be some variation on this (angular controller is executing before factory complete) .......but I just can't get it working

The factory is below:

my.factory("AWSbits", function() { 


var params = {
FunctionName: 'arn:aws:lambda:eu-west-1:xyz',
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: JSON.stringify ( {  
".....some stuff"
}),

};

var lambda = new AWS.Lambda();

return{
   lambdaexecute : function(){
   lambda.invoke(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
    else {
      console.log(data);           // successful response
      if (data.Payload != '{}') {
          jdata = JSON.parse(data.Payload);
           return jdata;
      }

  }
 })}};
);

The controller is:

my.controller('AWSController', [
              '$scope',
              'AWSbits',

     function($scope, AWSbits){
         $scope.output = AWSbits.lambdaexecute();
         console.log($scope.output);  ====> undefined

      }    

]);
1

1 Answers

0
votes

I have same issue like you.please find my solutions

Service.js

app.service('Lambda', function() {

  AWS.config.update({accessKeyId: 'accessKeyId here', secretAccessKey: 'secretAccessKey here'});
  AWS.config.region = 'us-west-1';

  AWS.config.apiVersions = {
    lambda: '2015-03-31',
    // other service API versions
  };

  var lambda = new AWS.Lambda();

  return {


    LambdaAPI: function (params,callback) {

      return lambda.invoke(params,callback);
    }
  }

});

Controller

app.controller('abcController', function($scope,Lambda) {
    var params = {
                FunctionName: 'function-name-here', /* required */
                Payload:'{"userId": "1","email": "[email protected]"}'

                };
    Lambda.LambdaAPI(params,function(err, data){
                if (err) console.log(err, err.stack); // an error occurred
                      console.log(data);
    })