Resources 部分中的 Fn::ForEach 示例 - AWS CloudFormation

这是新的《AWS CloudFormation 模板参考指南》。请更新您的书签和链接。有关开始使用 CloudFormation 的帮助,请参阅《AWS CloudFormation 用户指南》https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html

Resources 部分中的 Fn::ForEach 示例

这些示例演示了如何使用 Resources 部分中的 Fn::ForEach 内置函数。有关此部分的更多信息,请参阅《AWS CloudFormation 用户指南》中的 Resources

复制 Amazon SNS 资源

此示例片段返回包含四个 Amazon SNS 主题的列表,其逻辑 ID 对应于集合中的项目(SuccessFailureTimeoutUnknown),相匹配的 TopicNameFifoTopic 设置为 true

注意

对于需要同时使用 FIFO 和标准主题的模板,您可以使用 DisplayName 属性而不是 TopicName。这样,当 FifoTopictrue 时,CloudFormation 会自动生成带有相应 .fifo 后缀的主题名称。只需在 Properties 部分中将 TopicName 替换为 DisplayName: !Ref TopicName 即可。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::LanguageExtensions", "Resources": { "Fn::ForEach::Topics": [ "TopicName", ["Success", "Failure", "Timeout", "Unknown"], { "SnsTopic${TopicName}": { "Type": "AWS::SNS::Topic", "Properties": { "TopicName": {"Fn::Sub": "${TopicName}.fifo"}, "FifoTopic": true } } } ] } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::LanguageExtensions Resources: 'Fn::ForEach::Topics': - TopicName - [Success, Failure, Timeout, Unknown] - 'SnsTopic${TopicName}': Type: 'AWS::SNS::Topic' Properties: TopicName: !Sub '${TopicName}.fifo' FifoTopic: true

转换后的模板将等同于以下模板:

AWSTemplateFormatVersion: 2010-09-09 Resources: SnsTopicSuccess: Type: AWS::SNS::Topic Properties: TopicName: Success.fifo FifoTopic: true SnsTopicFailure: Type: AWS::SNS::Topic Properties: TopicName: Failure.fifo FifoTopic: true SnsTopicTimeout: Type: AWS::SNS::Topic Properties: TopicName: Timeout.fifo FifoTopic: true SnsTopicUnknown: Type: AWS::SNS::Topic Properties: TopicName: Unknown.fifo FifoTopic: true

复制 Amazon DynamoDB 资源

此示例片段创建了四个 AWS::DynamoDB::Table 资源,其名称为 PointsScore 等。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::LanguageExtensions", "Resources": { "Fn::ForEach::Tables": [ "TableName", ["Points", "Score", "Name", "Leaderboard"], { "DynamoDB${TableName}": { "Type": "AWS::DynamoDB::Table", "Properties": { "TableName": { "Ref": "TableName" }, "AttributeDefinitions": [ { "AttributeName": "id", "AttributeType": "S" } ], "KeySchema": [ { "AttributeName": "id", "KeyType": "HASH" } ], "ProvisionedThroughput": { "ReadCapacityUnits": "5", "WriteCapacityUnits": "5" } } } } ] } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::LanguageExtensions Resources: 'Fn::ForEach::Tables': - TableName - [Points, Score, Name, Leaderboard] - 'DynamoDB${TableName}': Type: 'AWS::DynamoDB::Table' Properties: TableName: !Ref TableName AttributeDefinitions: - AttributeName: id AttributeType: S KeySchema: - AttributeName: id KeyType: HASH ProvisionedThroughput: ReadCapacityUnits: '5' WriteCapacityUnits: '5'

转换后的模板将等同于以下模板:

AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::LanguageExtensions Resources: DynamoDBPoints: Type: AWS::DynamoDB::Table Properties: TableName: Points AttributeDefinitions: - AttributeName: id AttributeType: S KeySchema: - AttributeName: id KeyType: HASH ProvisionedThroughput: ReadCapacityUnits: '5' WriteCapacityUnits: '5' DynamoDBScore: Type: AWS::DynamoDB::Table Properties: TableName: Score AttributeDefinitions: - AttributeName: id AttributeType: S KeySchema: - AttributeName: id KeyType: HASH ProvisionedThroughput: ReadCapacityUnits: '5' WriteCapacityUnits: '5' DynamoDBName: Type: AWS::DynamoDB::Table Properties: TableName: Name AttributeDefinitions: - AttributeName: id AttributeType: S KeySchema: - AttributeName: id KeyType: HASH ProvisionedThroughput: ReadCapacityUnits: '5' WriteCapacityUnits: '5' DynamoDBLeaderboard: Type: AWS::DynamoDB::Table Properties: TableName: Leaderboard AttributeDefinitions: - AttributeName: id AttributeType: S KeySchema: - AttributeName: id KeyType: HASH ProvisionedThroughput: ReadCapacityUnits: '5' WriteCapacityUnits: '5'

复制多个资源

此示例使用命名约定 "{ResourceType}${Identifier}" 创建了多个 AWS::EC2::NatGatewayAWS::EC2::EIP 实例。您可以在单个 Fn::ForEach 循环中声明多个资源类型,以利用单个标识符。

集合中每个元素的唯一值在 Mappings 部分中定义,其中 Fn::FindInMap 内置函数用于引用相应的值。如果 Fn::FindInMap 无法找到相应的标识符,则 Condition 属性将不会设置为解析为 !Ref AWS:::NoValue

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::LanguageExtensions", "Conditions": { "TwoNatGateways": {"Fn::Equals": [{"Ref": "AWS::Region"}, "us-east-1"]}, "ThreeNatGateways": {"Fn::Equals": [{"Ref": "AWS::Region"}, "us-west-2"]} }, "Mappings": { "NatGateway": { "Condition": { "B": "TwoNatGateways", "C": "ThreeNatGateways" } } }, "Resources": { "VPC": { "Type": "AWS::EC2::VPC", "Properties": {"CidrBlock": "10.0.0.0/16"} }, "PublicSubnetA": { "Type": "AWS::EC2::Subnet", "Properties": { "VpcId": {"Ref": "VPC"}, "CidrBlock": "10.0.1.0/24", "AvailabilityZone": {"Fn::Select": [0, {"Fn::GetAZs": ""}]} } }, "PublicSubnetB": { "Type": "AWS::EC2::Subnet", "Properties": { "VpcId": {"Ref": "VPC"}, "CidrBlock": "10.0.2.0/24", "AvailabilityZone": {"Fn::Select": [1, {"Fn::GetAZs": ""}]} } }, "PublicSubnetC": { "Type": "AWS::EC2::Subnet", "Properties": { "VpcId": {"Ref": "VPC"}, "CidrBlock": "10.0.3.0/24", "AvailabilityZone": {"Fn::Select": [2, {"Fn::GetAZs": ""}]} } }, "Fn::ForEach::NatGatewayAndEIP": [ "Identifier", [ "A", "B", "C" ], { "NatGateway${Identifier}": { "Type": "AWS::EC2::NatGateway", "Properties": { "AllocationId": {"Fn::GetAtt": [{"Fn::Sub": "NatGatewayAttachment${Identifier}"}, "AllocationId"]}, "SubnetId": {"Ref": {"Fn::Sub": "PublicSubnet${Identifier}"}} }, "Condition": {"Fn::FindInMap": ["NatGateway", "Condition", {"Ref": "Identifier"}, {"DefaultValue": {"Ref": "AWS::NoValue"}}]} }, "NatGatewayAttachment${Identifier}": { "Type": "AWS::EC2::EIP", "Properties": { "Domain": "vpc" }, "Condition": {"Fn::FindInMap": ["NatGateway", "Condition", {"Ref": "Identifier"}, {"DefaultValue": {"Ref": "AWS::NoValue"}}]} } } ] } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::LanguageExtensions Conditions: TwoNatGateways: !Equals [!Ref "AWS::Region", "us-east-1"] ThreeNatGateways: !Equals [!Ref "AWS::Region", "us-west-2"] Mappings: NatGateway: Condition: B: TwoNatGateways C: ThreeNatGateways Resources: VPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 PublicSubnetA: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 10.0.1.0/24 AvailabilityZone: !Select [0, !GetAZs ""] PublicSubnetB: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 10.0.2.0/24 AvailabilityZone: !Select [1, !GetAZs ""] PublicSubnetC: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 10.0.3.0/24 AvailabilityZone: !Select [2, !GetAZs ""] Fn::ForEach::NatGatewayAndEIP: - Identifier - - A - B - C - NatGateway${Identifier}: Type: AWS::EC2::NatGateway Properties: AllocationId: !GetAtt - !Sub NatGatewayAttachment${Identifier} - AllocationId SubnetId: !Ref Fn::Sub: PublicSubnet${Identifier} Condition: !FindInMap - NatGateway - Condition - !Ref Identifier - DefaultValue: !Ref AWS::NoValue NatGatewayAttachment${Identifier}: Type: AWS::EC2::EIP Properties: Domain: vpc Condition: !FindInMap - NatGateway - Condition - !Ref Identifier - DefaultValue: !Ref AWS::NoValue

转换后的模板将等同于以下模板:

AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::LanguageExtensions Conditions: TwoNatGateways: !Equals [!Ref "AWS::Region", "us-east-1"] ThreeNatGateways: !Equals [!Ref "AWS::Region", "us-west-2"] Resources: VPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 PublicSubnetA: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 10.0.1.0/24 AvailabilityZone: !Select [0, !GetAZs ""] PublicSubnetB: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 10.0.2.0/24 AvailabilityZone: !Select [1, !GetAZs ""] PublicSubnetC: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 10.0.3.0/24 AvailabilityZone: !Select [2, !GetAZs ""] NatGatewayA: Type: AWS::EC2::NatGateway Properties: AllocationId: !GetAtt - NatGatewayAttachmentA - AllocationId SubnetId: !Ref PublicSubnetA NatGatewayB: Type: AWS::EC2::NatGateway Properties: AllocationId: !GetAtt - NatGatewayAttachmentB - AllocationId SubnetId: !Ref PublicSubnetB Condition: TwoNatGateways NatGatewayC: Type: AWS::EC2::NatGateway Properties: AllocationId: !GetAtt - NatGatewayAttachmentC - AllocationId SubnetId: !Ref PublicSubnetC Condition: ThreeNatGateways NatGatewayAttachmentA: Type: AWS::EC2::EIP Properties: Domain: vpc NatGatewayAttachmentB: Type: AWS::EC2::EIP Properties: Domain: vpc Condition: TwoNatGateways NatGatewayAttachmentC: Type: AWS::EC2::EIP Properties: Domain: vpc Condition: ThreeNatGateways

使用嵌套 Fn::ForEach 循环复制多个资源

此示例使用嵌套的 Fn::ForEach 循环将三个资源(AWS::EC2::NetworkAclAWS::EC2::SubnetAWS::EC2::SubnetNetworkAclAssociation)相互映射。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::LanguageExtensions", "Resources": { "VPC": { "Type": "AWS::EC2::VPC", "Properties": { "CidrBlock": "10.0.0.0/16", "EnableDnsSupport": "true", "EnableDnsHostnames": "true" } }, "Fn::ForEach::SubnetResources": [ "Prefix", [ "Transit", "Public" ], { "Nacl${Prefix}Subnet": { "Type": "AWS::EC2::NetworkAcl", "Properties": { "VpcId": { "Ref": "VPC" } } }, "Fn::ForEach::LoopInner": [ "Suffix", [ "A", "B", "C" ], { "${Prefix}Subnet${Suffix}": { "Type": "AWS::EC2::Subnet", "Properties": { "VpcId": { "Ref": "VPC" } } }, "Nacl${Prefix}Subnet${Suffix}Association": { "Type": "AWS::EC2::SubnetNetworkAclAssociation", "Properties": { "SubnetId": { "Ref": { "Fn::Sub": "${Prefix}Subnet${Suffix}" } }, "NetworkAclId": { "Ref": { "Fn::Sub": "Nacl${Prefix}Subnet" } } } } } ] } ] } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::LanguageExtensions Resources: VPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 EnableDnsSupport: 'true' EnableDnsHostnames: 'true' 'Fn::ForEach::SubnetResources': - Prefix - [Transit, Public] - 'Nacl${Prefix}Subnet': Type: 'AWS::EC2::NetworkAcl' Properties: VpcId: !Ref 'VPC' 'Fn::ForEach::LoopInner': - Suffix - [A, B, C] - '${Prefix}Subnet${Suffix}': Type: 'AWS::EC2::Subnet' Properties: VpcId: !Ref 'VPC' 'Nacl${Prefix}Subnet${Suffix}Association': Type: 'AWS::EC2::SubnetNetworkAclAssociation' Properties: SubnetId: !Ref 'Fn::Sub': '${Prefix}Subnet${Suffix}' NetworkAclId: !Ref 'Fn::Sub': 'Nacl${Prefix}Subnet'

转换后的模板将等同于以下模板:

AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::LanguageExtensions Resources: VPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 EnableDnsSupport: 'true' EnableDnsHostnames: 'true' NaclTransitSubnet: Type: AWS::EC2::NetworkAcl Properties: VpcId: !Ref VPC TransitSubnetA: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC NaclTransitSubnetAAssociation: Type: AWS::EC2::SubnetNetworkAclAssociation Properties: SubnetId: !Ref TransitSubnetA NetworkAclId: !Ref NaclTransitSubnet TransitSubnetB: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC NaclTransitSubnetBAssociation: Type: AWS::EC2::SubnetNetworkAclAssociation Properties: SubnetId: !Ref TransitSubnetB NetworkAclId: !Ref NaclTransitSubnet TransitSubnetC: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC NaclTransitSubnetCAssociation: Type: AWS::EC2::SubnetNetworkAclAssociation Properties: SubnetId: !Ref TransitSubnetC NetworkAclId: !Ref NaclTransitSubnet NaclPublicSubnet: Type: AWS::EC2::NetworkAcl Properties: VpcId: !Ref VPC PublicSubnetA: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC NaclPublicSubnetAAssociation: Type: AWS::EC2::SubnetNetworkAclAssociation Properties: SubnetId: !Ref PublicSubnetA NetworkAclId: !Ref NaclPublicSubnet PublicSubnetB: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC NaclPublicSubnetBAssociation: Type: AWS::EC2::SubnetNetworkAclAssociation Properties: SubnetId: !Ref PublicSubnetB NetworkAclId: !Ref NaclPublicSubnet PublicSubnetC: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC NaclPublicSubnetCAssociation: Type: AWS::EC2::SubnetNetworkAclAssociation Properties: SubnetId: !Ref PublicSubnetC NetworkAclId: !Ref NaclPublicSubnet

引用 Amazon EC2 资源的复制属性

此示例使用 Fn::ForEach 内置函数来引用复制的 AWS::EC2::Instance 资源。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::LanguageExtensions", "Mappings": { "Instances": { "InstanceType": { "B": "m5.4xlarge", "C": "c5.2xlarge" }, "ImageId": {"A": "ami-id1"} } }, "Resources": { "Fn::ForEach::Instances": [ "Identifier", [ "A", "B", "C" ], { "Instance${Identifier}": { "Type": "AWS::EC2::Instance", "Properties": { "InstanceType": {"Fn::FindInMap": ["Instances", "InstanceType", {"Ref": "Identifier"}, {"DefaultValue": "m5.xlarge"}]}, "ImageId": {"Fn::FindInMap": ["Instances", "ImageId", {"Ref": "Identifier"}, {"DefaultValue": "ami-id-default"}]} } } } ] }, "Outputs": { "SecondInstanceId": { "Description": "Instance Id for InstanceB", "Value": {"Ref": "InstanceB"} }, "SecondPrivateIp": { "Description": "Private IP for InstanceB", "Value": { "Fn::GetAtt": [ "InstanceB", "PrivateIp" ] } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::LanguageExtensions Mappings: Instances: InstanceType: B: m5.4xlarge C: c5.2xlarge ImageId: A: ami-id1 Resources: 'Fn::ForEach::Instances': - Identifier - [A, B, C] - 'Instance${Identifier}': Type: 'AWS::EC2::Instance' Properties: InstanceType: !FindInMap [Instances, InstanceType, !Ref 'Identifier', {DefaultValue: m5.xlarge}] ImageId: !FindInMap [Instances, ImageId, !Ref 'Identifier', {DefaultValue: ami-id-default}] Outputs: SecondInstanceId: Description: Instance Id for InstanceB Value: !Ref 'InstanceB' SecondPrivateIp: Description: Private IP for InstanceB Value: !GetAtt [InstanceB, PrivateIp]

转换后的模板将等同于以下模板:

AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::LanguageExtensions Resources: InstanceA: Type: AWS::EC2::Instance Properties: InstanceType: m5.xlarge ImageId: ami-id1 InstanceB: Type: AWS::EC2::Instance Properties: InstanceType: m5.4xlarge ImageId: ami-id-default InstanceC: Type: AWS::EC2::Instance Properties: InstanceType: c5.2xlarge ImageId: ami-id-default Outputs: SecondInstanceId: Description: Instance Id for InstanceB Value: !Ref InstanceB SecondPrivateIp: Description: Private IP for InstanceB Value: !GetAtt [InstanceB, PrivateIp]

复制 Amazon EC2 资源的属性

此示例使用 Fn::ForEach 内置函数将某些属性(例如 ImageIdInstanceTypeAvailabilityZone)重复到 AWS::EC2::Instance 资源。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::LanguageExtensions", "Mappings": { "InstanceA": { "Properties": { "ImageId": "ami-id1", "InstanceType": "m5.xlarge" } }, "InstanceB": { "Properties": { "ImageId": "ami-id2" } }, "InstanceC": { "Properties": { "ImageId": "ami-id3", "InstanceType": "m5.2xlarge", "AvailabilityZone": "us-east-1a" } } }, "Resources": { "Fn::ForEach::Instances": [ "InstanceLogicalId", [ "InstanceA", "InstanceB", "InstanceC" ], { "${InstanceLogicalId}": { "Type": "AWS::EC2::Instance", "Properties": { "DisableApiTermination": true, "UserData": { "Fn::Base64": { "Fn::Join": [ "", [ "#!/bin/bash\n", "yum update -y\n", "yum install -y httpd.x86_64\n", "systemctl start httpd.service\n", "systemctl enable httpd.service\n", "echo \"Hello World from $(hostname -f)\" > /var/www/html/index.html\n" ] ] } }, "Fn::ForEach::Properties": [ "PropertyName", [ "ImageId", "InstanceType", "AvailabilityZone" ], { "${PropertyName}": { "Fn::FindInMap": [ { "Ref": "InstanceLogicalId" }, "Properties", { "Ref": "PropertyName"}, { "DefaultValue": { "Ref": "AWS::NoValue" } } ] } } ] } } } ] } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::LanguageExtensions Mappings: InstanceA: Properties: ImageId: ami-id1 InstanceType: m5.xlarge InstanceB: Properties: ImageId: ami-id2 InstanceC: Properties: ImageId: ami-id3 InstanceType: m5.2xlarge AvailabilityZone: us-east-1a Resources: 'Fn::ForEach::Instances': - InstanceLogicalId - [InstanceA, InstanceB, InstanceC] - '${InstanceLogicalId}': Type: 'AWS::EC2::Instance' Properties: DisableApiTermination: true UserData: Fn::Base64: !Sub | #!/bin/bash yum update -y yum install -y httpd.x86_64 systemctl start httpd.service systemctl enable httpd.service echo "Hello World from $(hostname -f)" > /var/www/html/index.html 'Fn::ForEach::Properties': - PropertyName - [ImageId, InstanceType, AvailabilityZone] - '${PropertyName}': 'Fn::FindInMap': - Ref: 'InstanceLogicalId' - Properties - Ref: 'PropertyName' - {DefaultValue: !Ref 'AWS::NoValue'}

转换后的模板将等同于以下模板:

AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::LanguageExtensions Resources: InstanceA: Type: AWS::EC2::Instance Properties: DisableApiTermination: true UserData: Fn::Base64: !Sub | #!/bin/bash yum update -y yum install -y httpd.x86_64 systemctl start httpd.service systemctl enable httpd.service echo "Hello World from $(hostname -f)" > /var/www/html/index.html ImageId: ami-id1 InstanceType: m5.xlarge InstanceB: Type: AWS::EC2::Instance Properties: DisableApiTermination: true UserData: Fn::Base64: !Sub | #!/bin/bash yum update -y yum install -y httpd.x86_64 systemctl start httpd.service systemctl enable httpd.service echo "Hello World from $(hostname -f)" > /var/www/html/index.html ImageId: ami-id2 InstanceC: Type: AWS::EC2::Instance Properties: DisableApiTermination: true UserData: Fn::Base64: !Sub | #!/bin/bash yum update -y yum install -y httpd.x86_64 systemctl start httpd.service systemctl enable httpd.service echo "Hello World from $(hostname -f)" > /var/www/html/index.html ImageId: ami-id3 InstanceType: m5.2xlarge AvailabilityZone: us-east-1a

Fn::ForEachCollection 中传递非字母数字字符

此示例使用 &{} 语法,该语法允许在 Collection 中传递 IP 地址中的非字母数字字符(./)。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::LanguageExtensions", "Parameters": { "IpAddresses": { "Type": "CommaDelimitedList", "Default": "10.0.2.0/24,10.0.3.0/24,10.0.4.0/24" } }, "Resources": { "VPC": { "Type": "AWS::EC2::VPC", "Properties": { "CidrBlock": "10.0.0.0/16", "EnableDnsSupport": "true", "EnableDnsHostnames": "true" } }, "Fn::ForEach::Subnets": [ "CIDR", { "Ref": "IpAddresses" }, { "Subnet&{CIDR}": { "Type": "AWS::EC2::Subnet", "Properties": { "VpcId": { "Ref": "VPC" }, "CidrBlock": { "Ref": "CIDR" } } } } ] } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::LanguageExtensions Parameters: IpAddresses: Type: CommaDelimitedList Default: '10.0.2.0/24,10.0.3.0/24,10.0.4.0/24' Resources: VPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 EnableDnsSupport: 'true' EnableDnsHostnames: 'true' 'Fn::ForEach::Subnets': - CIDR - !Ref IpAddresses - 'Subnet&{CIDR}': Type: 'AWS::EC2::Subnet' Properties: VpcId: !Ref VPC CidrBlock: !Ref CIDR

转换后的模板将等同于以下模板:

AWSTemplateFormatVersion: 2010-09-09 Transform: AWS::LanguageExtensions Parameters: IpAddresses: Type: CommaDelimitedList Default: '10.0.2.0/24,10.0.3.0/24,10.0.4.0/24' Resources: VPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 EnableDnsSupport: 'true' EnableDnsHostnames: 'true' Subnet1002024: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 10.0.2.0/24 Subnet1003024: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 10.0.3.0/24 Subnet1004024: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 10.0.4.0/24