如何使用SSH隧道通过EC2实例连接到RDS实例?

pra*_*432 15 postgresql amazon-web-services amazon-rds pgadmin

所以这对我来说真的很新鲜,所以如果这是一个愚蠢的问题,我很抱歉。

我有一个不可公开访问的 RDS 实例,并且位于其自己的私有 VPC 中。我有一个允许连接到 RDS 的 EC2 实例,但不允许其他任何东西连接到该实例。

我现在希望 PgAdmin 能够显示来自我的 RDS 实例的数据。

我完成了 PgAdmin 中的向导,输入了 EC2 实例的公共 IP 作为隧道主机,用户名是ec2-user,身份验证是通过身份文件进行的(使用我用来 ssh 进入实例的 pem 文件)。

但是,我仍然无法连接。在“高级”选项卡中,PGAdmin 要求提供主机地址,但当我输入 RDS 实例的端点时会发出抱怨。

我如何让我的本地 pgAdmin 现在访问我的数据库,该数据库不再可以通过公共互联网访问?

---忘记添加错误信息

Unable to connect to server:

Failed to create the SSH tunnel.
Error: Could not establish session to SSH gateway
Run Code Online (Sandbox Code Playgroud)

Joh*_*ein 25

To reproduce your situation using a SSH Tunnel in PgAdmin, I did the following:

  • Launched an Amazon RDS database with:
    • Public accessibility: No
    • Security Group: Permit inbound access on 5432 from 10.0.0.0/16 (the CIDR of the VPC)
  • Launched a publicly-accessible Amazon EC2 instance in the same VPC
  • Configured PgAdmin "SSH Tunnel" as:
    • Tunnel host: Used the DNS Name of my EC2 instance
    • Tunnel port: 22
    • Username: ec2-user
    • Authentication: Identity file
    • Identity file: Used the keypair I would use the SSH into the instance
  • Configured PgAdmin "Connection" as:
    • Host: Endpoint from Amazon RDS console
    • Port: 5432
    • Database, Username, Password as set when launching the Amazon RDS database

It connected successfully.

If it is hanging for you, check that the Security Group is allowing an incoming connection from the EC2 instance.

SSH 隧道

联系


Joh*_*ein 23

您似乎希望使用具有端口转发功能的 Amazon EC2 实例来访问同一 VPC 中的私有 Amazon RDS 实例。

以下是我配置此类连接的方法。

1. 确认 SSH 对 EC2 实例有效

首先,确认您可以通过 SSH 访问 EC2 实例。您将使用类似于以下的命令:

ssh -i key.pem ec2-user@IP-ADDRESS
Run Code Online (Sandbox Code Playgroud)

2.使用端口转发

如果以上方法有效,则修改 SSH 命令以使用端口转发:

ssh -i key.pem -L 5432:RDS-HOST-NAME:5432 ec2-user@IP-ADDRESS
Run Code Online (Sandbox Code Playgroud)

这将通过 SSH 将您自己计算机上的端口 5432 转发到 EC2 实例。然后,发送到的任何流量都localhost:5432将通过 SSH 连接转发。然后,EC2 实例会将流量发送到RDS-HOST-NAME:5432。(替换RDS-HOST-NAME为 RDS 数据库的 DNS 名称。)

3. 将 PgAdmin 指向连接

在 PgAdmin 中,将数据库引用为:localhost:5432

当然,您可以在端口转发连接中使用不同的端口号。如果将多个连接转发到不同的数据库,这可能很有用。然而,如果可能的话,我喜欢让它们保持不变。