vpc.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: 'AWS CloudFormation template to create a VPC with public and private subnets, internet gateway, route tables, and associate them.'
Resources:
  ProjectVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsHostnames: true
      InstanceTenancy: default
      Tags:
        - Key: Name
          Value: project-vpc
  PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref ProjectVPC
      AvailabilityZone: ap-south-1a
      CidrBlock: 10.0.0.0/20
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: project-subnet-public1-ap-south-1a
  PublicSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref ProjectVPC
      AvailabilityZone: ap-south-1b
      CidrBlock: 10.0.16.0/20
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: project-subnet-public2-ap-south-1b
  PrivateSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref ProjectVPC
      AvailabilityZone: ap-south-1a
      CidrBlock: 10.0.128.0/20
      Tags:
        - Key: Name
          Value: project-subnet-private1-ap-south-1a
  PrivateSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref ProjectVPC
      AvailabilityZone: ap-south-1b
      CidrBlock: 10.0.144.0/20
      Tags:
        - Key: Name
          Value: project-subnet-private2-ap-south-1b
  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: project-igw
  InternetGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref ProjectVPC
      InternetGatewayId: !Ref InternetGateway
  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref ProjectVPC
      Tags:
        - Key: Name
          Value: project-rtb-public
  PublicRoute:
    Type: AWS::EC2::Route
    DependsOn: InternetGatewayAttachment
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway
  PublicSubnet1RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnet1
      RouteTableId: !Ref PublicRouteTable
  PublicSubnet2RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnet2
      RouteTableId: !Ref PublicRouteTable
  PrivateRouteTable1:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref ProjectVPC
      Tags:
        - Key: Name
          Value: project-rtb-private1-ap-south-1a
  PrivateRouteTable2:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref ProjectVPC
      Tags:
        - Key: Name
          Value: project-rtb-private2-ap-south-1b
  PrivateSubnet1RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PrivateSubnet1
      RouteTableId: !Ref PrivateRouteTable1
  PrivateSubnet2RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PrivateSubnet2
      RouteTableId: !Ref PrivateRouteTable2
