Node.js에서 RedShift 연결하는 법

이민석's avatar
Sep 11, 2024
Node.js에서 RedShift 연결하는 법

개요

Node.js에서 RedShift를 연결하는 방법을 소개합니다.

  1. Amazon RedShift란?

  2. Node.js에서 RedShift 연결하기

  3. Private Subnet의 RedShift에 연결하기

Amazon RedShift란?

Amazon RedShift는 PostgreSQL을 확장해서 만들어진 데이터베이스입니다. — [Ref]

RDS for PostgreSQL에 접속 가능한 SDK라면 특별한 차이점이 없는 한
Amazon RedShift에도 접속이 가능할 것입니다.

Node.js에서 RedShift 연결하기

Connect RedShift in Node.js를 검색하면 node-redshift 라이브러리를 추천합니다. — [Ref 1], [Ref 2]

하지만 해당 라이브러리는 7년 전 이후로 업데이트 되지 않고 있습니다.
또한 dmanjunath/node-redshift 내부 코드를 보면 pg를 쓰고 있습니다. — [Ref]

여기서 pg는 PostgreSQL에 접속이 가능한 Node.js SDK 중 하나입니다. — [Ref]

아래의 코드를 실행하면 성공적인 연결이 되는 것을 볼 수 있습니다.

import pg from 'pg;

async function connectPgClient() {

   const pgClient = new pg.Client({
      host: "RedShift Endpoint"
      port: 5439 // RedShift Default Port
      user: "RedShift Username"
      password: "RedShift Password"
      database: "RedShift Table"
      ssl: true
   });

   await pgClient.connect();
}

Private Subnet의 RedShift에 연결하기

일반적으로 프로덕션에서는 Private Subnet*에 DB Cluster를 배포합니다.

Private Subnet*은 인터넷을 통한 퍼블릭 엑세스가 되지 않는 서브넷을 의미합니다.

따라서 Public Subnet*에 배포된 Bastion Host를 통해서 RedShift Cluster에 접근하여야 합니다. — [Ref 1], [Ref 2], [Ref 3]

Bastion Host*는 퍼블릭 엑세스가 되는 서브넷에 배포된 EC2 Instance를 “경유”하여 Private Subnet의 대상에게 가기 위해서 사용하는 것을 의미합니다.

RedShift Cluster Endpoint를 /etc/hosts에 등록해야합니다.

sudo vi /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost

127.0.0.1 <RedShift Endpoint*>

이후 터미널을 열어서 ssh -L을 실행 후, 해당 창을 유지해야합니다.

ssh -L <Local Port*>:<RedShift Endpoint*>:<RedShift Port*> -i <Bastion Host PEM> <Bastion Host User*>@<Bastion Host Public IPv4*>

Local Port* 또한 되도록이면 5439 포트를 유지하도록 합시다.

ssh -i 5439:sample-redshift-cluster.abcdefghijkl.ap-northeast-2.redshift.amazonaws.com:5439 -i example.pem ec2-user@1.1.1.1

단 부득이하게 5439가 아닌 포트, 예를 들어 6430으로 변경했다면 어플리케이션 코드를 일부 변경해야 합니다.

import pg from 'pg;

async function connectPgClient() {

   const pgClient = new pg.Client({
      host: "RedShift Endpoint"
      port: 6430 // RedShift Default Port
      user: "RedShift Username"
      password: "RedShift Password"
      database: "RedShift Table"
      ssl: true
   });

   await pgClient.connect();
}
Share article
RSSPowered by inblog