【发布时间】:2022-01-25 12:05:09
【问题描述】:
背景:
我有一个自定义 VPC,其中有 2 个私有子网,其中包含一个位于 us-west-2 区域内的 Postgres RDS 实例和一个包含一个位于 us-west-2 区域内的 EC2 实例的公有子网。
私有子网 ACL:
- 允许端口 5432 上的所有入站 IPv4 流量
RDS 实例安全组:
- 允许端口 5432 上的所有入站 IPv4 流量
公共子网 ACL:
- 允许所有端口上的所有入站/出站流量
公共子网在其路由表中有一个互联网网关
EC2 实例安全组:
- 在端口 22 上允许来自本地 IP 的入站 SSH 流量
- 允许端口 5432、443 和 80 上的所有 IPv4 出站流量
在通过 SSH 连接到 EC2 实例后,我会导出与 RDS 实例的 Postgres 凭据关联的环境变量(例如 PGDATABASE=testdb、PGUSER=foo_user、PGHOST=identifier.cluster-foo.us-west-2.rds.amazonaws .com, PGPASSWORD=bar) 并使用 python 版本 3.7.10 运行以下 python 脚本:
import psycopg2
try:
conn = psycopg2.connect(connect_timeout=10)
cur = conn.cursor()
cur.execute("""SELECT now()""")
query_results = cur.fetchall()
print(query_results)
except Exception as e:
print("Database connection failed due to {}".format(e))
我收到以下超时错误:
Database connection failed due to connection to server at "foo-endpoint" (10.0.102.128), port 5432 failed: timeout expired
connection to server at "foo-endpoint (10.0.101.194), port 5432 failed: timeout expired
【问题讨论】:
-
为什么要修改 ACL?通常默认的就足够了。此外,您还没有为 ACL 提供出站规则。
-
@Marcin,是对的。默认的 NACL 足以建立连接,只需将它们重置为默认状态。
-
ACL 几乎肯定是问题所在。您还必须在 ACL 中打开临时端口。如果您不知道如何正确执行此操作,那么最好的解决方案是保留默认的 ACL 规则。
-
将 ACL 恢复为默认值并在 VPC 中启用 DNS 可以解决问题,感谢您的提示
标签: amazon-web-services amazon-ec2 amazon-rds amazon-vpc