웹사이트 검색

Cloudformation을 사용하여 AWS에서 VPC를 생성하는 방법


이 페이지에서

  1. 전제 조건
  2. 무엇을 할 것인가?\n
  3. 템플릿 만들기\n
  4. Cloudformation 스택 생성
  5. 결론

Cloudformation을 사용하면 AWS 리소스를 매우 쉽게 생성하고 관리할 수 있습니다. Cloudformation은 텍스트 파일을 사용하여 모든 AWS 리소스를 관리하는 데 사용할 수 있습니다. Cloudformation을 사용하면 수동으로 작업을 수행하지 않고도 인프라와 애플리케이션을 만들고 모델링할 수 있습니다. Cloudformation은 텍스트 파일 또는 템플릿에서 전체 인프라를 관리하는 데 도움이 됩니다. Cloudformation 템플릿은 AWS 인프라를 설명하는 JSON 또는 YAML 언어 형식의 텍스트 파일입니다.

이 기사에서는 2개의 퍼블릭 서브넷과 2개의 프라이빗 서브넷이 있는 VPC를 생성하는 Cloudformation을 볼 것입니다.

전제 조건

  1. AWS 계정(계정이 없는 경우 생성).
  2. Cloudformation 템플릿에 대한 기본적인 이해.\n

우리는 무엇을 할 것입니까?

  1. AWS에 로그인합니다.\n
  2. 템플릿을 만듭니다.\n
  3. Cloudformation 스택 생성

AWS에 로그인

  1. AWS 로그인 페이지로 이동하려면 여기를 클릭하십시오.\n

위의 링크를 누르면 로그인 세부 정보를 사용하여 로그인해야 하는 다음과 같은 웹 페이지가 표시됩니다.

AWS에 성공적으로 로그인하면 다음과 같이 모든 서비스가 나열된 기본 콘솔이 표시됩니다.

템플릿 만들기

스택 생성을 진행하기 전에 VPC를 생성하는 데 사용할 템플릿이 있어야 합니다. 다음 코드를 복사하고 로컬 컴퓨터에 저장합니다.

---
AWSTemplateFormatVersion: 2010-09-09
Description: >
  This Templates creates a VPC with 3 public and 3 private subnets.
Parameters:
  VpcCIDR:
    Type: String
    Description: VPC CIDR (Do Not Change if no customization is required). 
    Default: 10.10.0.0/16
  PrivateAZ1SubnetCIDR:
    Type: String
    Description: Subnet CIDR for 1st Availability Zone (Do Not Change if no customization is required).
    Default: 10.10.80.0/21
  PrivateAZ2SubnetCIDR:
    Type: String
    Description: Subnet CIDR for 2nd Availability Zone (Do Not Change if no customization is required).
    Default: 10.10.88.0/21
  PrivateAZ3SubnetCIDR:
    Type: String
    Description: Subnet CIDR for 3rd Availability Zone (Do Not Change if no customization is required).
    Default: 10.10.96.0/21
  PublicAZ1SubnetCIDR:
    Type: String
    Description: Subnet CIDR for 1st Availability Zone (Do Not Change if no customization is required).
    Default: 10.10.0.0/21
  PublicAZ2SubnetCIDR:
    Type: String
    Description: Subnet CIDR for 2nd Availability Zone (Do Not Change if no customization is required).
    Default: 10.10.8.0/21
  PublicAZ3SubnetCIDR:
    Type: String
    Description: Subnet CIDR for 3rd Availability Zone (Do Not Change if no customization is required). 
    Default: 10.10.16.0/21
Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default: VPC
        Parameters:
          - VpcCIDR
      - Label:
          default: Availabilty Zone 1
        Parameters:
          - PublicAZ1SubnetCIDR
          - PrivateAZ1SubnetCIDR
      - Label:
          default: Availabilty Zone 1
        Parameters:
          - PublicAZ2SubnetCIDR
          - PrivateAZ2SubnetCIDR
      - Label:
          default: Availabilty Zone 1
        Parameters:
          - PublicAZ3SubnetCIDR
          - PrivateAZ3SubnetCIDR
Resources:
  Vpc:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VpcCIDR
      Tags:
        - Key: Name
          Value: !Sub ${AWS::StackName}
  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Sub ${AWS::StackName}
  VPCGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      InternetGatewayId: !Ref InternetGateway
      VpcId: !Ref Vpc
  # Public Subnets - Route Table
  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref Vpc
      Tags:
        - Key: Name
          Value: !Sub ${AWS::StackName}-public
        - Key: Type
          Value: public
  PublicSubnetsRoute:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway
    DependsOn: VPCGatewayAttachment
  # Public Subnets
  PublicAZ1Subnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref Vpc
      CidrBlock: !Ref PublicAZ1SubnetCIDR
      AvailabilityZone: !Select [0, !GetAZs ""]
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub
            - ${AWS::StackName}-public-${AZ}
            - { AZ: !Select [0, !GetAZs ""] }
        - Key: Type
          Value: public
  PublicAZ1SubnetRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicAZ1Subnet
      RouteTableId: !Ref PublicRouteTable
  PublicAZ2Subnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref Vpc
      CidrBlock: !Ref PublicAZ2SubnetCIDR
      AvailabilityZone: !Select [1, !GetAZs ""]
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub
            - ${AWS::StackName}-public-${AZ}
            - { AZ: !Select [1, !GetAZs ""] }
        - Key: Type
          Value: public
  PublicAZ2SubnetRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicAZ2Subnet
      RouteTableId: !Ref PublicRouteTable
  PublicAZ3Subnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref Vpc
      CidrBlock: !Ref PublicAZ3SubnetCIDR
      AvailabilityZone: !Select [2, !GetAZs ""]
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub
            - ${AWS::StackName}-public-${AZ}
            - { AZ: !Select [2, !GetAZs ""] }
        - Key: Type
          Value: public
  PublicAZ3SubnetRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicAZ3Subnet
      RouteTableId: !Ref PublicRouteTable
  # Private Subnets - NAT Gateways
  AZ1NatGatewayEIP:
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc
    DependsOn: VPCGatewayAttachment
  AZ1NatGateway:
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt AZ1NatGatewayEIP.AllocationId
      SubnetId: !Ref PublicAZ1Subnet
  AZ2NatGatewayEIP:
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc
    DependsOn: VPCGatewayAttachment
  AZ2NatGateway:
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt AZ2NatGatewayEIP.AllocationId
      SubnetId: !Ref PublicAZ2Subnet
  AZ3NatGatewayEIP:
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc
    DependsOn: VPCGatewayAttachment
  AZ3NatGateway:
    Type: AWS::EC2::NatGateway
    Properties:
      AllocationId: !GetAtt AZ3NatGatewayEIP.AllocationId
      SubnetId: !Ref PublicAZ3Subnet
  # Private Subnets
  PrivateAZ1Subnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref Vpc
      CidrBlock: !Ref PrivateAZ1SubnetCIDR
      AvailabilityZone: !Select [0, !GetAZs ""]
      Tags:
        - Key: Name
          Value: !Sub
            - ${AWS::StackName}-private-${AZ}
            - { AZ: !Select [0, !GetAZs ""] }
        - Key: Type
          Value: private
  PrivateAZ1RouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref Vpc
      Tags:
        - Key: Name
          Value: !Sub
            - ${AWS::StackName}-private-${AZ}
            - { AZ: !Select [0, !GetAZs ""] }
        - Key: Type
          Value: private
  PrivateAZ1Route:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateAZ1RouteTable
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref AZ1NatGateway
  PrivateAZ1RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PrivateAZ1Subnet
      RouteTableId: !Ref PrivateAZ1RouteTable
  PrivateAZ2Subnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref Vpc
      CidrBlock: !Ref PrivateAZ2SubnetCIDR
      AvailabilityZone: !Select [1, !GetAZs ""]
      Tags:
        - Key: Name
          Value: !Sub
            - ${AWS::StackName}-private-${AZ}
            - { AZ: !Select [1, !GetAZs ""] }
        - Key: Type
          Value: private
  PrivateAZ2RouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref Vpc
      Tags:
        - Key: Name
          Value: !Sub
            - ${AWS::StackName}-private-${AZ}
            - { AZ: !Select [1, !GetAZs ""] }
        - Key: Type
          Value: private
  PrivateAZ2Route:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateAZ2RouteTable
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref AZ2NatGateway
  PrivateAZ2RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PrivateAZ2Subnet
      RouteTableId: !Ref PrivateAZ2RouteTable
  PrivateAZ3Subnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref Vpc
      CidrBlock: !Ref PrivateAZ3SubnetCIDR
      AvailabilityZone: !Select [2, !GetAZs ""]
      Tags:
        - Key: Name
          Value: !Sub
            - ${AWS::StackName}-private-${AZ}
            - { AZ: !Select [2, !GetAZs ""] }
        - Key: Type
          Value: private
  PrivateAZ3RouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref Vpc
      Tags:
        - Key: Name
          Value: !Sub
            - ${AWS::StackName}-private-${AZ}
            - { AZ: !Select [2, !GetAZs ""] }
        - Key: Type
          Value: private
  PrivateAZ3Route:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateAZ3RouteTable
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref AZ3NatGateway
  PrivateAZ3RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PrivateAZ3Subnet
      RouteTableId: !Ref PrivateAZ3RouteTable
  
Outputs:
  VpcId:
    Description: VPC Id
    Value: !Ref Vpc
    Export:
      Name: !Sub "${AWS::StackName}-VPC-ID"

Cloudformation 스택 생성

Cloudformation 스택을 생성하려면 왼쪽 상단의 "Services\를 클릭하고 "Cloudformation\을 검색합니다.

메인 대시보드에서 "스택 생성\ -> "새 리소스(표준)로"를 클릭합니다.

스택에는 로컬 파일 또는 S3 버킷의 객체 파일일 수 있는 템플릿 파일이 필요합니다. 로컬 템플릿이 있을 것이므로 "템플릿이 준비됨\ -> "템플릿 파일 업로드\를 클릭하고 "파일 선택\을 클릭한 다음 로컬 템플릿 파일을 선택하고 계속 진행합니다.

스택에 이름을 지정하고 다른 모든 매개변수는 변경하지 마십시오.

필요한 경우 태그를 제공하십시오.

페이지를 아래로 스크롤하고 "스택 생성\을 클릭합니다.

시간이 좀 걸리니 그때까지 기다리세요.

"이벤트\ 탭에서 진행 중인 상태나 이벤트를 볼 수 있습니다.

이제 VPC로 이동하여 생성된 VPC를 확인할 수 있습니다. VPC로 이동하려면 상단의 "Services\를 클릭하고 VPC를 왼쪽으로 검색합니다.

메인 대시보드에서 생성된 VPC, 서브넷, 라우팅 테이블, 인터넷 게이트웨이, Nat 게이트웨이의 수를 확인할 수 있습니다.

더 이상 필요하지 않은 경우 스택을 삭제하여 VPC를 삭제할 수 있습니다.

결론

이 기사에서는 2개의 퍼블릭 서브넷과 2개의 프라이빗 서브넷이 있는 VPC를 생성하기 위해 Cloudformation 스택을 생성하는 단계를 살펴보았습니다.