0

I am using this guide on the AWS deeplens. I am experimenting with the code and at one point it stopped working for the second standalone lambda function. At some point, the code started getting the exception and I only recently noticed it.

Here is the code in question:

def sendMessageToIoTTopic(iotMessage):
    # this is a default topic if its not specified in the lambda envirionment
    topicName = "worker-safety"
    if "iot_topic" in os.environ:
        topicName = os.environ['iot_topic']
        
    print("Send message to topic: " + topicName + " before")
    iotClient = boto3.client('iot-data', region_name='us-east-1')
    response = iotClient.publish(
            topic=topicName,
            qos=1,
            payload=json.dumps(iotMessage)
        )
    print("Send message to topic: " + topicName + "after")
    print("using this topic **{}** response is {}".format(topicName, response))

The guide says to modify the default timeout from 1 second to 3 seconds. Here is the log for this code with a 3 second timeout.

2021-01-31T14:28:35.579-05:00   Before we send to iot MQTT?

2021-01-31T14:28:35.579-05:00   Send message to topic: worker-safety-demo-cloud before

2021-01-31T14:28:36.202-05:00   END RequestId: 89794691-a149-4480-b0bd-fd1a69522774

2021-01-31T14:28:36.202-05:00   REPORT RequestId: 89794691-a149-4480-b0bd-fd1a69522774 Duration: 3003.48 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 35 MB

2021-01-31T14:28:36.202-05:00   2021-01-31T19:28:36.201Z 89794691-a149-4480-b0bd-fd1a69522774 Task timed out after 3.00 seconds

2021-01-31T14:28:38.016-05:00   START RequestId: 71562d34-28d6-471a-8a43-b1e0bab1d621 Version: $LATEST

Adjusting the timeout for the lambda function from 3 seconds to 15 seconds allows us to see the timeout error in detail. It shows that it is an SSL error.

enter image description here

This is the resultant log with more info:

2021-01-31T14:36:08.775-05:00   Before we send to iot MQTT?

2021-01-31T14:36:08.775-05:00   Send message to topic: worker-safety-demo-cloud before

2021-01-31T14:36:17.295-05:00   [ERROR] SSLError: SSL validation failed for https://data.iot.us-east-1.amazonaws.com/topics/worker-safety-demo-cloud?qos=1 [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1091) Traceback (most recent call last):   File "/var/task/lambda_function.py", line 177, in lambda_handler     sendMessageToIoTTopic(iotMessage)   File "/var/task/lambda_function.py", line 119, in sendMessageToIoTTopic     payload=json.dumps(iotMessage)   File "/var/runtime/botocore/client.py", line 357, in _api_call     return self._make_api_call(operation_name, kwargs)   File "/var/runtime/botocore/client.py", line 663, in _make_api_call     operation_model, request_dict, request_context)   File "/var/runtime/botocore/client.py", line 682, in _make_request     return self._endpoint.make_request(operation_model, request_dict)   File "/var/runtime/botocore/endpoint.py", line 102, in make_request     return self._send_request(request_dict, operation_model)   File "/var/runtime/botocore/endpoint.py", line 137, in _send_request     success_response, exception):   File "/var/runtime/botocore/endpoint.py", line 256, in _needs_retry     caught_exception=caught_exception, request_dict=request_dict)   File "/var/runtime/botocore/hooks.py", line 356, in emit     return self._emitter.emit(aliased_event_name, **kwargs)   File "/var/runtime/botocore/hooks.py", line 228, in emit     return self._emit(event_name, kwargs)   File "/var/runtime/botocore/hooks.py", line 211, in _emit     response = handler(**kwargs)   File "/var/runtime/botocore/retryhandler.py", line 183, in __call__     if self._checker(attempts, response, caught_exception):   File "/var/runtime/botocore/retryhandler.py", line 251, in __call__     caught_exception)   File "/var/runtime/botocore/retryhandler.py", line 277, in _should_retry     return self._checker(attempt_number, response, caught_exception)   File "/var/runtime/botocore/retryhandler.py", line 317, in __call__     caught_exception)   File "/var/runtime/botocore/retryhandler.py", line 223, in __call__     attempt_number, caught_exception)   File "/var/runtime/botocore/retryhandler.py", line 359, in _check_caught_exception     raise caught_exception   File "/var/runtime/botocore/endpoint.py", line 200, in _do_get_response     http_response = self._send(request)   File "/var/runtime/botocore/endpoint.py", line 269, in _send     return self.http_session.send(request)   File "/var/runtime/botocore/httpsession.py", line 281, in send     raise SSLError(endpoint_url=request.url, error=e)

2021-01-31T14:36:17.296-05:00   END RequestId: 0da11ea2-c9d3-42ce-a8d5-cd3e709bf42e

The SSL error in detail is this:

[ERROR] SSLError: SSL validation failed for https://data.iot.us-east-1.amazonaws.com/topics/worker-safety-demo-cloud?qos=1 [SSL: CERTIFICATE_VERIFY_FAILED]
 certificate verify failed: unable to get local issuer certificate (_ssl.c:1091)Traceback (most recent call last):  
File "/var/task/lambda_function.py", line 177, in lambda_handler    sendMessageToIoTTopic(iotMessage)  
File "/var/task/lambda_function.py", line 119, in sendMessageToIoTTopic    payload=json.dumps(iotMessage)  
File "/var/runtime/botocore/client.py", line 357, in _api_call    return self._make_api_call(operation_name, kwargs)  
File "/var/runtime/botocore/client.py", line 663, in _make_api_call    operation_model, request_dict, request_context)  
File "/var/runtime/botocore/client.py", line 682, in _make_request    return self._endpoint.make_request(operation_model, request_dict)  
File "/var/runtime/botocore/endpoint.py", line 102, in make_request    return self._send_request(request_dict, operation_model)  
File "/var/runtime/botocore/endpoint.py", line 137, in _send_request    success_response, exception):  
File "/var/runtime/botocore/endpoint.py", line 256, in _needs_retry    caught_exception=caught_exception, request_dict=request_dict)  
File "/var/runtime/botocore/hooks.py", line 356, in emit    return self._emitter.emit(aliased_event_name, **kwargs)  
File "/var/runtime/botocore/hooks.py", line 228, in emit    return self._emit(event_name, kwargs)  
File "/var/runtime/botocore/hooks.py", line 211, in _emit    response = handler(**kwargs)  File "/var/runtime/botocore/retryhandler.py", line 183, in __call__    if self._checker(attempts, response, caught_exception):  
File "/var/runtime/botocore/retryhandler.py", line 251, in __call__    caught_exception)  File "/var/runtime/botocore/retryhandler.py", line 277, in _should_retry    return self._checker(attempt_number, response, caught_exception)  
File "/var/runtime/botocore/retryhandler.py", line 317, in __call__    caught_exception)  File "/var/runtime/botocore/retryhandler.py", line 223, in __call__    attempt_number, caught_exception)  
File "/var/runtime/botocore/retryhandler.py", line 359, in _check_caught_exception    raise caught_exception  
File "/var/runtime/botocore/endpoint.py", line 200, in _do_get_response    http_response = self._send(request)  
File "/var/runtime/botocore/endpoint.py", line 269, in _send    return self.http_session.send(request) 
 File "/var/runtime/botocore/httpsession.py", line 281, in send    raise SSLError(endpoint_url=request.url, error=e)
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
netskink
  • 4,033
  • 2
  • 34
  • 46
  • 1
    [“SSL: CERTIFICATE_VERIFY_FAILED” Error when publish MQTT, AWS IoT](https://stackoverflow.com/a/65329452/2246345). The library version to use as per the solution is `certifi==2020.11.8 ` – samtoddler Jan 31 '21 at 19:59
  • Hmm, how do I use this `certifi==2020.11.8` info? Do I include in a requirements.txt file? Currently, this code consists of a single .py file. – netskink Feb 01 '21 at 16:22
  • yeah, I am assuming you are packaging your dependencies with your code and uploading to lambda, in that case just add that to your `requiremetns.txt` and zip everything together and push it to lambda. – samtoddler Feb 01 '21 at 16:32
  • Hmm, I have a file in the same directory as the .py file (on lambda console) named requirements.txt with `certifi==2020.11.8` a newline and it still has an SSL error. – netskink Feb 01 '21 at 16:52
  • check this [Updating a function with additional dependencies](https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-dependencies) for packaging the dependencies. – samtoddler Feb 01 '21 at 17:15
  • ok. that will take a while. I'll try. many thanks. – netskink Feb 01 '21 at 17:25
  • Yikes, I believe I resolved this and sadly forgot to update the answer. My sincere apologies. – netskink Aug 07 '21 at 18:54

0 Answers0