title: How do I recive back messages through MQTT from lambda function?
body: <p>Let me introduce my problem to you, first of all I have to say that i'm newbie at AWS and MQTT, I mean (don't be mad at me), my question: I want to publish through a MQTT client to AWS IoT/Lambda a simple JSON {'petition':'Hola', 'n':0}, and once published, the lambda function has to send back the JSON message {'petition': 'Mundo', 'n': 0} and it has to do it 10 times. The code works fine when u publish the message but it does not recive any. Please I need help to solve it.</p>
  <p>Here's the squema:</p>
  <h2>DEVICE ---&gt; MQTT ---&gt; IoT SQL SENTENCE ---&gt; AWS LAMBDA</h2>
  <h2>DEVICE &lt;--- MQTT &lt;--- AWS LAMBDA</h2>
  <p>NOTE: the device is running in linux terminal and messages has to appear somehow on the screen.</p>
  <h2>AWS Lambda handler</h2>
  <pre><code>import json
  
  def lambda_handler(event, context):
      # TODO implement
      data={}
      data[&quot;petition&quot;]=&quot;Mundo&quot;
      data[&quot;n&quot;]=event[&quot;n&quot;]
      mensaje = event['petition']
      if mensaje =='Hola':
          envio=json.dumps(data)
          return data
      else:
          return 'nada encontrado'
  </code></pre>
  <h2>IoT SQL SENTENCE</h2>
  <pre><code>SELECT * FROM 'Hello_World'
  </code></pre>
  <p>NOTE: those 2 above are implemented using lambda function service.</p>
  <h2>DEVICE CODE</h2>
  <pre><code>import paho.mqtt.client as paho
  import os
  import socket
  import ssl
  import time
  import json
  
  
  
  def on_connect(client, userdata, flags, rc):
      print(&quot;Connection returned result: &quot; + str(rc) )
      # Subscribing in on_connect() means that if we lose the connection and
      # reconnect then subscriptions will be renewed.
  
  def on_message(client, userdata, msg):
      print(&quot;topic: &quot;+msg.topic)
      print(&quot;payload: &quot;+str(msg.payload))
      data=json.loads(msg.payload)
      
      if data[&quot;n&quot;]&lt;9 and data[&quot;petition&quot;]==&quot;Mundo&quot;:
          data[&quot;petition&quot;] = &quot;Hola&quot;
          data[&quot;n&quot;] = n+1
          envio=json.dumps(data)
          mqttc.publish(thingName, envio)
          time.sleep(4)
  
  def on_publish(client,userdata,missatge):           #create function for callback
      print(&quot;data published \n&quot;)
  
  print &quot;Create client instance&quot;
  mqttc = paho.Client()
  mqttc.on_connect = on_connect
  mqttc.on_publish = on_publish    
  mqttc.on_message = on_message
  
  #mqttc.on_log = on_log
  data={}
  awshost = &quot;a20u2bg4i3u0uq.iot.eu-west-2.amazonaws.com&quot;
  awsport = 8883
  clientId = &quot;39cfba3c326847909aa5f4544211f4a4&quot;
  thingName = &quot;Hello_World&quot;
  caPath = &quot;/#/rootCA.pem&quot;
  certPath = &quot;/#/3218b320c9-certificate.pem.crt&quot;
  keyPath = &quot;/#/3218b320c9-private.pem.key&quot;
  
  mqttc.tls_set(caPath, certfile=certPath, keyfile=keyPath, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
  print &quot;connecting to browser&quot;
  mqttc.connect(awshost, awsport, keepalive=60)
  
  mqttc.loop_start()
  time.sleep(2)
  print&quot;subscribing....&quot;
  mqttc.subscribe(thingName)
  print&quot;subscribed&quot;
  time.sleep(2)
  
  data[&quot;petition&quot;]=&quot;Hola&quot;
  data[&quot;n&quot;]=0
  envio=json.dumps(data)
  print(envio)
  print &quot;Sending...&quot;
  mqttc.publish(thingName, envio)
  
  time.sleep(4)
  print &quot;Sent&quot;
  
  
  mqttc.disconnect()
  mqttc.loop_stop()
  </code></pre>