AWS

[AWS] CloudFormation을 활용하여 퍼블릭, 프라이빗 인스턴스 구성 - 2 (프라이빗 인스턴스)

bbiyak2da 2023. 1. 10. 20:43

 

https://bbiyak-cloud.tistory.com/11

 

[AWS] CloudFormation을 활용하여 퍼블릭, 프라이빗 인스턴스 구성 - 1 (퍼블릭 인스턴스)

https://bbiyak-cloud.tistory.com/9 [AWS] 퍼블릭 서브넷 VPC 구성 (VPC, 서브넷, 라우팅 테이블, 인터넷 게이트웨이, 보안그룹 등) VPC (Virtual Private Cloud) - Virtual Private Cloud(VPC)는 사용자의 AWS 계정 전용 가상 네

bbiyak-cloud.tistory.com

이전 글과 이어집니다.

 

이전 글에서는 퍼블릭 인스턴스만 구성했다면,

이번 글에서는 약간의 코드를 추가하여 프라이빗 인스턴스도 구성해보자.


실습 아키텍처


1. 기존의 템플릿 파일에 신규 코드 추가 후, 스택 생성

 

[NAT 게이트웨이, EIP]

  NATGateWay:
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt EIP.AllocationId
      SubnetId: !Ref CloudNetaPublicSN
      Tags:
        - Key: Name
          Value: NAT-Gateway

  EIP:
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc
​

해당 코드를 추가해준다.

 

[NAT 게이트웨이 경로 추가]

  NATRoute:
    Type: AWS::EC2::Route
    DependsOn: CloudNetaIGWAttachment
    Properties:
      RouteTableId: !Ref CloudNetaPrivateRT
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NATGateWay

해당 코드를 추가해준다.

 

[최종 코드 완성본]

 

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

  NATGateWay: # NAT 게이트웨이
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt EIP.AllocationId
      SubnetId: !Ref CloudNetaPublicSN
      Tags:
        - Key: Name
          Value: NAT-Gateway
          
  NATRoute: # NAT 게이트웨이 경로 추가
    Type: AWS::EC2::Route
    DependsOn: CloudNetaIGWAttachment
    Properties:
      RouteTableId: !Ref CloudNetaPrivateRT
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NATGateWay
      
  EIP: # EIP 할당
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc
      
  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

visual studio code를 열어, 해당 코드를 입력 한 템플릿 파일을 만들어준다.

꼭! 파일 확장명은 .yml or .yaml 로 저장

스택 업데이트로 파일 교체

 

즉, 프라이빗에서 인터넷 접속을 하기 위해NAT 게이트웨이를 추가하였다.


2. 확인

패스워드를 이용하여 퍼블릭에서 -> 프라이빗으로 로그인해보자.

퍼블릭 인스턴스의 퍼블릭 ip 주소를 복사한다.

해당 ip로 SSH 연결 후

프라이빗 인스턴스의 프라이빗 ip 주소를 복사한다.

퍼블릭 인스턴스에서 

프라이빗 인스턴스의 프라이빗 ip 주소를 이용하여 접속해보자

 

ssh root@프라이빗 인스턴스의 프라이빗 ipv4 주소

템플릿 코드에 패스워드를 'qwe123'으로 지정하였으므로,

패스워드를 묻는 칸에 해당 비밀번호(qwe123)를 입력한다.

 

이후, 성공적으로 로그인 접속한 것을 확인할 수 있다.

추가적으로 웹 접속 확인 또한 진행하면 된다.