这些示例演示了如何使用 CloudFormation 模板 Conditions 语法 部分中的 Fn::ForEach
内置函数。
重要
Conditions
必须是列出的第二个属性或更高属性。如果 Conditions
是 Fn::ForEach
的模板片段参数中列出的第一个属性,则堆栈创建将失败。
Resources:
'Fn::ForEach::Topics':
- LogicalId
- !Ref TopicList
- '${LogicalId}':
Condition: !Sub 'TopicCondition${LogicalId}'
Type: AWS::SNS::Topic
Properties:
TopicName: !Sub 'My${LogicalId}'
Conditions
必须作为第二个密钥或更高密钥添加,创建堆栈才能成功:
Resources:
'Fn::ForEach::Topics':
- LogicalId
- !Ref TopicList
- '${LogicalId}':
Type: AWS::SNS::Topic
Condition: !Sub 'TopicCondition${LogicalId}'
Properties:
TopicName: !Sub 'My${LogicalId}'
主题
复制单个条件
此示例使用 CloudFormation 模板 Conditions 语法 部分中的 Fn::ForEach
内置函数来复制具有不同属性的多个相似条件。
JSON
{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::LanguageExtensions",
"Parameters": {
"ParamA": {
"Type": "String",
"AllowedValues": [
"true",
"false"
]
},
"ParamB": {
"Type": "String",
"AllowedValues": [
"true",
"false"
]
},
"ParamC": {
"Type": "String",
"AllowedValues": [
"true",
"false"
]
},
"ParamD": {
"Type": "String",
"AllowedValues": [
"true",
"false"
]
}
},
"Conditions": {
"Fn::ForEach::CheckTrue": [
"Identifier",
["A", "B", "C", "D"],
{
"IsParam${Identifier}Enabled": {
"Fn::Equals": [
{"Ref": {"Fn::Sub": "Param${Identifier}"}},
"true"
]
}
}
]
},
"Resources": {
"WaitConditionHandle": {
"Type": "AWS::CloudFormation::WaitConditionHandle"
}
}
}
YAML
AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::LanguageExtensions'
Parameters:
ParamA:
Type: String
AllowedValues:
- 'true'
- 'false'
ParamB:
Type: String
AllowedValues:
- 'true'
- 'false'
ParamC:
Type: String
AllowedValues:
- 'true'
- 'false'
ParamD:
Type: String
AllowedValues:
- 'true'
- 'false'
Conditions:
'Fn::ForEach::CheckTrue':
- Identifier
- [A, B, C, D]
- 'IsParam${Identifier}Enabled': !Equals
- !Ref
'Fn::Sub': 'Param${Identifier}'
- 'true'
Resources:
WaitConditionHandle:
Type: 'AWS::CloudFormation::WaitConditionHandle'
转换后的模板将等同于以下模板:
AWSTemplateFormatVersion: 2010-09-09
Parameters:
ParamA:
Type: String
AllowedValues:
- 'true'
- 'false'
ParamB:
Type: String
AllowedValues:
- 'true'
- 'false'
ParamC:
Type: String
AllowedValues:
- 'true'
- 'false'
ParamD:
Type: String
AllowedValues:
- 'true'
- 'false'
Conditions:
IsParamAEnabled: !Equals
- !Ref ParamA
- 'true'
IsParamBEnabled: !Equals
- !Ref ParamB
- 'true'
IsParamCEnabled: !Equals
- !Ref ParamC
- 'true'
IsParamDEnabled: !Equals
- !Ref ParamD
- 'true'
Resources:
WaitConditionHandle:
Type: 'AWS::CloudFormation::WaitConditionHandle'