#### 1.背景描述
考虑安全原因,多数托管服务只提供VPC 内网访问方式。而在开发调试过程中,某些情况下需要通过互联网连接到这些托管服务。首先推荐的方案是是 Client VPN、Site to Site VPN 等组网方式,将本地网络与云上 VPC 网络打通。 但由于条件限制组网方案可能不适用,本文介绍通过 SSH 隧道的方式来访问 VPC 内的资源。
#### 2.前置条件
* 一台 EC2,可被本地客户端通过互联网访问,同时能够通过内网访问到 VPC 内的资源(RDS、Elasticache Redis)
* 支持 SSH 隧道的客户端,例如
* RDS MySQL:DBeaver 等
* Elasticache Redis:Another Redis Desktop Manager等
#### 3. 图形客户端访问 VPC 内的RDS MySQL
1. 开启 DBeaver,菜单选择“新建数据库连接”

2.数据库类型选择 MySQL

3.进入 SSH 配置界面,填写 EC2 公网 IP、用户名以及密钥,点击“测试隧道配置”,确认隧道配置成功

4.进入数据库地址配置界面,填写 RDS 地址、用户名以及密码,点击“测试连接”确认连接成功,点击确认

5.确认可以正常访问RDS 数据库资源

#### 4.图形客户端访问 VPC 内的 Elasticache Redis
1.启动“Another Redis Desktop Manager”, 在菜单栏点击“New connection”,填入 Elasticache Redis 集群的访问地址。同时选择“SSH”隧道配置,填入 EC2 公网 IP、用户名以及密钥,然后点击右下角“Ok”创建 Redis 连接

2.确认连接成功

#### 5.代码访问 VPC 内的资源
对于客户端不支持 SSH 隧道或者需要通过程序连接 VPC 内的资源,可以使用命令行创建 SSH 隧道的方式实现,将连接到本机特定端口的流量通过EC2 转发至 VPC 内的资源,读者可自行搜索或者查询参考资料的内容。
#### 6.参考资料
1. https://www.ssh.com/academy/ssh/tunneling-example#local-forwarding
2. https://docs.aws.amazon.com/documentdb/latest/developerguide/connect-from-outside-a-vpc.html