【问题标题】:botocore.exceptions.ConnectTimeoutError: Connect timeout on endpoint URLbotocore.exceptions.ConnectTimeoutError:端点 URL 上的连接超时
【发布时间】:2021-10-30 20:16:41
【问题描述】:

我正在使用 boto3 通过 Lambda Python 函数读取 S3 中的许多文本文件。

我在下面连接到 S3 的代码

config = Config(
            read_timeout=900,
            connect_timeout=900,
            retries={"max_attempts": 0}
        )
        session = boto3.Session(region_name='ap-southeast-2')

        s3 = session.resource('s3', config=config)
        bucket = s3.Bucket(bucket)
        for obj in bucket.objects.all():

大约30个文本文件成功ok。但之后,它得到以下错误消息

[ERROR] 2021-09-01T09:35:08.628Z    6a5bf649-2057-43e4-a3eb-1b4438e582fb    Exception on /process [POST]
Traceback (most recent call last):
  File "/opt/python/urllib3/connection.py", line 169, in _new_conn
    conn = connection.create_connection(
  File "/opt/python/urllib3/util/connection.py", line 96, in create_connection
    raise err
  File "/opt/python/urllib3/util/connection.py", line 86, in create_connection
    sock.connect(sa)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/var/runtime/botocore/httpsession.py", line 323, in send
    urllib_response = conn.urlopen(
  File "/var/task/serverless_sdk/vendor/wrapt/wrappers.py", line 602, in __call__
    return self._self_wrapper(self.__wrapped__, self._self_instance,
  File "/opt/python/urllib3/connection.py", line 353, in connect
    conn = self._new_conn()
  File "/opt/python/urllib3/connection.py", line 174, in _new_conn
    raise ConnectTimeoutError(
urllib3.exceptions.ConnectTimeoutError: (<botocore.awsrequest.AWSHTTPSConnection object at 0x7fe7b7a3e130>, 'Connection to xxxxxxxxxxxxxxxxxxxxxx.amazonaws.com timed out. (connect timeout=5)')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tmp/sls-py-req/flask/app.py", line 2070, in wsgi_app
    response = self.full_dispatch_request()
  File "/var/runtime/botocore/httpsession.py", line 356, in send raise ConnectTimeoutError(endpoint_url=request.url, error=e) botocore.exceptions.ConnectTimeoutError: Connect timeout on endpoint URL: "https://xxxxxxxxxxxxxxxxx.s3.ap-southeast-2.amazonaws.com/?encoding-type=url"

有什么办法可以解决这个问题?

【问题讨论】:

  • 你的函数在 vpc 中吗?
  • @Marcin 是的。这个问题重要吗?
  • 是的,因为 vpc 中的 lambda 没有互联网连接,除非您设置正确。它必须在vpc中吗?如果没有,请将其从 vpc 中删除,它应该可以工作。
  • @Marcin 我在 vpc 中添加 Lambda 的原因是,它必须连接到 Rds 并且 Rds 在 vpc 中。那么还有什么其他方法可以解决这个问题吗?

标签: python flask amazon-s3 aws-lambda boto3


【解决方案1】:

VPC 中的 Lambda 没有公共 IP,因此无法从公共子网访问互联网。因此,您可以在公共子网中创建 NAT 网关,并将您的 lambda 放在 私有子网 中。一旦您为私有子网中的任何0.0.0.0/0 连接设置路由表以进入 NAT,您的 lambda 将获得 Internet 访问权限:

相反,您可以通过 VPC interface endpoint 为 AWS Config 避免使用 NAT 和互联网。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 2019-03-07
    • 2021-06-01
    • 2014-03-08
    • 1970-01-01
    • 2021-02-23
    相关资源
    最近更新 更多