$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-cloudformation-vpcProvides AWS CloudFormation patterns for VPC foundations, including subnets, route tables, internet and NAT gateways, endpoints, and reusable outputs. Use when creating a new network baseline, segmenting public and private workloads, or preparing CloudFormation networking stacks
| 1 | # AWS CloudFormation VPC Infrastructure |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Build a VPC foundation with CloudFormation that stays readable, reusable, and safe to evolve. Provides a clear subnet and routing model with predictable connectivity for public and private workloads, plus outputs that downstream stacks can consume without duplicating network logic. |
| 6 | |
| 7 | Use the `references/` files for larger templates and extended service combinations. |
| 8 | |
| 9 | ## When to Use |
| 10 | |
| 11 | - Creating a new VPC stack for an application or shared platform |
| 12 | - Adding public and private subnets across one or more Availability Zones |
| 13 | - Wiring internet access, NAT egress, or private endpoints |
| 14 | - Exporting VPC, subnet, route table, and security-group-adjacent identifiers for other stacks |
| 15 | - Preparing reusable infrastructure for ECS, EKS, Lambda, EC2, or RDS stacks |
| 16 | |
| 17 | ## Instructions |
| 18 | |
| 19 | ### 1. Start with the address plan |
| 20 | |
| 21 | Before writing resources, define: |
| 22 | - VPC CIDR range |
| 23 | - Number of Availability Zones |
| 24 | - Public, private, and isolated subnet ranges |
| 25 | - Which workloads need internet ingress, NAT egress, or only private AWS service access |
| 26 | |
| 27 | This prevents route-table sprawl and painful subnet replacement later. |
| 28 | |
| 29 | ### 2. Build the core network resources in layers |
| 30 | |
| 31 | Create the stack in this order: |
| 32 | 1. VPC and subnets |
| 33 | 2. Internet Gateway for public ingress and egress |
| 34 | 3. NAT gateways if private subnets need outbound internet access |
| 35 | 4. Route tables and subnet associations |
| 36 | 5. Optional VPC endpoints for private access to AWS services |
| 37 | |
| 38 | Keep each layer easy to inspect in the template and avoid mixing unrelated application resources into the same stack. |
| 39 | |
| 40 | ### 3. Parameterize only the environment-dependent values |
| 41 | |
| 42 | Useful parameters include: |
| 43 | - Environment name |
| 44 | - VPC CIDR and subnet CIDRs |
| 45 | - Number of AZs or explicit subnet IDs in nested-stack scenarios |
| 46 | - Flags for optional endpoints or NAT layout |
| 47 | |
| 48 | Do not parameterize every route or tag unless it meaningfully changes between environments. |
| 49 | |
| 50 | ### 4. Export only what consumers really need |
| 51 | |
| 52 | Typical outputs: |
| 53 | - VPC ID |
| 54 | - Public, private, and isolated subnet IDs |
| 55 | - Route table IDs when downstream stacks must attach routes |
| 56 | - Security boundaries or prefix-list references only when another stack consumes them |
| 57 | |
| 58 | Stable outputs make application stacks easier to compose and migrate. |
| 59 | |
| 60 | ### 5. Validate before deployment |
| 61 | |
| 62 | Run these commands to validate the template and verify routing: |
| 63 | |
| 64 | ```bash |
| 65 | # Validate CloudFormation template syntax |
| 66 | aws cloudformation validate-template --template-body file://vpc.yaml |
| 67 | |
| 68 | # Review change set before applying |
| 69 | aws cloudformation create-change-set \ |
| 70 | --stack-name my-vpc \ |
| 71 | --template-body file://vpc.yaml \ |
| 72 | --change-set-type CREATE |
| 73 | |
| 74 | # Verify route table associations |
| 75 | aws ec2 describe-route-tables \ |
| 76 | --filters "Name=vpc-id,Values=<vpc-id>" |
| 77 | |
| 78 | # Check subnet to route table mappings |
| 79 | aws ec2 describe-route-tables \ |
| 80 | --filters "Name=association.subnet-id,Values=<subnet-id>" |
| 81 | |
| 82 | # Verify internet gateway attachment |
| 83 | aws ec2 describe-internet-gateways \ |
| 84 | --filters "Name=attachment.vpc-id,Values=<vpc-id>" |
| 85 | ``` |
| 86 | |
| 87 | ## Examples |
| 88 | |
| 89 | ### Example 1: Complete two-tier VPC with routing |
| 90 | |
| 91 | This template creates a VPC with public and private subnets, internet gateway, NAT gateway, and properly configured route tables. |
| 92 | |
| 93 | ```yaml |
| 94 | AWSTemplateFormatVersion: "2010-09-09" |
| 95 | Description: "Two-tier VPC with public and private subnets" |
| 96 | |
| 97 | Resources: |
| 98 | # VPC |
| 99 | MainVpc: |
| 100 | Type: AWS::EC2::VPC |
| 101 | Properties: |
| 102 | CidrBlock: 10.0.0.0/16 |
| 103 | EnableDnsHostnames: true |
| 104 | EnableDnsSupport: true |
| 105 | Tags: |
| 106 | - Key: Name |
| 107 | Value: !Sub "${AWS::StackName}-main" |
| 108 | |
| 109 | # Internet Gateway |
| 110 | InternetGateway: |
| 111 | Type: AWS::EC2::InternetGateway |
| 112 | Properties: |
| 113 | Tags: |
| 114 | - Key: Name |
| 115 | Value: !Sub "${AWS::StackName}-igw" |
| 116 | |
| 117 | # Attach IGW to VPC |
| 118 | GatewayToInternet: |
| 119 | Type: AWS::EC2::VPCGatewayAttachment |
| 120 | Properties: |
| 121 | VpcId: !Ref MainVpc |
| 122 | InternetGatewayId: !Ref InternetGateway |
| 123 | |
| 124 | # Public Subnet (AZ 1) |
| 125 | PublicSubnetA: |
| 126 | Type: AWS::EC2::Subnet |
| 127 | Properties: |
| 128 | VpcId: !Ref MainVpc |
| 129 | CidrBlock: 10.0.1.0/24 |
| 130 | AvailabilityZone: !Select [0, !GetAZs ""] |
| 131 | MapPublicIpOnLaunch: true |
| 132 | Tags: |
| 133 | - Key: Name |
| 134 | Value: !Sub "${AWS::StackName}-public-a" |
| 135 | |
| 136 | # Private Subnet (AZ 1) |
| 137 | PrivateSubnetA: |
| 138 | Type: AWS::EC2::Subnet |
| 139 | Properties: |
| 140 | VpcId: !Ref MainVpc |
| 141 | CidrBlock: 10.0.11.0/24 |
| 142 | AvailabilityZone: !Select [0, !GetAZs ""] |
| 143 | Tags: |
| 144 | - Key: Name |
| 145 | Value: !Sub "${AWS::StackName}-private-a" |
| 146 | |
| 147 | # Elastic IP for NAT Gateway |
| 148 | NatEip: |
| 149 | Type: AWS::EC2::EIP |
| 150 | DependsOn: GatewayToInternet |
| 151 | Properties: |
| 152 | Doma |