用ssh tunnel的方式设置 AWS DocumentDB 公网访问
创始人
2024-12-28 12:38:22
0

AWS DocumentDB的设定是只允许VPC内进行访问的,同时官方文档给了步骤,通过ssh tunnel的方式,可以从公网,或者从VPC外的网络,对DocumentDB进行访问。

我阅读了AWS官方文档并测试了这个步骤,如下是详细的步骤说明:

1. 我们需要DocumentDB在同一个VPC内的,具有公网IP的EC2,来实现ssh tunnel的方式。我们要先确保在EC2上能够连结到DocumentDB ( DocumentDB的安全组也需要开放EC2的Private IP)

2. 下面是在本地创建ssh tunnel的测试步骤 :

2.1:

ssh -i "C:\Users\xxxxxx\xxxxxx.pem" -L 27017:docdb.cluster-xxxxx.docdb.cn-northwest-1.amazonaws.com.cn:27017 ec2-user@ -N

2.2

创建ssh tunnel以后,这个terminal的tab页面本身是没有反应的,就像是这个query hang住了一样,但是您可以在本地的这个terminal上,另起一个tab (我用的是mobaxterm工具)

2.3 我的本地无法安装MongoDB客户端,在这里我用telnet测试了一下27017端口,来验证上一步中的ssh tunnel是否测试成功:

 /home/mobaxterm  telnet localhost 27017 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Connection closed by foreign host.

2.4 这时候,我们断开了2.1时候创建的ssh tunnel

2.5 再次测试telnet,发现没有办法连接:

 /home/mobaxterm  telnet localhost 27017 Trying 127.0.0.1... telnet: Unable to connect to remote host: Connection refused

如上测试便证明,本地和DocumentDB通过ssh tunnel是可以连接的,您如果在本地可以安装mongo client, 那么也是可以用Mongo client连接到documentdb的。

3. 如果我们不用localhost来连接,而是通过本地的公网IP,让其他的机器通过这个tunnel来连接,那么测试步骤如下:

在下面的测试例子中,我们将DocumentDB所在VPC内的公网访问的EC2 跳板机标记为EC2 A,然后从EC2 B上去访问DocumentDB:EC2 B --> EC2 A --> DocumentDB

3.1 确保EC2 A能够连结DocumentDB,确保EC2 B可以ssh 到EC2 A上。

3.2  在EC2 B上,建立如下ssh tunnel:

[ec2-user@ip-EC2 B ~]$ ssh -i  -L 0.0.0.0:27017:shirandoc.cluster-xxxxxxx.docdb.cn-north-1.amazonaws.com.cn:27017 ec2-user@ -N

3.3. 确保 EC2 B的安全组,开放了允许自己的公网IP访问的Ingress Rule

3.4. 在EC2 B上测试如下:

[ec2-user@ip-xx ~]$ telnet 127.0.0.1 27017 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. ^] telnet> quit  [ec2-user@ip-xx ~]$ telnet  27017 Trying 69.230.254.208... Connected to 69.230.254.208. Escape character is '^]'. quit } ^]  [ec2-user@ip-10-0-0-60 ~]$ telnet localhost 27017 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. ^]

我们也在EC2 B上安装了mongo client, 测试用EC2 B的公网IP连结也是成功的:

[ec2-user@ip-xxx ~]$ mongo --host :27017 --username root --password xxxxx                       MongoDB shell version v4.0.28 connecting to: mongodb://:27017/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("4041eeb8-9bd4-4781-b20e-58f391f45e75") } MongoDB server version: 4.0.0  Warning: Non-Genuine MongoDB Detected  This server or service appears to be an emulation of MongoDB rather than an official MongoDB product.  Some documented MongoDB features may work differently, be entirely missing or incomplete, or have unexpected performance characteristics.  To learn more please visit: https://dochub.mongodb.org/core/non-genuine-mongodb-server-warning.  rs0:PRIMARY> ^C bye 

参考文档:
https://docs.amazonaws.cn/documentdb/latest/developerguide/connect-from-outside-a-vpc.html
 

相关内容

热门资讯

三分钟详情!德州透视插件(透视... 三分钟详情!德州透视插件(透视)本来真的有挂,解密教程(有挂功能)-哔哩哔哩德州透视插件辅助器中分为...
透视能赢!aapoker破解侠... 透视能赢!aapoker破解侠是真的吗(透视)果然是有挂,细节揭秘(有挂稳定)-哔哩哔哩1、很好的工...
透视软件!佛手在线大菠萝智能辅... 透视软件!佛手在线大菠萝智能辅助器,菠萝德州透视脚本,手册教程(有挂总结)-哔哩哔哩亲,关键说明,佛...
第四分钟了解!德普之星的辅助工... 第四分钟了解!德普之星的辅助工具介绍(透视)真是真的是有挂,攻略教程(有挂方法)-哔哩哔哩1、超多福...
透视最新!we-poker辅助... 透视最新!we-poker辅助软件教程(透视)一贯存在有挂,揭秘教程(有挂开发)-哔哩哔哩;1、完成...
透视模拟器!德普软件,来玩德州... 透视模拟器!德普软件,来玩德州破解器,要领教程(有挂辅助)-哔哩哔哩1、来玩德州破解器透视辅助简单,...
第9分钟透视!wepoker有... 第9分钟透视!wepoker有辅助器吗(透视)原来存在有挂,插件教程(讲解有挂)-哔哩哔哩wepok...
透视透视挂!we poker辅... 透视透视挂!we poker辅助器下载(透视)好像有挂,扑克教程(有挂模拟器)-哔哩哔哩1、起透看视...
透视脚本!pokemmo脚本手... 透视脚本!pokemmo脚本手机版,werplan外卦神器,攻略教程(有挂秘笈)-哔哩哔哩进入游戏-...
1分钟辅助!pokemmo脚本... 1分钟辅助!pokemmo脚本辅助下载(透视)一贯真的是有挂,解密教程(详细教程)-哔哩哔哩1)po...