|
| 1 | +AWSTemplateFormatVersion: '2010-09-09' |
| 2 | +Description: >- |
| 3 | + Shared VPC with public and private subnets across two AZs. |
| 4 | + Includes internet gateway, NAT gateway, and route tables. |
| 5 | + Deploy once, reference from tutorial stacks via cross-stack exports. |
| 6 | +
|
| 7 | +Resources: |
| 8 | + VPC: |
| 9 | + Type: AWS::EC2::VPC |
| 10 | + Properties: |
| 11 | + CidrBlock: 10.0.0.0/16 |
| 12 | + EnableDnsSupport: true |
| 13 | + EnableDnsHostnames: true |
| 14 | + Tags: |
| 15 | + - Key: Name |
| 16 | + Value: !Sub '${AWS::StackName}' |
| 17 | + |
| 18 | + InternetGateway: |
| 19 | + Type: AWS::EC2::InternetGateway |
| 20 | + |
| 21 | + GatewayAttachment: |
| 22 | + Type: AWS::EC2::VPCGatewayAttachment |
| 23 | + Properties: |
| 24 | + VpcId: !Ref VPC |
| 25 | + InternetGatewayId: !Ref InternetGateway |
| 26 | + |
| 27 | + PublicSubnet1: |
| 28 | + Type: AWS::EC2::Subnet |
| 29 | + Properties: |
| 30 | + VpcId: !Ref VPC |
| 31 | + CidrBlock: 10.0.1.0/24 |
| 32 | + AvailabilityZone: !Select [0, !GetAZs ''] |
| 33 | + MapPublicIpOnLaunch: true |
| 34 | + Tags: |
| 35 | + - Key: Name |
| 36 | + Value: !Sub '${AWS::StackName}-public-1' |
| 37 | + |
| 38 | + PublicSubnet2: |
| 39 | + Type: AWS::EC2::Subnet |
| 40 | + Properties: |
| 41 | + VpcId: !Ref VPC |
| 42 | + CidrBlock: 10.0.2.0/24 |
| 43 | + AvailabilityZone: !Select [1, !GetAZs ''] |
| 44 | + MapPublicIpOnLaunch: true |
| 45 | + Tags: |
| 46 | + - Key: Name |
| 47 | + Value: !Sub '${AWS::StackName}-public-2' |
| 48 | + |
| 49 | + PrivateSubnet1: |
| 50 | + Type: AWS::EC2::Subnet |
| 51 | + Properties: |
| 52 | + VpcId: !Ref VPC |
| 53 | + CidrBlock: 10.0.3.0/24 |
| 54 | + AvailabilityZone: !Select [0, !GetAZs ''] |
| 55 | + Tags: |
| 56 | + - Key: Name |
| 57 | + Value: !Sub '${AWS::StackName}-private-1' |
| 58 | + |
| 59 | + PrivateSubnet2: |
| 60 | + Type: AWS::EC2::Subnet |
| 61 | + Properties: |
| 62 | + VpcId: !Ref VPC |
| 63 | + CidrBlock: 10.0.4.0/24 |
| 64 | + AvailabilityZone: !Select [1, !GetAZs ''] |
| 65 | + Tags: |
| 66 | + - Key: Name |
| 67 | + Value: !Sub '${AWS::StackName}-private-2' |
| 68 | + |
| 69 | + NatEip: |
| 70 | + Type: AWS::EC2::EIP |
| 71 | + DependsOn: GatewayAttachment |
| 72 | + |
| 73 | + NatGateway: |
| 74 | + Type: AWS::EC2::NatGateway |
| 75 | + Properties: |
| 76 | + AllocationId: !GetAtt NatEip.AllocationId |
| 77 | + SubnetId: !Ref PublicSubnet1 |
| 78 | + Tags: |
| 79 | + - Key: Name |
| 80 | + Value: !Sub '${AWS::StackName}-nat' |
| 81 | + |
| 82 | + PublicRouteTable: |
| 83 | + Type: AWS::EC2::RouteTable |
| 84 | + Properties: |
| 85 | + VpcId: !Ref VPC |
| 86 | + |
| 87 | + PublicRoute: |
| 88 | + Type: AWS::EC2::Route |
| 89 | + DependsOn: GatewayAttachment |
| 90 | + Properties: |
| 91 | + RouteTableId: !Ref PublicRouteTable |
| 92 | + DestinationCidrBlock: 0.0.0.0/0 |
| 93 | + GatewayId: !Ref InternetGateway |
| 94 | + |
| 95 | + PublicSubnet1RouteAssoc: |
| 96 | + Type: AWS::EC2::SubnetRouteTableAssociation |
| 97 | + Properties: |
| 98 | + SubnetId: !Ref PublicSubnet1 |
| 99 | + RouteTableId: !Ref PublicRouteTable |
| 100 | + |
| 101 | + PublicSubnet2RouteAssoc: |
| 102 | + Type: AWS::EC2::SubnetRouteTableAssociation |
| 103 | + Properties: |
| 104 | + SubnetId: !Ref PublicSubnet2 |
| 105 | + RouteTableId: !Ref PublicRouteTable |
| 106 | + |
| 107 | + PrivateRouteTable: |
| 108 | + Type: AWS::EC2::RouteTable |
| 109 | + Properties: |
| 110 | + VpcId: !Ref VPC |
| 111 | + |
| 112 | + PrivateRoute: |
| 113 | + Type: AWS::EC2::Route |
| 114 | + Properties: |
| 115 | + RouteTableId: !Ref PrivateRouteTable |
| 116 | + DestinationCidrBlock: 0.0.0.0/0 |
| 117 | + NatGatewayId: !Ref NatGateway |
| 118 | + |
| 119 | + PrivateSubnet1RouteAssoc: |
| 120 | + Type: AWS::EC2::SubnetRouteTableAssociation |
| 121 | + Properties: |
| 122 | + SubnetId: !Ref PrivateSubnet1 |
| 123 | + RouteTableId: !Ref PrivateRouteTable |
| 124 | + |
| 125 | + PrivateSubnet2RouteAssoc: |
| 126 | + Type: AWS::EC2::SubnetRouteTableAssociation |
| 127 | + Properties: |
| 128 | + SubnetId: !Ref PrivateSubnet2 |
| 129 | + RouteTableId: !Ref PrivateRouteTable |
| 130 | + |
| 131 | +Outputs: |
| 132 | + VpcId: |
| 133 | + Value: !Ref VPC |
| 134 | + Export: |
| 135 | + Name: !Sub '${AWS::StackName}-VpcId' |
| 136 | + PublicSubnet1Id: |
| 137 | + Value: !Ref PublicSubnet1 |
| 138 | + Export: |
| 139 | + Name: !Sub '${AWS::StackName}-PublicSubnet1' |
| 140 | + PublicSubnet2Id: |
| 141 | + Value: !Ref PublicSubnet2 |
| 142 | + Export: |
| 143 | + Name: !Sub '${AWS::StackName}-PublicSubnet2' |
| 144 | + PrivateSubnet1Id: |
| 145 | + Value: !Ref PrivateSubnet1 |
| 146 | + Export: |
| 147 | + Name: !Sub '${AWS::StackName}-PrivateSubnet1' |
| 148 | + PrivateSubnet2Id: |
| 149 | + Value: !Ref PrivateSubnet2 |
| 150 | + Export: |
| 151 | + Name: !Sub '${AWS::StackName}-PrivateSubnet2' |
| 152 | + PublicSubnets: |
| 153 | + Value: !Join [',', [!Ref PublicSubnet1, !Ref PublicSubnet2]] |
| 154 | + Export: |
| 155 | + Name: !Sub '${AWS::StackName}-PublicSubnets' |
| 156 | + PrivateSubnets: |
| 157 | + Value: !Join [',', [!Ref PrivateSubnet1, !Ref PrivateSubnet2]] |
| 158 | + Export: |
| 159 | + Name: !Sub '${AWS::StackName}-PrivateSubnets' |
0 commit comments