You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于可用区的子网创建:利用AWS CloudFormation模板自动配置

Dynamically Create Subnets Matching Region AZ Count in CloudFormation

Got it, this is a super common ask when you want your CloudFormation template to be region-agnostic and adapt automatically to whichever region you deploy it to. No hardcoding AZs or subnet counts—let's build a working template that does exactly that.

Core Approach

We’ll lean on three key CloudFormation intrinsic functions to make this dynamic logic work:

  • !GetAZs '': Pulls the full list of available zones in the target region (the empty string tells it to use the region you’re deploying to)
  • !Cidr: Generates a list of CIDR blocks based on your VPC’s base range and the number of AZs
  • !ForEach: Loops through the AZ list to create one subnet per available zone

Complete CloudFormation Template (YAML)

AWSTemplateFormatVersion: '2010-09-09'
Description: Auto-create subnets matching the number of AZs in the target region

Parameters:
  VPCCidrBlock:
    Type: String
    Default: 10.0.0.0/16
    Description: Base CIDR for your VPC (needs to be large enough to split into subnets)
  SubnetCidrPrefix:
    Type: Number
    Default: 24
    Description: Prefix length for each subnet (e.g., 24 gives /24 subnets)

Resources:
  MyVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VPCCidrBlock
      EnableDnsSupport: true
      EnableDnsHostnames: true

  # Dynamically generate a subnet for each AZ
  !ForEach [AZIndex, !Range [0, !Length !GetAZs '', 1]]:
    Subnet-${AZIndex}:
      Type: AWS::EC2::Subnet
      Properties:
        VpcId: !Ref MyVPC
        AvailabilityZone: !Select [!Ref AZIndex, !GetAZs '']
        CidrBlock: !Select [!Ref AZIndex, !Cidr [!Ref VPCCidrBlock, !Length !GetAZs '', !Ref SubnetCidrPrefix]]
        MapPublicIpOnLaunch: true # Flip to false if you need private subnets

Outputs:
  VpcId:
    Value: !Ref MyVPC
  AllSubnetIds:
    Value: !Join [',', !ForEach [AZIndex, !Range [0, !Length !GetAZs '', 1]]: !Ref Subnet-${AZIndex}]
  SubnetAZs:
    Value: !Join [',', !GetAZs '']

How It Breaks Down

Let’s walk through the critical bits:

  1. Fetch AZ List: !GetAZs '' returns something like ["us-west-2a", "us-west-2b", "us-west-2c"] for a region with 3 AZs.
  2. Generate CIDRs: !Cidr takes your VPC’s base CIDR, the number of AZs (from !Length !GetAZs ''), and your subnet prefix. For a 10.0.0.0/16 VPC and /24 subnets, it spits out ["10.0.0.0/24", "10.0.1.0/24", "10.0.2.0/24"].
  3. Loop to Create Subnets: The !ForEach construct iterates over each index in the AZ list, creating a subnet paired with the matching AZ and CIDR block.
  4. Outputs: The template dumps all subnet IDs and their corresponding AZs for quick reference after deployment.

Quick Notes

  • Double-check your VPCCidrBlock has enough space: a /16 VPC can handle up to 256 /24 subnets, which is way more than any AWS region’s AZ count.
  • Adjust MapPublicIpOnLaunch to false if you’re building private subnets (no public IPs assigned automatically).
  • This uses CloudFormation’s ForEach feature (launched in 2023), so make sure your AWS environment supports it (most modern setups do).

内容的提问来源于stack exchange,提问作者mohd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:37:03