Linux/macOS 머신에서 배스천 호스트를 사용하여 Amazon RDS DB 인스턴스에 연결하려면 어떻게 해야 합니까?

3분 분량
0

공개적으로 액세스할 수 없는 Amazon Relational Database Service(Amazon RDS) DB 인스턴스가 있습니다. 내 Linux/macOS 컴퓨터에서 이 인스턴스에 연결하고 싶습니다. 배스천 호스트를 사용하여 RDS DB 인스턴스에 연결하려면 어떻게 해야 합니까?

간략한 설명

프라이빗 Amazon RDS 또는 Amazon Aurora DB 인스턴스에 연결하려면 VPN 또는 AWS Direct Connect를 사용하는 것이 모범 사례입니다. VPN 또는 AWS Direct Connect를 사용할 수 없는 경우에는 배스천 호스트 사용이 선호되는 옵션입니다. 이 방법을 사용하여 VPC 외부에서 Aurora 서버리스 및 RDS 프록시에 연결할 수도 있습니다. 이 예제는 RDS DB 인스턴스가 비공개인 경우에도 Linux/macOS 시스템에서 RDS DB 인스턴스에 연결할 수 있도록 배스션 호스트를 설정하는 방법을 보여 줍니다.

해결 방법

1.    DB 인스턴스를 수정하여 Amazon RDS DB 인스턴스를 비공개로 설정합니다. 프라이빗 서브넷을 포함하고 공개적으로 액세스 가능(publicly accessible) 파라미터를 **아니오(no)**로 설정합니다(즉, 라우팅 테이블에 **인터넷 게이트웨이 - igw(Internet gateway - igw)**가 없음). 포트(5432, 3306)가 모든 IP로부터 DB를 허용하도록 보안 그룹을 설정합니다.

2.    DB 인스턴스와 동일한 VPC에서 사용 가능한 가장 작은 EC2 인스턴스를 시작합니다. Amazon EC2(Amazon Elastic Compute Cloud) 인스턴스를 퍼블릭 서브넷을 통해 인터넷에서 액세스할 수 있도록 설정합니다(즉, 라우팅 테이블에 Internet gateway - igw가 있음). 연결하려는 Linux/macOS 시스템의 IP를 허용하도록 보안 그룹을 설정합니다.

3.    Linux/macOS 시스템에서 다음 명령을 실행하여 컴퓨터에서 연결할 터널을 만듭니다.

Syntax 1:
ssh -i <identity_file> -f -l <bastion-host-username> -L 
<local-port-you-connect-to>:<rds-endpoint>:<rds:listening-port>
<bastion-host-public-ip> -v

Example Command:
ssh -i "private_key.pem" -f -l ec2-user -L 5432:172.31.39.62:5432  3.133.141.189 -v

위의 명령(SSH 터널링)을 실행할 때 다음 설정을 구성합니다.

  • debug1: Local connections to LOCALHOST: 5432 forwarded to remote address 172.31.39.62:5432
  • debug1: Local forwarding listening on 127.0.0.1 port 5432.
  • debug1: channel 0: new [port listener]
  • debug1: Local forwarding listening on ::1 port 5432.
Syntax 2:
ssh -i "Private_key.pem" -f -N -L 5433:RDS_Instance_Endpoint:5432 ec2-user@EC2-Instance_Endpoint -v

Example Command:
ssh -i "private.pem" -f -N -L
5433:pg115.xxxx.us-east-2.rds.amazonaws.com:5432
ec2-user@ec2-xxxx-xxx9.us-east-2.compute.amazonaws.com -v

4.    이제 SSH 터널링이 설정되었으므로 로컬 Linux/macOS 시스템에서 DB 인스턴스에 연결할 수 있습니다. 다음 예제는 PostgreSQL에 연결하지만, 이 방식을 사용하여 MySQL 또는 원하는 다른 엔진에 연결할 수도 있습니다.

Syntax -

psql -hlocalhost -Upostgres -p<local-port-you-connect-to> -d postgres


-h = localhost

-U = the username present in the DB for connectivity

-p = 'local-port-you-connect-to' from the SSH Tunneling command

-d = Any DB, user wish to connect.

Example command -
a483e73d651f:.ssh rahul_saha$ psql -hlocalhost -Upostgres -p5432 -d postgres
Password for user postgres: 
psql (12.1, server 11.5)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=> \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 rdsadmin  | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | rdsadmin=CTc/rdsadmin
 template0 | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/rdsadmin          +
           |          |          |             |             | rdsadmin=CTc/rdsadmin
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

$ psql -hlocalhost -Upostgres -p -d postgres
$ mysql -h127.0.0.1 -uroot -p

참고: DB 인스턴스에 연결할 때 localhost 키워드를 사용하는 경우 MySQL은 소켓을 사용하여 연결을 시도합니다. MySQL DB 인스턴스에 액세스할 때는 호스트 이름 127.0.0.1을 사용해야 합니다. 자세한 내용은 MySQL 설명서의 Can't connect to [local] MySQL server를 참조하십시오.


관련 정보

AWS 기반 Linux 배스천 호스트

Amazon EC2 인스턴스를 배스천 호스트로 사용하여 로컬 시스템에서 프라이빗 Amazon RDS DB 인스턴스에 연결