본문 바로가기

AWS

Private EC2 경유해서 Session Manager 이용해 RDS 접속하기

728x90

조금 특이한 아키텍처 구조

Bastion host의 22번 포트를 보안 때문에 열고 싶지 않아 Session Manager 이용해서 들어가고 싶은데

RDS까지 접근을 해야했다.

 

아키텍처에는 Bastion host라고 했지만 Private EC2면 된다

구조는 Developer -> System Manager(Session Manager) -> Private EC2 -> RDS이다

이렇게 하는 이유는 RDS를 외부에서 접근하기 위해서는 반드시 Bastion host 처럼 경유할 수 있는 무언가가 필요하다.

Session manager에서 직접 접근이 불가능하기 때문이다.

 

 

저번에는 vpc endpoint 이용해서 접속했지만, 이번에는 NAT Gateway 이용해서 public 망을 통해 접근하는 구조이다.

필요한 것은 VPC에 Nat gateway, 적절한 권한이 있는 IAM Role을 단 EC2이다. (별거없음)

 

 

IAM Role에 필요한 정책은 AmazonSSMManagedInstanceCore이다.

사실 이거 하나만 있어도 된다고는 하는데 session manager 버그인지 Credential 오류가 있어서 그냥 ssm 관련된 건 다 넣었다.

만약 session manager credential error가 있다면 ec2 인스턴스를 재부팅하거나, 중지->시작 해보자

 

 

EC2 보안그룹은 아주 심플하다.

그냥 아웃바운드 규칙에서 HTTPS만 열려있으면 된다.

 

 

RDS는 평소에 만들 듯이 Private Subnet에 만들었으며, t4g.micro, PostgreSQL(가장 최신 버전), Single AZ로 구성했다.

EC2는 system manager agent를 별도로 설치하기 귀찮아서 Amazon Linux 2023, t4g.micro로 Private Subnet에 구성했다.

 

 

Session Manager는 꼭 AWS 콘솔에 들어가지 않아도 로컬 PC에서 AWS CLI를 통해 들어갈 수 있다.

나는 Assume Role 기반으로 --profile을 붙여 접속했다.

필요한 건 AWS CLI만 설치해있으면 된다.

https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

 

Installing or updating to the latest version of the AWS CLI - AWS Command Line Interface

When updating from a previous version, the unzip command prompts to overwrite existing files. To skip these prompts, such as with script automation, use the -u update flag for unzip. This flag automatically updates existing files and creates new ones as ne

docs.aws.amazon.com

 

 

 

로컬에서 session manager 접속 방법

aws ssm start-session --target <EC2 인스턴스 ID> --region <EC2 리전>

 

 

그러면 Bastion host에 접속이 된 것이고, 여기서 RDS에 접속하기만 하면 된다.

 

 

그냥 private ec2에서 rds 접속하는 것과 다름없다.

session manager가 엉망이여서 시간이 좀 걸렸지, 그 외에는 똑같은 구조임.

728x90