mcpField
A
aws-cloudformation-vpc

aws-cloudformation-vpc

github.com/giuseppe-trisciuoglio云与 DevOps36565 次安装7 周前更新
代码仓库

关于 aws-cloudformation-vpc

为使用 CloudFormation 构建 VPC 网络基础设施的用户提供指导,涵盖子网、路由、互联网及私有服务连接,并通过输出供其他堆栈复用。

主要功能

  1. 01规划 VPC 与子网 CIDR
  2. 02配置互联网网关和 NAT
  3. 03设置路由表与子网关联
  4. 04添加 AWS 服务 VPC 端点
  5. 05导出网络标识供其他堆栈使用

适用场景

  1. 01创建应用或共享平台 VPC
  2. 02为公有和私有子网配置路由
  3. 03为 AWS 服务设置私有访问
  4. 04向其他堆栈提供子网标识

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill aws-cloudformation-vpc

安装后在 Claude Code 中输入/aws-cloudformation-vpc确认已加载。

附带文件references/examples.mdreferences/reference.md

详细介绍

AWS CloudFormation VPC 基础设施

概述

使用 CloudFormation 构建 VPC 基础架构,使其易于阅读、重复使用并安全演进。提供清晰的子网和路由模型,为公有和私有工作负载实现可预测的连接,并提供下游堆栈可使用的输出,避免重复网络逻辑。

较大的模板和扩展的服务组合请参阅 references/ 文件。

适用场景

  • 为应用或共享平台创建新的 VPC 堆栈
  • 在一个或多个可用区中添加公有和私有子网
  • 配置互联网访问、NAT 出站流量或私有端点
  • 导出 VPC、子网、路由表以及与安全组相关的标识符,供其他堆栈使用
  • 为 ECS、EKS、Lambda、EC2 或 RDS 堆栈准备可重复使用的基础设施

使用说明

1. 从地址规划开始

创建资源前,先定义:

  • VPC CIDR 范围
  • 可用区数量
  • 公有、私有和隔离子网范围
  • 哪些工作负载需要互联网入站流量、NAT 出站流量,或仅需访问私有 AWS 服务

这有助于避免路由表过度扩张,以及日后替换子网时遇到麻烦。

2. 分层构建核心网络资源

按以下顺序创建堆栈:

  1. VPC 和子网
  2. 用于公有入站和出站流量的互联网网关
  3. 如果私有子网需要出站互联网访问,则创建 NAT 网关
  4. 路由表和子网关联
  5. 可选的 VPC 端点,用于私有访问 AWS 服务

确保模板中的每一层都易于检查,并避免在同一个堆栈中混入无关的应用资源。

3. 仅参数化依赖环境的值

实用参数包括:

  • 环境名称
  • VPC CIDR 和子网 CIDR
  • 可用区数量,或嵌套堆栈场景中的显式子网 ID
  • 可选端点或 NAT 布局的开关

除非路由或标签会因环境而发生有意义的变化,否则不要将它们全部参数化。

4. 仅导出使用方真正需要的内容

常见输出:

  • VPC ID
  • 公有、私有和隔离子网 ID
  • 下游堆栈需要附加路由时使用的路由表 ID
  • 仅在其他堆栈会使用时导出安全边界或前缀列表引用

稳定的输出有助于更轻松地组合和迁移应用堆栈。

5. 部署前进行验证

运行以下命令以验证模板并检查路由:

# Validate CloudFormation template syntax
aws cloudformation validate-template --template-body file://vpc.yaml

# Review change set before applying
aws cloudformation create-change-set \
  --stack-name my-vpc \
  --template-body file://vpc.yaml \
  --change-set-type CREATE

# Verify route table associations
aws ec2 describe-route-tables \
  --filters "Name=vpc-id,Values=<vpc-id>"

# Check subnet to route table mappings
aws ec2 describe-route-tables \
  --filters "Name=association.subnet-id,Values=<subnet-id>"

# Verify internet gateway attachment
aws ec2 describe-internet-gateways \
  --filters "Name=attachment.vpc-id,Values=<vpc-id>"

示例

示例 1:包含路由的完整双层 VPC

此模板会创建一个 VPC,其中包含公有和私有子网、互联网网关、NAT 网关以及配置正确的路由表。

AWSTemplateFormatVersion: "2010-09-09"
Description: "Two-tier VPC with public and private subnets"

Resources:
  # VPC
  MainVpc:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsHostnames: true
      EnableDnsSupport: true
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-main"

  # Internet Gateway
  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-igw"

  # Attach IGW to VPC
  GatewayToInternet:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref MainVpc
      InternetGatewayId: !Ref InternetGateway

  # Public Subnet (AZ 1)
  PublicSubnetA:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MainVpc
      CidrBlock: 10.0.1.0/24
      AvailabilityZone: !Select [0, !GetAZs ""]
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-public-a"

  # Private Subnet (AZ 1)
  PrivateSubnetA:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref MainVpc
      CidrBlock: 10.0.11.0/24
      AvailabilityZone: !Select [0, !GetAZs ""]
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-private-a"

  # Elastic IP for NAT Gateway
  NatEip:
    Type: AWS::EC2::EIP
    DependsOn: GatewayToInternet
    Properties:
      Domain: vpc

  # NAT Gateway
  NatGateway:
    Type: AWS::EC2::NatGateway
    Properties:
      SubnetId: !Ref PublicSubnetA
      AllocationId: !GetAtt NatEip.AllocationId

  # Public Route Table
  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref MainVpc
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-public-rt"

  # Default route to IGW
  PublicDefaultRoute:
    Type: AWS::EC2::Route
    DependsOn: GatewayToInternet
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway

  # Associate public subnet
  PublicSubnetARouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnetA
      RouteTableId: !Ref PublicRouteTable

  # Private Route Table
  PrivateRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref MainVpc
      Tags:
        - Key: Name
          Value: !Sub "${AWS::StackName}-private-rt"

  # Default route via NAT Gateway
  PrivateDefaultRoute:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PrivateRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref NatGateway

  # Associate private subnet
  PrivateSubnetARouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PrivateSubnetA
      RouteTableId: !Ref PrivateRouteTable

Outputs:
  VpcId:
    Description: VPC ID
    Value: !Ref MainVpc
    Export:
      Name: !Sub "${AWS::StackName}-VpcId"

  PublicSubnetA:
    Description: Public subnet AZ1
    Value: !Ref PublicSubnetA
    Export:
      Name: !Sub "${AWS::StackName}-PublicSubnetA"

  PrivateSubnetA:
    Description: Private subnet AZ1
    Value: !Ref PrivateSubnetA
    Export:
      Name: !Sub "${AWS::StackName}-PrivateSubnetA"

  PublicRouteTableId:
    Description: Public route table ID
    Value: !Ref PublicRouteTable
    Export:
      Name: !Sub "${AWS::StackName}-PublicRouteTableId"

  PrivateRouteTableId:
    Description: Private route table ID
    Value: !Ref PrivateRouteTable
    Export:
      Name: !Sub "${AWS::StackName}-PrivateRouteTableId"
示例 2:用于私有 S3 访问的 VPC 端点
Resources:
  # S3 VPC Endpoint
  S3Endpoint:
    Type: AWS::EC2::VPCEndpoint
    Properties:
      VpcId: !Ref MainVpc
      ServiceName: !Sub "com.amazonaws.${AWS::Region}.s3"
      RouteTableIds:
        - !Ref PrivateRouteTable
      VpcEndpointType: Gateway

最佳实践

  • 在名称和标签中明确标注公有、私有和隔离子网的用途
  • 在预算允许时,生产环境优先为每个可用区使用一个 NAT 网关,以提高韧性
  • 使用 VPC 端点,减少访问 AWS 服务时不必要的 NAT 流量
  • 从网络堆栈导出 VPC 和子网标识符,而不是在其他地方重新设定网络假设
  • 与依赖此网络的堆栈一起审查网络变更,因为路由和子网变更可能造成广泛影响
  • 保持根技能内容精简,并将更复杂的网络变体移至 references/examples.md

限制和警告

  • NAT 网关会产生小时费用和数据传输费用;访问 AWS 服务时请考虑使用 VPC 端点
  • CIDR 重叠会阻碍对等连接、传输网络和未来的网络扩展
  • 即使模板有效,替换路由表或子网也可能中断流量
  • 端点配额、可用区可用性和服务特定的子网要求因区域而异
  • 将可用区硬编码可能会降低跨账户和区域的可移植性

参考资料

  • references/examples.md
  • references/reference.md

评论

支持 Markdown