이 예제에서는 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'