Redshift Data API

GREEN FIELD·2023년 1월 18일
0

아주 편한 기능을 알게 되어 소개합니다.

사내에서 FinOps DW용으로 Redshift Serverless를 사용하고 있다. Lambda를 이용해서 데이터 수집부를 구축하면서 Redshift Data API라는 걸 알게 되었다.

Redshift Data API를 사용하면 따로 코드에서 커넥션 관리를 해 줄 필요가 없다!.
Redshift Cluster type은 따로 secret 설정을 해줘야 하지만, serverless는 role 권한 승인만으로 커넥션을 해줄 수 있어서 연결 과정이 참 간편하다.

Redshift Data API란

built-in Redshift Data API를 사용하면 web services-based application (Lambda, SageMaker notebooks and cloud9)에서 Redshift로 액세스할 수 있다.

앞서 말한 것처럼 별도의 커넥션 관리가 필요없으며, string 타입 변수로 SQL statement를 적어 param으로 넣어주면 비동기로 실행된다.

커넥션이 참 편한데

  • cluster는 secret manager 또는 temporary credentials에 넣고 필요할 때 알아서 꺼내서 사용
  • serverless는 Redshift에서 IAM Role Grant를 해주는 것으로 커넥션 완료! (단 db name, workgroup name 정도는 추가해줘야 함)

how to work with this

Redshift Cluster에서의 적용 방법은 AWS doc에게 양보하기로 하고... (또 하나 참고할만한 링크 추가)

https://docs.aws.amazon.com/redshift/latest/mgmt/data-api.html#data-api-calling-considerations-authentication
https://hevodata.com/learn/redshift-data-api/#Authorizing-Access-to-an-Amazon-Redshift-Data-API
Redshift Data API의 maximum duration 등 calling the Data API guidance도 제공하니 serverless 유저도 읽어보면 좋을 듯하다.

현재 사용중인 서버리스를 기준으로 설명하자면 사실 딱히 할 게 없다. 그만큼 간편한데,

먼저 Redshift Data API를 사용할 서비스에서 사용할 role에, 'AmazonRedshiftDataFullAccess'를 policy를 추가해주면 된다.

두번째, Redshift에서 해당 role이 접근할 수 있도록 권한 추가를 해주면 된다.

GRANT ALL PRIVILEGES ON SCHEMA data_source TO "IAMR:<role name>";

세번째, 쿼리를 날릴 때 sql문과 함께 db name, workgroup name을 함께 넣어주면 된다.

response = client.execute_statement(
        Database=<db name>,
        WorkgroupName=<workgroup name>,
        Sql=<sql>
    )

끝~

0개의 댓글