AWS

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

bbiyak2da 2023. 1. 10. 20:22

 

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

 

[AWS] 퍼블릭 서브넷 VPC 구성 (VPC, 서브넷, 라우팅 테이블, 인터넷 게이트웨이, 보안그룹 등)

VPC (Virtual Private Cloud) - Virtual Private Cloud(VPC)는 사용자의 AWS 계정 전용 가상 네트워크이다. - 해당 IP 주소 범위를 설정하고, 서브넷을 만든 후 라우팅 테이블, 네트워크 게이트웨이, 네트워크 인터

bbiyak-cloud.tistory.com

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

 

[AWS] 프라이빗 서브넷 VPC 구성

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

bbiyak-cloud.tistory.com

이전 글에서는 퍼블릭/프라이빗 서브넷 VPC 구성 작업을 '콘솔'로 진행했다면

이번 글에서는 퍼블릭/프라이빗 서브넷 VPC 구성 작업을 '코드'를 이용하여 'CloudFormation'로 진행해본다.


실습 아키텍처


1. 템플릿 파일 생성 후 스택 생성

 

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

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

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

 

CloudFormation - 스택 생성

템플릿 파일을 올려준다.

 

인스턴스에서 Public, Private 인스턴스 둘 다 생성 되어있는지 확인


2. 퍼블릭 인스턴스 웹 접속

 

 

Xshell에서 퍼블릭 인스턴스 ip 주소를 넣은 후 연결한다.

 

sudo su - : root로 전환

yum -y install httpd : httpd 설치

vim /var/www/html/index.html : /var/www/html/index.html 편집기 오픈

이후 원하는 내용으로 html 코드 입력

systemctl restart httpd : httpd 재부팅

 

퍼블릭 ip 주소로 웹 접속이 잘 되면 실습 성공

(뭐가 그리 힘들었니 과거의 나야 ,, ㅎㅎ)