Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

s3.getObject inside lambda doesn't return anything

I have the following code, inside a lambda function, which is an Amazon echo skill:

"AMAZON.HelpIntent": function (intent, session, response) {
  var speechOutput ="Start";

  // Try S3
  var s3 = new AWS.S3({httpOptions: { timeout: 2000 }});
  var params = {
    Bucket: 'bucket',
    Key: 'file',
  };

  s3.getObject(params, function (err, data) {
    if (err) {
      // console.log(err, err.stack);
      speechOutput += "inside error";
      speechOutput += "Did not get it!" + err + ":===:" + err.stack;
    }
    else {
      speechOutput += "inside success";
      // console.log(data);
      speechOutput += "Got it! :" + data.Body.toString('ascii');
    }
  });

  speechOutput += " End. ";

  var repromptText = "Help reprompt.";
  response.ask(speechOutput, repromptText);
},

seems pretty straight forward... however, when the skill is executed, the response is this:

    {
          "version": "1.0",
          "response": {
            "outputSpeech": {
              "type": "PlainText",
              "text": "Start End. "
            },
            "shouldEndSession": false,
            "reprompt": {
              "outputSpeech": {
                "type": "PlainText",
                "text": "Help reprompt."
              }
            }
          },
          "sessionAttributes": {}
   }

In other words, s3.getObject doesn't throw any errors, and doesn't run.

The lamda function has 'lambda_basic_execution' role, which in IAM is defined to have full access to the S3:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:*",
      "Resource": "*"
    }
  ]
}

Inside S3, there is a bucket (named 'bucket') and file (named 'file'). File is accessible from CLI, and the web. Permissions on the bucket and file are set to be "everyone".

Please help.

like image 493
Mark Rogov Avatar asked Sep 10 '16 19:09

Mark Rogov


1 Answers

You aren't waiting for the S3 call to complete. The last 3 lines in your code are executing before the s3.getObject() callback has been called. Try changing your code by moving those last 3 lines into the callback, like this:

"AMAZON.HelpIntent": function (intent, session, response) {
  var speechOutput ="Start";

  // Try S3
  var s3 = new AWS.S3({httpOptions: { timeout: 2000 }});
  var params = {
    Bucket: 'bucket',
    Key: 'file',
  };

  s3.getObject(params, function (err, data) {
    if (err) {
      // console.log(err, err.stack);
      speechOutput += "inside error";
      speechOutput += "Did not get it!" + err + ":===:" + err.stack;
    }
    else {
      speechOutput += "inside success";
      // console.log(data);
      speechOutput += "Got it! :" + data.Body.toString('ascii');
    }


    speechOutput += " End. ";

    var repromptText = "Help reprompt.";
    response.ask(speechOutput, repromptText);
  });
},
like image 78
Mark B Avatar answered Oct 28 '22 21:10

Mark B