[AWS] 게이트웨이 엔드포인트
엔드포인트란?
1. 엔드포인트 : AWS 퍼블릭 서비스로 프라이빗 연결을 제공하는 기능
ㄴ게이트웨이 엔드포인트 : AWS 퍼블릭 서비스 중 S3, DynamoDB에 대한 연결
ㄴ인터페이스 엔드포인트 : 위 대상 외에 나머지 AWS 퍼블릭 서비스에 대한 연결
2. 엔드포인트 서비스 : 사용자가 지정한 서비스 대상에 대한 프라이빗 연결
실제 EC2 인스턴스에서 서울 리전에 존재하는 S3 서비스의 DNS 주소로 접근시,
퍼블릭 인스턴스는 인터넷 구간을 통해 통신하여 접근할 수 있으나
프라이빗 인스턴스는 통신할 수 없다.
따라서, 'VPC 엔드포인트' 기능을 활용하여
모든 네트워크가 S3과 통신이 가능하고, 외부 인터넷이 아닌 AWS 내부 네트워크를 통해 통신해보자
1. CloudFormation으로 기본 환경 구성
[VPC 엔드포인트 코드]
이전 글에서 사용했던 코드에
Endpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
RouteTableIds:
- !Ref CloudNetaPublicRT
- !Ref CloudNetaPrivateRT
VpcId: !Ref CloudNetaVPC
ServiceName: !Sub 'com.amazonaws.${AWS::Region}.s3'
해당 코드를 추가해준다.
[최종 코드 완성본]
Parameters:
KeyName:
Description: Name do an esisting RC2 KeyPair to enable SSH access to the instances. Linked to AWS Parameters
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
Resources:
CloudNetaVPC:# VPC
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
Tags:
- Key: Name
Value: CloudNeta-VPC
CloudNetaPublicSN:# 퍼블릭 서브넷
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref CloudNetaVPC
AvailabilityZone: !Select [ 0, !GetAZs '' ]
CidrBlock: 10.0.0.0/24
Tags:
- Key: Name
Value: CloudNeta-Public-SN
CloudNetaPrivateSN: # 프라이빗 서브넷
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref CloudNetaVPC
AvailabilityZone: !Select [ 2, !GetAZs '' ]
CidrBlock: 10.0.1.0/24
Tags:
- Key: Name
Value: CloudNeta-Private-SN
CloudNetaIGW: # 인터넷 게이트웨이
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: CloudNeta-IGW
CloudNetaIGWAttachment:# IGW-VPC 연결
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref CloudNetaIGW
VpcId: !Ref CloudNetaVPC
CloudNetaPublicRT: # 퍼블릭 라우팅 테이블
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref CloudNetaVPC
Tags:
- Key: Name
Value: CloudNeta-Public-RT
DefaultPublicRoute: # 퍼블릭 라우팅 테이블에 경로추가
Type: AWS::EC2::Route
DependsOn: CloudNetaIGWAttachment
Properties:
RouteTableId: !Ref CloudNetaPublicRT
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref CloudNetaIGW
CloudNetaPrivateRT: # 프라이빗 라우팅 테이블
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref CloudNetaVPC
Tags:
- Key: Name
Value: CloudNeta-Public-RT
CloudNetaPublicSNRouteTableAssociation: # 퍼블릭 라우팅 테이블 - 퍼블릭 서브넷 연결
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref CloudNetaPublicRT
SubnetId: !Ref CloudNetaPublicSN
CloudNetaPrivateSNRouteTableAssociation: # 프라이빗 라우팅 테이블 - 프라이빗 서브넷 연결
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref CloudNetaPrivateRT
SubnetId: !Ref CloudNetaPrivateSN
CloudNetaSecurityGroup: # 보안그룹
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable HTTP access via port 80 and SSH access via port 22
VpcId: !Ref CloudNetaVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
- IpProtocol: icmp
FromPort: -1
ToPort: -1
CidrIp: 0.0.0.0/0
CloudNetaPublicEC2: # 퍼블릭 EC2 인스턴스
Type: AWS::EC2::Instance
Properties:
ImageId: 본인 ami ID
InstanceType: t2.micro
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: CloudNeta-Public-EC2
NetworkInterfaces:
- DeviceIndex: 0
SubnetId: !Ref CloudNetaPublicSN
GroupSet:
- !Ref CloudNetaSecurityGroup
AssociatePublicIpAddress: true
CloudNetaPrivateEC2: # 프라이빗 EC2 인스턴스
Type: AWS::EC2::Instance
Properties:
ImageId: 본인 ami ID
InstanceType: t2.micro
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: CloudNeta-Private-EC2
NetworkInterfaces:
- DeviceIndex: 0
SubnetId: !Ref CloudNetaPrivateSN
GroupSet:
- !Ref CloudNetaSecurityGroup
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
(
echo "qwe123"
echo "qwe123"
) | passwd --stdin root
sed -i "s/^PasswordAuthentication no/PasswordAuthentication yes/g" /etc/ssh/sshd_config
sed -i "s/^#PermitRootLogin yes/PermitRootLogin yes/g" /etc/ssh/sshd_config
service sshd restart
Endpoint: # VPC 엔드포인트
Type: AWS::EC2::VPCEndpoint
Properties:
RouteTableIds:
- !Ref CloudNetaPublicRT
- !Ref CloudNetaPrivateRT
VpcId: !Ref CloudNetaVPC
ServiceName: !Sub 'com.amazonaws.${AWS::Region}.s3'
파일 저장후
스택 생성
2. 게이트웨이 엔드포인트 생성
VPC - 엔드포인트 - 엔드포인트 생성 클릭
엔드포인트 이름 : 원하는 엔드포인트 이름
서비스 범주 : AWS 서비스
서비스 : com.amazonaws.ap-northeast-2.s3 / 유형 : gateway
VPC : CloudFormation으로 생성된 VPC
라우팅테이블 : 클라우드포메이션으로 생성된 퍼블릭, 프라이빗 라우팅 테이블 지정
생성 완료
[확인]
라우팅 테이블을 확인해보면
퍼블릭 라우팅 테이블, 프라이빗 라우팅 테이블 둘 다 S3에 대한 경로가 설정되어 있는 것을 알 수 있다.
3. 게이트웨이 엔드포인트 검증
S3 서비스에 연결하기 위한 게이트웨이 엔드포인트를 생성하였다.
실제 EC2 인스턴스에 접속하여 S3와 통신 되는지 확인해보자
인스턴스를 Xshell을 통해 SSH 연결해주자.
Xshell에서 총 2개의 세션이 필요하다.
1. 퍼블릭 인스턴스 연결 (퍼블릭)
2. 퍼블릭 인스턴스 연결 후, 프라이빗으로 접속 (프라이빗)
[#1. 퍼블릭 EC2 인스턴스에서 TEST]
ping s3.ap-northeast-2.amazonaws.com : s3 서비스로 접근 되는지 ping 테스트
퍼블릭 인스턴스에서는 s3 서비스 접근 성공
[#2. 프라이빗 EC2 인스턴스에서 TEST]
ssh root@프라이빗 EC2 퍼블릭 ip : 퍼블릭 EC2 인스턴스 -> 프라이빗 EC2 인스턴스 접근
ping s3.ap-northeast-2.amazonaws.com : s3 서비스로 접근 되는지 ping 테스트
프라이빗 인스턴스에서도 s3 서비스 접근 성공
두 인스턴스 모두 서울 리전에 존재하는 S3 서비스의 DNS 주소로 통신이 가능하다.
4. 결론
- 퍼블릭, 프라이빗 EC2 인스턴스에서 S3과 통신하기 위해 데이터를 가상 라우터로 전달
- 가상 라우터는 각 라우팅 테이블을 참고하여, 게이트웨이 엔드포인트로 향하는 라우팅 경로를 확인하여 전달
- 게이트웨이 엔드포인트를 통해 S3로 데이터가 전달, 정상적인 통신 가능