본문 바로가기

클라우드 공부

CloudFormation으로 VPC 생성

CloudFormation으로 VPC 생성

클라우드에 대해 공부해보고자 인프런의 "AWS 클라우드 서비스 인프라 구축 이해와 해킹, 보안" 강의를 수강하면서 필기한 내용입니다.

생성 전에 vpc.yaml 파일을 다운 받는다.

vpc.yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: VPC Configuration
Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: myVPC
  
  PublicSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      CidrBlock: 10.0.0.0/24
      AvailabilityZone: "ap-northeast-2a"
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: public subnet
  PrivateSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      CidrBlock: 10.0.10.0/24
      AvailabilityZone: "ap-northeast-2c"
      Tags:
        - Key: Name
          Value: private subnet
  
  IGW:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: myigw
  Attachigw:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      InternetGatewayId: !Ref IGW
      VpcId: !Ref VPC
  
  EIP:
    Type: AWS::EC2::EIP
  NAT:
    Type: AWS::EC2::NatGateway
    DependsOn: Attachigw
    Properties:
      AllocationId: !GetAtt EIP.AllocationId
      SubnetId: !Ref PublicSubnet
  
  PublicRT:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: Public RT
  PublicRoute:
    Type: AWS::EC2::Route
    DependsOn: Attachigw
    Properties:
      RouteTableId: !Ref PublicRT
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref IGW
  PublicSubnetRTAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PublicRT
      SubnetId: !Ref PublicSubnet
  
  PrivateRT:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: Private RT
  PrivateRoute:
    Type: AWS::EC2::Route
    DependsOn: Attachigw
    Properties:
      RouteTableId: !Ref PrivateRT
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NAT
  PrivateSubnetRTAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PrivateRT
      SubnetId: !Ref PrivateSubnet

구성하게 된다면 아래와 같은 구조가 될 것이다.

생성을 위해 이전 실습과 같이 스택을 생성한다.

스택 생성 확인

이후 VPC를 들어가 확인해 보면 코드의 내용대로 완벽하게 생성된 것을 확인할 수 있다. (VPC, 서브넷, 탄력적 IP등등)

인스턴스 연결

인스턴스 생성

연결 성공 확인

이 역시 사용하지 않는다면 반드시 잘 삭제해야 합니다 과금이 나올 수 있으니 삭제하고 세부 내역 잘 확인하시길…