首页 > 解决方案 > AWS Lambda 使用 boto3.client('iot-data') TIMEOUT as of 2021-01-26

问题描述

我有一个非常简单的 AWS Lambda 函数,它每隔几秒就会触发 3 个月,没有失败

import boto3

shadow_client = boto3.client('iot-data')
    def lambda_handler(event, context):
    response = shadow_client.list_named_shadows_for_thing(thingName='XXXXXX')
    ...

截至 2021-01-26T18:53:05.415+00:00 这已在 3000 毫秒后开始超时

我也突然收到 SSL 错误的通知。

boto3发布历史看,昨天(2021-01-26)是boto3 1.16.60发布的时候。也许无关。与此同时,有人在 2021 年 1 月 26 日报告 SSL 错误帖子。他们指出 AWS 可能会从一个 boto3 版本切换到另一个版本。

对任何 boto3.client('iot-data') 函数的任何调用都会在 3000 毫秒后超时:

boto3.client('iot-data').list_named_shadows_for_thing()
boto3.client('iot-data').get_thing_shadow()

谁能帮我:

  1. 修理它
  2. 避免对 boto3 的更改阻止我的代码在未来运行?

错误日志:

[错误] SSLError: SSL 验证失败 https://data.iot.eu-central-1.amazonaws.com/things/XXXX/shadow [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl. c:1091) 回溯(最近一次调用):文件“/var/task/lambda_function.py”,第 37 行,在 lambda_handler client_shadow = get_shadow(clientID)['state']['desired'] File "/var/ task/lambda_function.py”,第 94 行,在 get_shadow 响应 = iot_data_client.get_thing_shadow(thingName=thing_name) 文件“/var/runtime/botocore/client.py”,第 357 行,在 _api_call return self._make_api_call(operation_name, kwargs) _make_api_call 中的文件“/var/runtime/botocore/client.py”,第 663 行
operation_model, request_dict, request_context) 文件“/var/runtime/botocore/client.py”,第 682 行,在 _make_request
返回 self._endpoint.make_request(operation_model, request_dict)
文件“/var/runtime/botocore/endpoint.py”,第 102 行,在 make_request
返回 self._send_request(request_dict, operation_model) 文件“/var/runtime/botocore/endpoint.py”,第 137 行,在 _send_request success_response
,异常):文件“/var/runtime/botocore/endpoint.py ",第 256 行,在 _needs_retry
catch_exception=caught_exception, request_dict=request_dict) 文件“/var/runtime/botocore/hooks.py”,第 356 行,在发出返回 self._emitter.emit(aliased_event_name, **kwargs) 文件“/var/runtime/botocore/hooks .py”,第 228 行,在发射中返回 self._emit(event_name, kwargs) 文件“/var/runtime/botocore/hooks.py”,第 211 行,在 _emit response = handler(**kwargs) 文件“/var/ runtime/botocore/retryhandler.py”,第 183 行,调用中 if self._checker(attempts, response,catch_exception):文件“/var/runtime/botocore/retryhandler.py”,第 251 行,调用中 catch_exception) 文件“/ var/runtime/botocore/retryhandler.py",第 277 行,在 _should_retry
返回 self._checker(attempt_number,response,catch_exception)
文件“/var/runtime/botocore/retryhandler.py”,第 317 行,调用
中被捕获的 异常)文件“/var/runtime/botocore/retryhandler.py”,第 223 行,调用中的 尝试编号,捕获的异常)文件“/var/runtime /botocore/retryhandler.py”,第 359 行,在 _check_caught_exception 中提出了 catch_exception 文件“/var/runtime/botocore/endpoint.py”,第 200 行,在 _do_get_response http_response
= self._send(request) 文件“/var/runtime/botocore /endpoint.py”,第 269 行,在 _send 中返回 self.http_session.send(request) 文件“/var/runtime/botocore/httpsession.py”,第 281 行,在发送中引发 SSLError(endpoint_url=request.url,error= e) @timestamp 1611687531606

实际上超时是静默发生的,只有通过搜索超时才能从日志中看到

摄取
时间 1611687185615 日志 535942143265:/aws/lambda/XXXXXXXXXXX logStream
2021/01/26/[$LATEST]xxxxxxxxxxxxxxxxxxxxx 消息 2021-01-26T18:53:05.415Z 0b454f65-1366-4525-02788-940d 36 秒后请求 cId 超时 36 秒
0b454f65-1366-4525-8288-940d6f667e6c 时间戳
1611687185415

标签: boto3

解决方案


解决了。
根据亚马逊..

对于给您带来的不便,我们深表歉意。根本原因可能是较新版本的 boto3 导入了底层安全模块 certifi 的最新版本以进行证书验证。在 12 月的 certifi 版本中,他们删除了对为 boto3 使用的默认 iotdata 端点签署证书的 VeriSign 根 CA 的信任。修复方法是将 iotdata 端点设置为使用客户特定的 iot 核心 ATS URL。您可以通过在 CLI 中或从 IoT Core 控制台的设置菜单中调用“aws iot describe-endpoint --endpoint-type iot:data-ats”来找到它。我们正在努力在未来的 boto3 版本中解决这个问题。

解决方案在这里

这是修复的简单版本

#EDIT THIS LINE TO USE ENDPOINT URL
client = boto3.client('iot-data', region_name='eu-central-1', endpoint_url='https://xxxxxxxxxxxxxx-ats.iot.eu-central-1.amazonaws.com')

其中 xxxxxxxxxxxxxx-ats.iot.eu-central-1.amazonaws.com 是您来自 AWS的终端节点


推荐阅读