Exemples de Fn::ForEach dans la section consacrée aux Resources - AWS CloudFormation

Il s'agit du nouveau guide AWS CloudFormation de référence des modèles. Veuillez mettre à jour vos favoris et vos liens. Pour obtenir de l'aide pour démarrer CloudFormation, consultez le guide de AWS CloudFormation l'utilisateur.

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Exemples de Fn::ForEach dans la section consacrée aux Resources

Ces exemples illustrent l'utilisation de la fonction intrinsèque Fn::ForEach dans la section Resources. Pour plus d'informations sur cette section, consultez la section Ressources du guide de AWS CloudFormation l'utilisateur.

Répliquer une ressource Amazon SNS

Cet exemple d'extrait de code renvoie une liste de quatre rubriques Amazon SNS, avec l'ID logique correspondant aux éléments de la collection (Success, Failure, Timeout, Unknown) et avec des correspondances TopicName et FifoTopic définies sur true.

Note

Pour les modèles qui doivent fonctionner à la fois avec le FIFO et avec les sujets standard, vous pouvez utiliser la DisplayName propriété au lieu deTopicName. Cela permet de CloudFormation générer automatiquement des noms de sujets avec le .fifo suffixe approprié quand FifoTopic c'est true le cas. Il suffit de le TopicName remplacer par DisplayName: !Ref TopicName dans la Properties section.

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

Le modèle transformé sera équivalent au modèle suivant :

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

Répliquer une ressource Amazon DynamoDB

Cet exemple d'extrait de code crée quatre ressources AWS::DynamoDB::Table dotées de noms tels que Points, Score, etc.

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'

Le modèle transformé sera équivalent au modèle suivant :

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'

Répliquer plusieurs ressources

Cet exemple crée plusieurs instances de AWS::EC2::NatGatewayet AWS : EC2 : :EIP en utilisant une convention de dénomination de. "{ResourceType}${Identifier}" Vous pouvez déclarer plusieurs types de ressources dans une seule boucle Fn::ForEach pour tirer parti d'un identifiant unique.

Des valeurs uniques pour chaque élément de la collection sont définies dans la Mappings section, où la fonction Fn::FindInMap intrinsèque est utilisée pour référencer la valeur correspondante. Si vous Fn::FindInMap ne trouvez pas l'identifiant correspondant, la Condition propriété ne sera pas définie comme résolution sur!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

Le modèle transformé sera équivalent au modèle suivant :

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

Répliquer plusieurs ressources à l'aide de boucles Fn::ForEach imbriquées

Cet exemple utilise des boucles Fn::ForEach imbriquées pour mapper trois ressources (AWS::EC2::NetworkAcl, AWS::EC2::Subnet et AWS::EC2::SubnetNetworkAclAssociation) entre elles.

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'

Le modèle transformé sera équivalent au modèle suivant :

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

Référencer les propriétés répliquées d'une ressource Amazon EC2

Cet exemple utilise la fonction intrinsèque Fn::ForEach pour faire référence aux ressources AWS::EC2::Instance répliquées.

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]

Le modèle transformé sera équivalent au modèle suivant :

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]

Répliquer les propriétés d'une ressource Amazon EC2

Cet exemple utilise la fonction intrinsèque Fn::ForEach pour répéter certaines propriétés telles que ImageId, InstanceType et AvailabilityZone à une ressource 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'}

Le modèle transformé sera équivalent au modèle suivant :

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

Transmission de caractères non alphanumériques dans le formulaire CollectionFn::ForEach

Cet exemple utilise la &{} syntaxe qui permet de transmettre les caractères non alphanumériques (.et/) des adresses IP dans le. Collection

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

Le modèle transformé sera équivalent au modèle suivant :

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