title: AWS Lambda cannot publish to IoT topic properly
body: <p>I have written Lambda function using JavaScript, which responses to my voice and turns on the LED on my Raspberry.
  But I have a problem with publishing its state to my thing topic. While Alexa responses correct ("Turning on" if Im asking to turn it on and "Turning off" if asking to off), my topic doesn't always get the state changes. Some times it gets data and sometime it doesn't and after few more invocations it gets data in bulk, and I cant even get the logic of creating a sequence of data in that bulk.</p>
  
  <pre><code>var AWS = require('aws-sdk');
  
  var config = {};
  config.IOT_BROKER_ENDPOINT      = "xxxxxx.iot.us-east-1.amazonaws.com";
  config.IOT_BROKER_REGION        = "us-east-1";
  config.IOT_THING_NAME           = "raspberry";
  
  var iotData = new AWS.IotData({endpoint: config.IOT_BROKER_ENDPOINT});
  var topic = 'LED';
  
  exports.handler = function (event, context) {
  ...
  ...
  function updatePowerState (intent, session, callback) {
      var speechOutput = '';
      var newValue = '';
      var repromptText = '';
      const cardTitle = 'Power';
      var sessionAttributes = {};
      const shouldEndSession = true;
      var value = intent.slots.PowerState.value;
      if(value == 'on' || value == 'off') {
          newValue = value.toUpperCase();
          speechOutput = 'Turning your lamp ' + value;
          updateShadow(newValue);
      } else {
          speechOutput = 'I didnt understand you. Please, repeat your request.';
      }
      callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));    
  }
  
  function updateShadow(newValue) {
          let payload = {
          state: {
              desired: {
                  power_state: newValue
              }
          }
      };
  
      var JSON_payload = JSON.stringify(payload);
  
      var updates = {
          topic: topic,
          payload: JSON_payload,
          qos: 0
      };
  
      iotData.publish(updates, (err, data) =&gt; {
    if(err) {
        console.log(err);
    }
    else {
        console.log('Success!');
    }
  });
  }
  </code></pre>
  
  <p>Do you have any ideas about its causes? Thank you!</p>