Quay lại

Kết nối Local Code tới Database Private trên AWS qua Bastion Host (DocumentDB, Redis, RDS/Aurora) Chuyên mục Devops    2025-07-09    5 Lượt xem    5 Lượt thích    comment-3 Created with Sketch Beta. 0 Bình luận

Trong môi trường staging hoặc production, việc triển khai Database trong private subnet là điều cần thiết để tăng cường bảo mật. Tuy nhiên, điều này lại gây khó khăn cho developer khi cần test hoặc phát triển ứng dụng local kết nối đến các DB này.

✅ Giải pháp: SSH Tunnel qua Bastion Host

Bài viết này hướng dẫn cách thiết lập SSH Tunnel từ máy local tới các dịch vụ:

  • Amazon DocumentDB (MongoDB-compatible)

  • Amazon ElastiCache Redis

  • Amazon RDS PostgreSQL / Aurora PostgreSQL


🧱 Mô hình Kết nối

[ Developer Machine ]
        |
        |  SSH Tunnel (Port Forward)
        v
[ Bastion Host (Public EC2) ]
        |
        |  Internal VPC
        v
[ Private DB: DocumentDB / Redis / RDS / Aurora ]​

1️⃣ Chuẩn bị Trước

☑️ Thông tin cần thiết:

Tên Mô tả
Bastion IP Public IP của EC2 Bastion Host
SSH Key File .pem để SSH vào Bastion
DB Endpoint Tên host của database trong private subnet
Local Port Cổng mở trên máy local
Dev OS Linux/macOS/WSL (đã có lệnh ssh)

2️⃣ Cấu hình SSH Tunnel cho từng loại DB

Dùng lệnh ssh -L để tạo local port forwarding qua Bastion đến DB.


🔷 A. DocumentDB (MongoDB-compatible)

ssh -i ~/.ssh/your-key.pem -N -L 27017:your-docdb-endpoint:27017 ubuntu@your-bastion-ip​

🔌 Cách kết nối từ code/backend:

MONGO_URI=mongodb://your_user:your_password@localhost:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred​

📎 Tải file CA:

wget -O global-bundle.pem https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem​

💡 Lưu ý: Nếu mật khẩu MongoDB có ký tự đặc biệt (&, @...), hãy URL-encode chúng.


🔴 B. Redis (ElastiCache TLS)

ssh -i ~/.ssh/your-key.pem -N -L 6379:your-redis-endpoint:6379 ubuntu@your-bastion-ip​

🔌 Code connect Redis:

REDIS_URL=rediss://localhost:6379

Có password thì:

REDIS_URL=rediss://:your_password@localhost:6379​

📌 Redis trong ElastiCache thường bật TLS — hãy dùng rediss:// thay vì redis://.


🟣 C. RDS PostgreSQL / Aurora

ssh -i ~/.ssh/your-key.pem -N -L 5432:your-rds-endpoint:5432 ubuntu@your-bastion-ip​

🔌 Code (Node.js, Python, Django...):

DATABASE_URL=postgres://username:password@localhost:5432/dbname​

Hoặc dùng lệnh CLI:

PGPASSWORD=your_password psql -h localhost -U your_user -d your_db -p 5432​

3️⃣ Tự động hóa với Script

Tạo file start-db-tunnels.sh:

#!/bin/bash

KEY=~/.ssh/your-key.pem
BASTION=ubuntu@your-bastion-ip

# DocumentDB
ssh -f -i $KEY -N -L 27017:your-docdb-endpoint:27017 $BASTION

# Redis
ssh -f -i $KEY -N -L 6379:your-redis-endpoint:6379 $BASTION

# PostgreSQL
ssh -f -i $KEY -N -L 5432:your-rds-endpoint:5432 $BASTION

echo "✅ Tunnels active! Connect using localhost ports."​

Chạy:

chmod +x start-db-tunnels.sh
./start-db-tunnels.sh​

4️⃣ Kiểm tra tunnel hoạt động

lsof -i -P | grep LISTEN​

Bạn sẽ thấy:

ssh     12345 user   3u  IPv4  ...  TCP 127.0.0.1:27017 (LISTEN)​

5️⃣ Khi nào nên dùng SSH Tunnel?

Trường hợp Dùng SSH Tunnel?
Dev/test local, cần kết nối DB nội bộ ✅ Có
CI/CD pipeline ❌ Không nên (dùng VPC Endpoint hoặc Lambda)
Production apps ❌ Không — phải deploy vào VPC trực tiếp
DevOps debug nhanh ✅ Rất tiện

🧩 Tùy chọn nâng cao

  • Dùng VS Code Remote SSH để dev trực tiếp trong Bastion

  • Dùng AWS SSM Session Manager thay cho SSH (bảo mật hơn, không cần mở port 22)

  • Kết hợp với Docker Compose override để redirect DB host về localhost


✅ Kết luận

Kết nối qua SSH Tunnel là kỹ thuật cực kỳ hữu ích cho developer hoặc DevOps khi cần tương tác với các dịch vụ nội bộ AWS như DocumentDB, Redis, RDS, Aurora. Chỉ cần cấu hình đúng, bạn có thể chạy ứng dụng local như đang ở trong VPC.

Bình luận (0)

Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough

Bài viết liên quan

Learning English Everyday