AWS Lambda function - can't call update thing shadow










1















According to boto3 documentation here: https://boto3.readthedocs.org/en/latest/reference/services/iot-data.html#client the update_thing_shadow method takes the thingName & JSON payload as parameters. Currently it reads:



 client = boto3.client('iot-data', region_name='us-east-1')
data = "state" : "desired" : "switch" : "on"
mypayload = json.dumps(data)
response = client.update_thing_shadow(
thingName = 'MyDevice',
payload = b'mypayload'
)


When I use the command line there's no problem but can't seem to get it right from within the lamba function. I've called it with numerous versions of code (json.JSONEncoder, bytearray(), etc..) without any luck. The errors range from syntax to (ForbiddenException) when calling the UpdateThingShadow operation: Bad Request: ClientError. Has anyone had success calling this or a similar method from within a AWS lambda function? Thanks.










share|improve this question
























  • What exactly is payload = b'mypayload' supposed to do here?

    – garnaat
    Mar 16 '16 at 17:32











  • update 'MyDevice' with a desired state, in this case set a switch to the 'on' position

    – JC23
    Mar 16 '16 at 20:04






  • 1





    But that is just a string constant containing "mypayload". It's not the actual payload you have stored in the variable called mypayload. That's why I'm confused.

    – garnaat
    Mar 16 '16 at 20:28











  • For clarification see above as stated in initial post "I've called it with numerous versions of code .... without any luck." What's there currently is the latest as all previous attempts failed.

    – JC23
    Mar 16 '16 at 22:24






  • 2





    Well, the example you show above would not appear to be correct. As I stated above, you are supposed to be passing a JSON payload but you are passing a string constant "mypayload". I would suggest fixing that issue and then updating the question with the new code and the exact response you are getting from the service.

    – garnaat
    Mar 17 '16 at 2:50















1















According to boto3 documentation here: https://boto3.readthedocs.org/en/latest/reference/services/iot-data.html#client the update_thing_shadow method takes the thingName & JSON payload as parameters. Currently it reads:



 client = boto3.client('iot-data', region_name='us-east-1')
data = "state" : "desired" : "switch" : "on"
mypayload = json.dumps(data)
response = client.update_thing_shadow(
thingName = 'MyDevice',
payload = b'mypayload'
)


When I use the command line there's no problem but can't seem to get it right from within the lamba function. I've called it with numerous versions of code (json.JSONEncoder, bytearray(), etc..) without any luck. The errors range from syntax to (ForbiddenException) when calling the UpdateThingShadow operation: Bad Request: ClientError. Has anyone had success calling this or a similar method from within a AWS lambda function? Thanks.










share|improve this question
























  • What exactly is payload = b'mypayload' supposed to do here?

    – garnaat
    Mar 16 '16 at 17:32











  • update 'MyDevice' with a desired state, in this case set a switch to the 'on' position

    – JC23
    Mar 16 '16 at 20:04






  • 1





    But that is just a string constant containing "mypayload". It's not the actual payload you have stored in the variable called mypayload. That's why I'm confused.

    – garnaat
    Mar 16 '16 at 20:28











  • For clarification see above as stated in initial post "I've called it with numerous versions of code .... without any luck." What's there currently is the latest as all previous attempts failed.

    – JC23
    Mar 16 '16 at 22:24






  • 2





    Well, the example you show above would not appear to be correct. As I stated above, you are supposed to be passing a JSON payload but you are passing a string constant "mypayload". I would suggest fixing that issue and then updating the question with the new code and the exact response you are getting from the service.

    – garnaat
    Mar 17 '16 at 2:50













1












1








1


1






According to boto3 documentation here: https://boto3.readthedocs.org/en/latest/reference/services/iot-data.html#client the update_thing_shadow method takes the thingName & JSON payload as parameters. Currently it reads:



 client = boto3.client('iot-data', region_name='us-east-1')
data = "state" : "desired" : "switch" : "on"
mypayload = json.dumps(data)
response = client.update_thing_shadow(
thingName = 'MyDevice',
payload = b'mypayload'
)


When I use the command line there's no problem but can't seem to get it right from within the lamba function. I've called it with numerous versions of code (json.JSONEncoder, bytearray(), etc..) without any luck. The errors range from syntax to (ForbiddenException) when calling the UpdateThingShadow operation: Bad Request: ClientError. Has anyone had success calling this or a similar method from within a AWS lambda function? Thanks.










share|improve this question
















According to boto3 documentation here: https://boto3.readthedocs.org/en/latest/reference/services/iot-data.html#client the update_thing_shadow method takes the thingName & JSON payload as parameters. Currently it reads:



 client = boto3.client('iot-data', region_name='us-east-1')
data = "state" : "desired" : "switch" : "on"
mypayload = json.dumps(data)
response = client.update_thing_shadow(
thingName = 'MyDevice',
payload = b'mypayload'
)


When I use the command line there's no problem but can't seem to get it right from within the lamba function. I've called it with numerous versions of code (json.JSONEncoder, bytearray(), etc..) without any luck. The errors range from syntax to (ForbiddenException) when calling the UpdateThingShadow operation: Bad Request: ClientError. Has anyone had success calling this or a similar method from within a AWS lambda function? Thanks.







json amazon-web-services aws-lambda boto3 aws-iot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 16 '16 at 20:36









marc_s

581k13011211268




581k13011211268










asked Mar 16 '16 at 17:16









JC23JC23

312417




312417












  • What exactly is payload = b'mypayload' supposed to do here?

    – garnaat
    Mar 16 '16 at 17:32











  • update 'MyDevice' with a desired state, in this case set a switch to the 'on' position

    – JC23
    Mar 16 '16 at 20:04






  • 1





    But that is just a string constant containing "mypayload". It's not the actual payload you have stored in the variable called mypayload. That's why I'm confused.

    – garnaat
    Mar 16 '16 at 20:28











  • For clarification see above as stated in initial post "I've called it with numerous versions of code .... without any luck." What's there currently is the latest as all previous attempts failed.

    – JC23
    Mar 16 '16 at 22:24






  • 2





    Well, the example you show above would not appear to be correct. As I stated above, you are supposed to be passing a JSON payload but you are passing a string constant "mypayload". I would suggest fixing that issue and then updating the question with the new code and the exact response you are getting from the service.

    – garnaat
    Mar 17 '16 at 2:50

















  • What exactly is payload = b'mypayload' supposed to do here?

    – garnaat
    Mar 16 '16 at 17:32











  • update 'MyDevice' with a desired state, in this case set a switch to the 'on' position

    – JC23
    Mar 16 '16 at 20:04






  • 1





    But that is just a string constant containing "mypayload". It's not the actual payload you have stored in the variable called mypayload. That's why I'm confused.

    – garnaat
    Mar 16 '16 at 20:28











  • For clarification see above as stated in initial post "I've called it with numerous versions of code .... without any luck." What's there currently is the latest as all previous attempts failed.

    – JC23
    Mar 16 '16 at 22:24






  • 2





    Well, the example you show above would not appear to be correct. As I stated above, you are supposed to be passing a JSON payload but you are passing a string constant "mypayload". I would suggest fixing that issue and then updating the question with the new code and the exact response you are getting from the service.

    – garnaat
    Mar 17 '16 at 2:50
















What exactly is payload = b'mypayload' supposed to do here?

– garnaat
Mar 16 '16 at 17:32





What exactly is payload = b'mypayload' supposed to do here?

– garnaat
Mar 16 '16 at 17:32













update 'MyDevice' with a desired state, in this case set a switch to the 'on' position

– JC23
Mar 16 '16 at 20:04





update 'MyDevice' with a desired state, in this case set a switch to the 'on' position

– JC23
Mar 16 '16 at 20:04




1




1





But that is just a string constant containing "mypayload". It's not the actual payload you have stored in the variable called mypayload. That's why I'm confused.

– garnaat
Mar 16 '16 at 20:28





But that is just a string constant containing "mypayload". It's not the actual payload you have stored in the variable called mypayload. That's why I'm confused.

– garnaat
Mar 16 '16 at 20:28













For clarification see above as stated in initial post "I've called it with numerous versions of code .... without any luck." What's there currently is the latest as all previous attempts failed.

– JC23
Mar 16 '16 at 22:24





For clarification see above as stated in initial post "I've called it with numerous versions of code .... without any luck." What's there currently is the latest as all previous attempts failed.

– JC23
Mar 16 '16 at 22:24




2




2





Well, the example you show above would not appear to be correct. As I stated above, you are supposed to be passing a JSON payload but you are passing a string constant "mypayload". I would suggest fixing that issue and then updating the question with the new code and the exact response you are getting from the service.

– garnaat
Mar 17 '16 at 2:50





Well, the example you show above would not appear to be correct. As I stated above, you are supposed to be passing a JSON payload but you are passing a string constant "mypayload". I would suggest fixing that issue and then updating the question with the new code and the exact response you are getting from the service.

– garnaat
Mar 17 '16 at 2:50












2 Answers
2






active

oldest

votes


















1














This code is working fine for me:



def set_thing_state(thingName, state):
# Change topic, qos and payload
payload = json.dumps('state': 'desired': 'property': state )

logger.info("IOT update, thingName:"+thingName+", payload:"+payload)
#payload = 'state': 'desired': 'property': state


response = client.update_thing_shadow(
thingName = thingName,
payload = payload
)

logger.info("IOT response: " + str(response))
logger.info("Body:"+response['payload'].read())


def get_thing_state(thingName):

response = client.get_thing_shadow(thingName=thingName)

streamingBody = response["payload"]
jsonState = json.loads(streamingBody.read())

print jsonState
#print jsonState["state"]["reported"]


Good luck






share|improve this answer






























    0














    garnaat is right.
    Just replace payload = b'mypayload' with payload = mypayload and it should work.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36042567%2faws-lambda-function-cant-call-update-thing-shadow%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      This code is working fine for me:



      def set_thing_state(thingName, state):
      # Change topic, qos and payload
      payload = json.dumps('state': 'desired': 'property': state )

      logger.info("IOT update, thingName:"+thingName+", payload:"+payload)
      #payload = 'state': 'desired': 'property': state


      response = client.update_thing_shadow(
      thingName = thingName,
      payload = payload
      )

      logger.info("IOT response: " + str(response))
      logger.info("Body:"+response['payload'].read())


      def get_thing_state(thingName):

      response = client.get_thing_shadow(thingName=thingName)

      streamingBody = response["payload"]
      jsonState = json.loads(streamingBody.read())

      print jsonState
      #print jsonState["state"]["reported"]


      Good luck






      share|improve this answer



























        1














        This code is working fine for me:



        def set_thing_state(thingName, state):
        # Change topic, qos and payload
        payload = json.dumps('state': 'desired': 'property': state )

        logger.info("IOT update, thingName:"+thingName+", payload:"+payload)
        #payload = 'state': 'desired': 'property': state


        response = client.update_thing_shadow(
        thingName = thingName,
        payload = payload
        )

        logger.info("IOT response: " + str(response))
        logger.info("Body:"+response['payload'].read())


        def get_thing_state(thingName):

        response = client.get_thing_shadow(thingName=thingName)

        streamingBody = response["payload"]
        jsonState = json.loads(streamingBody.read())

        print jsonState
        #print jsonState["state"]["reported"]


        Good luck






        share|improve this answer

























          1












          1








          1







          This code is working fine for me:



          def set_thing_state(thingName, state):
          # Change topic, qos and payload
          payload = json.dumps('state': 'desired': 'property': state )

          logger.info("IOT update, thingName:"+thingName+", payload:"+payload)
          #payload = 'state': 'desired': 'property': state


          response = client.update_thing_shadow(
          thingName = thingName,
          payload = payload
          )

          logger.info("IOT response: " + str(response))
          logger.info("Body:"+response['payload'].read())


          def get_thing_state(thingName):

          response = client.get_thing_shadow(thingName=thingName)

          streamingBody = response["payload"]
          jsonState = json.loads(streamingBody.read())

          print jsonState
          #print jsonState["state"]["reported"]


          Good luck






          share|improve this answer













          This code is working fine for me:



          def set_thing_state(thingName, state):
          # Change topic, qos and payload
          payload = json.dumps('state': 'desired': 'property': state )

          logger.info("IOT update, thingName:"+thingName+", payload:"+payload)
          #payload = 'state': 'desired': 'property': state


          response = client.update_thing_shadow(
          thingName = thingName,
          payload = payload
          )

          logger.info("IOT response: " + str(response))
          logger.info("Body:"+response['payload'].read())


          def get_thing_state(thingName):

          response = client.get_thing_shadow(thingName=thingName)

          streamingBody = response["payload"]
          jsonState = json.loads(streamingBody.read())

          print jsonState
          #print jsonState["state"]["reported"]


          Good luck







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 21:51









          José IbañezJosé Ibañez

          42047




          42047























              0














              garnaat is right.
              Just replace payload = b'mypayload' with payload = mypayload and it should work.






              share|improve this answer



























                0














                garnaat is right.
                Just replace payload = b'mypayload' with payload = mypayload and it should work.






                share|improve this answer

























                  0












                  0








                  0







                  garnaat is right.
                  Just replace payload = b'mypayload' with payload = mypayload and it should work.






                  share|improve this answer













                  garnaat is right.
                  Just replace payload = b'mypayload' with payload = mypayload and it should work.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 13 '16 at 15:14









                  AbeAbe

                  11




                  11



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36042567%2faws-lambda-function-cant-call-update-thing-shadow%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      政党