title: AWS Lambda Timeout (NodeJS - when calling Google Calendar API)
body: <p>I took the code from here: <a href="https://developers.google.com/google-apps/calendar/quickstart/nodejs" rel="nofollow noreferrer">https://developers.google.com/google-apps/calendar/quickstart/nodejs</a> and got it working locally and to return a JSON object of the calendar events. </p>
  
  <p>I turned it into a module called GoogleCalendarAPI.js, and still had it working locally. </p>
  
  <p>I tried the approach from this StackOverflow answer: <a href="https://stackoverflow.com/questions/42170504/how-to-oauth-google-api-from-lambda-aws">How to oAuth Google API from Lambda AWS?</a>.  I'm getting the same result whether I use the Token, retrieve as described there; or whether I use the client_secret.json file. </p>
  
  <pre><code>exports.getCalendarJSONEvents = 
    function getCalendarJSONEvents(callback) {
      console.log("started getCalendarJSONEvents");
      listEvents(TOKEN, function(jsonResult) {
           console.log("Json Callback Events="); 
           console.log(jsonResult); 
           callback(jsonResult); 
      });
  }
   function listEvents(auth, callback) {... etc... 
  </code></pre>
  
  <p>The calling Lambda index.js does this: </p>
  
  <pre><code>console.log("log: WhenIntent");
  var GoogleCalendarAPI = require('./GoogleCalendarAPI.js');
  GoogleCalendarAPI.getCalendarJSONEvents(
      function callback(calEvents){
          console.log("WRAPPER: calendarResults=" + 
                       JSON.stringify(calEvents));
          // chunk misc code here 
          this.emit(':tell', speechText); 
   });
  </code></pre>
  
  <p>The last thing I see in my log is "log: WhenIntent".  It's an Alexa App, and I'm using my user account to access one public calendar. I have no idea what it's doing after that, and not sure how to debug it any further.  I'm not sure why I don't see "started getCalendarJSONEvents" before the timeout (default 3 secsond, and also tried 5 seconds).</p>
  
  <p>Update 1:</p>
  
  <p>Okay, the CloudWatch Log did show "The API returned an error: Error: Bad Request", and I thought it was Lambda error, but it was coming from the listEvents.  I added line noted below, and at least now the logic flowed back, and I will the reason for the "Bad Request" later. </p>
  
  <pre><code>function listEvents(auth, callback) {
    console.log("log: start listEvents"); 
    var calendar = google.calendar('v3');
    console.log("log: listEvents got calendar var"); 
    var jsonOut = "{"; 
    calendar.events.list({
      auth: auth,
      calendarId: 'somename@group.calendar.google.com',
      timeMin: (new Date()).toISOString(),
      maxResults: 10,
      singleEvents: true,
      orderBy: 'startTime'
    }, function(err, response) {
      if (err) {
        // made this error more clear! 
        console.log('The Google Calendar API returned an error: ' + err);
        callback(err);  // added this line 
        return;
      }
      var events = response.items;
      if (events.length == 0) {
        console.log('No upcoming events found.');
      } else {
        console.log('Upcoming 10 events:');
        var jsonLine = ""; 
        for (var i = 0; i &lt; events.length; i++) {
          var event = events[i];
          var start = event.start.dateTime || event.start.date;
          console.log('%s - %s', start, event.summary);
          jsonLine = 
              [{ 'date': event.start.date,
                'summary': event.summary
              }]; 
          console.log("jsonline=")
          jsonOut = jsonOut + JSON.stringify(jsonLine) + ","; 
  
        }
        jsonOut = jsonOut + "}"; 
      }
      console.log("End of listEvents: jsonOut = " + 
                  JSON.stringify(jsonOut)); 
      callback(jsonOut); 
    });
  </code></pre>