lanlin/notes

外部访问 AWS DocumentDB or How To Access AWS DocumentDB From Local

Opened this issue · 0 comments

lanlin commented

场景

AWS 的数据库及快速缓存服务等,都是只支持 VPC 内访问的。

可在同一个 VPC 的某个 EC2 示例上安装 nginx, 以 nginx 代理的形式实现外部对其的访问。

Nginx 配置 (以 DocumentDB 为例)

以下示例的配置将在 EC2 上开启 27017 及 28018 两个对外端口,

并将端口访问转发给对应的上游 AWS 服务

27017 <--> the.domain.to.aws.foo.database:27017
28018 <--> the.domain.to.aws.foo.database:28018

示例配置:

stream {
    server {
        listen  27017 so_keepalive=on;
        proxy_pass    stream_mongo_27017;
        proxy_timeout 5m;
        proxy_connect_timeout 5s;
    }

    server {
        listen  28018 so_keepalive=on;
        proxy_pass    stream_mongo_28018;
        proxy_timeout 5m;
        proxy_connect_timeout 5s;
    }

    upstream stream_mongo_27017 {
         server the.domain.to.aws.foo.database:27017;
    }

    upstream stream_mongo_28018 {
         server the.domain.to.aws.foo.database:28018;
    }
}

假设,该 EC2 的 公共IP 为 10.0.0.1
那么通过 10.0.0.1:27017 以及 10.0.0.1:28018 即可访问到对应的 DocumentDB 服务

补充说明

配置上述代理,需要满足以下几个条件:

  1. 安装 Nginx 的 EC2 和要被代理的 AWS 服务必须位于同一个 VPC 内

  2. 该 EC2 需要拥有一个公共IP

  3. 该 EC2 所在的子网路由需要拥有互联网网关

  4. 该 EC2 对应的端口 (例子中为 27017,28018)需要在安全组的入站及出站允许范围