选择您的 Cookie 首选项

我们使用必要 Cookie 和类似工具提供我们的网站和服务。我们使用性能 Cookie 收集匿名统计数据,以便我们可以了解客户如何使用我们的网站并进行改进。必要 Cookie 无法停用,但您可以单击“自定义”或“拒绝”来拒绝性能 Cookie。

如果您同意,AWS 和经批准的第三方还将使用 Cookie 提供有用的网站功能、记住您的首选项并显示相关内容,包括相关广告。要接受或拒绝所有非必要 Cookie,请单击“接受”或“拒绝”。要做出更详细的选择,请单击“自定义”。

Conditions 部分中的 Fn::ForEach 示例

聚焦模式
Conditions 部分中的 Fn::ForEach 示例 - AWS CloudFormation
筛选器视图

这些示例演示了如何使用 CloudFormation 模板 Conditions 语法 部分中的 Fn::ForEach 内置函数。

重要

Conditions 必须是列出的第二个属性或更高属性。如果 ConditionsFn::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'

本页内容

下一主题:

Fn::GetAtt

上一主题:

Outputs 部分
隐私网站条款Cookie 首选项
© 2025, Amazon Web Services, Inc. 或其附属公司。保留所有权利。