AWSTemplateFormatVersion: '2010-09-09'
Description: 'Compact VPC Architecture (10.0.0.0/20) for Public, App, and DB tiers'

Resources:
  # 1. VPC 생성 (범위 축소: 10.0.0.0/20 - 총 4,096개 IP)
  MainVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/20
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: compact-vpc

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: compact-igw

  AttachGateway:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref MainVPC
      InternetGatewayId: !Ref InternetGateway

  # 2. 서브넷 재설계 (10.0.0.0/20 범위 내 배치)
  # Public Subnets (각 256개)
  PublicSubnetA:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MainVPC
      CidrBlock: 10.0.0.0/24
      AvailabilityZone: !Select [0, !GetAZs '']
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: sbn-pub-a

  PublicSubnetB:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MainVPC
      CidrBlock: 10.0.1.0/24
      AvailabilityZone: !Select [1, !GetAZs '']
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: sbn-pub-b

  # App Private Subnets (각 1,024개 - EKS 고려)
  AppPrivateSubnetA:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MainVPC
      CidrBlock: 10.0.4.0/22
      AvailabilityZone: !Select [0, !GetAZs '']
      Tags:
        - Key: Name
          Value: sbn-app-a

  AppPrivateSubnetB:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MainVPC
      CidrBlock: 10.0.8.0/22
      AvailabilityZone: !Select [1, !GetAZs '']
      Tags:
        - Key: Name
          Value: sbn-app-b

  # DB Private Subnets (각 256개)
  DBPrivateSubnetA:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MainVPC
      CidrBlock: 10.0.12.0/24
      AvailabilityZone: !Select [0, !GetAZs '']
      Tags:
        - Key: Name
          Value: sbn-db-a

  DBPrivateSubnetB:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MainVPC
      CidrBlock: 10.0.13.0/24
      AvailabilityZone: !Select [1, !GetAZs '']
      Tags:
        - Key: Name
          Value: sbn-db-b

  # (NAT Gateway, EIP 및 라우팅 설정은 이전과 동일하므로 생략하거나 기존 로직 유지)
  # ... [이후 NAT 및 RouteTable 설정은 위 서브넷 ID를 참조하여 동일하게 구성] ...