Could not connect to the endpoint URL (DynamoDB)
TL;DR — botocore 无法打开到它所解析出的 DynamoDB 端点的连接。几乎总是 --endpoint-url 错误、DynamoDB Local 未运行、一个错误/拼错的区域生成了一个不存在的主机名,或者没有网络路径(离线、VPC、安全组)。指向一个可达的端点,并确认有东西在监听。
含义
botocore.exceptions.EndpointConnectionError: Could not connect to the
endpoint URL: "http://localhost:8000/"
# what the engine actually returns, reproduced against Amazon DynamoDB (live service, us-east-1):
connect ECONNREFUSED 127.0.0.1:9boto3/botocore 从区域(或你显式指定的 endpoint_url)构建 DynamoDB 端点 URL,然后无法与之建立 TCP/HTTP 连接。与认证或验证错误不同,请求从未到达任何正在运行的服务。它既会针对 DynamoDB Local(模拟器已关闭/端口错误)出现,也会针对真实 AWS(区域主机名错误或网络被阻断)出现。
为什么会发生
- DynamoDB Local 未运行——你传入了
--endpoint-url http://localhost:8000,但模拟器没有在那个端口上运行(参见 connection refused)。 - 端点 URL 错误——拼写错误、端口错误,或者在本地模拟器提供
http服务处用了https。 - 区域错误——一个拼错的区域(
us-east-11)解析到一个不存在的主机名,因此无法建立连接。 - 没有网络路径——离线,或者 DynamoDB 流量被 VPC 端点、安全组或网络 ACL 阻断。
- 跨容器/跨主机——在 Docker 内部,
localhost是容器自身,而不是主机或 DynamoDB Local 服务。
如何修复
- 确认端点处有什么在监听:
curl http://localhost:8000 # DynamoDB Local answers with a small response - 修正端点 URL——正确的主机、端口(默认
8000)和协议(Local 用http)。 - 检查真实 AWS 的区域拼写,并确保你有一条网络路由(VPC 端点/安全组规则允许 DynamoDB)。
- 在 Docker 中,使用 compose 服务名或
host.docker.internal而不是localhost(host.docker.internal在 Docker Desktop 中自动解析;在 Linux Docker Engine 上添加--add-host host.docker.internal:host-gateway)。 - 在与真实 AWS 对话? 完全去掉
--endpoint-url覆盖,让 botocore 构建正确的区域端点。
将 DynoTable 指向 Local
DynoTable 验证每个配置文件上的 Test Connection 的连接
Settings → Profiles (Connect an AWS account)。对于
DynamoDB本地,设置端点http://localhost:8000(或:4566对于LocalStack)使用占位符凭据 — CLI 需要的值相同。按⌘P
当你的应用程序使用与 DynoTable 不同的端口时切换配置文件。指南:
Connect to DynamoDB Local & LocalStack。与真实的 AWS 相比,区域字符串中的拼写错误会生成不正确的主机名解析 — 与无法访问的本地端口相同的 EndpointConnectionError 形状。调试前在配置文件上按aws sts get-caller-identity确认
DynamoDB特定设置。
相关错误
- Could not connect to DynamoDB Local (ECONNREFUSED)——被拒绝的本地连接的 Node/JS 等价情况。
- 配置中缺少 region——没有区域,因此 SDK 根本无法构建端点。
- 学习:Running DynamoDB Local · Connecting to DynamoDB Local / LocalStack
来源
- Deploying DynamoDB locally on your computer — AWS DynamoDB Developer Guide(默认端口 8000,虚拟凭据,
--endpoint-url) - DynamoDB local usage notes — AWS DynamoDB Developer Guide(设置本地端点;每个 CLI 命令都需要
--endpoint-url) - Error handling — Boto3 Developer Guide(
EndpointConnectionError作为客户端 botocore 异常) - ⟦3⟧ — Docker CLI reference(
host.docker.internal/host-gateway用于容器到主机的连接)