CloudFormation 範本Conditions語法 - AWS CloudFormation

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

CloudFormation 範本Conditions語法

選用的 Conditions 區段包含陳述式來定義在哪些情況下建立或設定實體。例如,您可以建立條件並將其與資源或輸出建立關聯,以便 CloudFormation 僅在條件為 true 時建立資源或輸出。同樣地,您可以將條件與屬性建立關聯,以便 CloudFormation 只有在條件為 true 時,才會將屬性設定為特定值。如果條件為 false,CloudFormation 會將 屬性設定為您指定的替代值。

當您想要重複使用範本在不同內容中建立資源時,您可以使用條件,例如測試與生產環境。例如,您可以在範本中新增接受 prodtest做為輸入的輸入EnvironmentType參數。對於prod環境,您可以包含具有特定功能的 EC2 執行個體,而對於test環境,您可以使用減少的功能來節省成本。此條件定義可讓您定義要建立哪些資源,以及如何為每個環境類型設定這些資源。

語法

Conditions 區段由索引鍵名稱 Conditions 組成。每個條件宣告都包含邏輯 ID 和一或多個內部函數。

JSON

"Conditions": { "LogicalConditionName1": { "Intrinsic function": ...[ }, "LogicalConditionName2": { "Intrinsic function": ... } }

YAML

Conditions: LogicalConditionName1: Intrinsic function: ... LogicalConditionName2: Intrinsic function: ...

條件的運作方式

若要使用條件,請依照下列步驟進行:

  1. 新增參數定義 – 定義您的條件將在範本的 Parameters區段中評估的輸入。條件會根據這些輸入參數值評估為 true 或 false。請注意,虛擬參數會自動提供,而且不需要 Parameters區段中的明確定義。如需這些虛擬參數的詳細資訊,請參閱使用虛擬參數取得 AWS 值

  2. 新增條件定義 – 使用 Fn::If或 等內部函數在 Conditions區段中定義條件Fn::Equals。這些條件決定 CloudFormation 何時建立相關聯的資源。這些條件可以根據:

    • 輸入或虛擬參數值

    • 其他條件

    • 映射值

    不過,您無法在條件中參考資源邏輯 IDs 或其屬性。

  3. 條件與資源或輸出建立關聯 – 使用 Condition金鑰和條件的邏輯 ID 參考資源或輸出中的條件。或者,在範本的其他部分 (例如屬性值) Fn::If中使用 根據條件來設定值。如需詳細資訊,請參閱使用 Condition金鑰

CloudFormation 會在建立或更新堆疊時評估條件。CloudFormation 會建立與 true 條件相關聯的實體,並忽略與 false 條件相關聯的實體。CloudFormation 也會在每次堆疊更新期間重新評估這些條件,然後再修改任何資源。與 true 條件保持關聯的實體會更新,而與 false 條件相關聯的實體則會刪除。

重要

在更新堆疊期間,您無法自行更新條件。唯有涵蓋新增、修改或刪除資源等變更作業時,才能更新條件。

條件內部函數

您可以使用下列內部函數來定義條件:

注意

只有在範本的 Resources 區段和 Outputs 區段的中繼資料屬性、更新政策屬性和屬性值中,才支援 Fn::If

使用 Condition金鑰

定義條件後,您可以使用 OutputsCondition金鑰將其套用至範本中的多個位置,例如 Resources和 。Condition 金鑰參考條件的邏輯名稱,並傳回指定條件的評估結果。

將條件與資源建立關聯

若要有條件地建立資源,請將 Condition金鑰和條件的邏輯 ID 做為資源的屬性新增。CloudFormation 只會在條件評估為 true 時建立資源。

JSON

"NewVolume" : { "Type" : "AWS::EC2::Volume", "Condition" : "IsProduction", "Properties" : { "Size" : "100", "AvailabilityZone" : { "Fn::GetAtt" : [ "EC2Instance", "AvailabilityZone" ]} } }

YAML

NewVolume: Type: AWS::EC2::Volume Condition: IsProduction Properties: Size: 100 AvailabilityZone: !GetAtt EC2Instance.AvailabilityZone

將條件與輸出建立關聯

您也可以將條件與輸出建立關聯。CloudFormation 只會在關聯的條件評估為 true 時建立輸出。

JSON

"Outputs" : { "VolumeId" : { "Condition" : "IsProduction", "Value" : { "Ref" : "NewVolume" } } }

YAML

Outputs: VolumeId: Condition: IsProduction Value: !Ref NewVolume

其他條件中的參考條件

Conditions區段中定義條件時,您可以使用 Condition金鑰參考其他條件。這可讓您結合多個條件來建立更複雜的條件式邏輯。

在下列範例中,只有在 IsProductionIsProdAndFeatureEnabled條件評估為 true 時,IsFeatureEnabled條件才會評估為 true。

JSON

"Conditions": { "IsProduction" : {"Fn::Equals" : [{"Ref" : "Environment"}, "prod"]}, "IsFeatureEnabled" : { "Fn::Equals" : [{"Ref" : "FeatureFlag"}, "enabled"]}, "IsProdAndFeatureEnabled" : { "Fn::And" : [ {"Condition" : "IsProduction"}, {"Condition" : "IsFeatureEnabled"} ] } }

YAML

Conditions: IsProduction: !Equals [!Ref Environment, "prod"] IsFeatureEnabled: !Equals [!Ref FeatureFlag, "enabled"] IsProdAndFeatureEnabled: !And - !Condition IsProduction - !Condition IsFeatureEnabled

使用 有條件地傳回屬性值 Fn::If

如需更精細的控制,您可以使用Fn::If內部 函數來有條件地傳回資源或輸出中的兩個屬性值之一。此函數會評估條件,如果條件為 true,則傳回一個值,如果條件為 false,則傳回另一個值。

條件式屬性值

下列範例示範如何根據環境條件設定 EC2 執行個體類型。如果IsProduction條件評估為 true,執行個體類型會設定為 c5.xlarge。否則,它會設定為 t3.small

JSON
"Properties" : { "InstanceType" : { "Fn::If" : [ "IsProduction", "c5.xlarge", "t3.small" ] } }
YAML
Properties: InstanceType: !If - IsProduction - c5.xlarge - t3.small

條件式屬性移除

您也可以使用AWS::NoValue虛擬參數做為傳回值,在條件為 false 時移除對應的屬性。

JSON
"DBSnapshotIdentifier" : { "Fn::If" : [ "UseDBSnapshot", {"Ref" : "DBSnapshotName"}, {"Ref" : "AWS::NoValue"} ] }
YAML
DBSnapshotIdentifier: !If - UseDBSnapshot - !Ref DBSnapshotName - !Ref "AWS::NoValue"

範例

以環境為基礎的資源建立

以下範例會佈建 EC2 執行個體,並且只有在環境類型為 時,才會有條件地建立和連接新的 EBS 磁碟區prod。如果環境是 test,它們只會建立沒有額外磁碟區的 EC2 執行個體。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Parameters": { "EnvType": { "Description": "Environment type", "Default": "test", "Type": "String", "AllowedValues": [ "prod", "test" ], "ConstraintDescription": "must specify prod or test" } }, "Conditions": { "IsProduction": { "Fn::Equals": [ { "Ref": "EnvType" }, "prod" ] } }, "Resources": { "EC2Instance": { "Type": "AWS::EC2::Instance", "Properties": { "ImageId": "ami-1234567890abcdef0", "InstanceType": "c5.xlarge" } }, "MountPoint": { "Type": "AWS::EC2::VolumeAttachment", "Condition": "IsProduction", "Properties": { "InstanceId": { "Ref": "EC2Instance" }, "VolumeId": { "Ref": "NewVolume" }, "Device": "/dev/sdh" } }, "NewVolume": { "Type": "AWS::EC2::Volume", "Condition": "IsProduction", "Properties": { "Size": 100, "AvailabilityZone": { "Fn::GetAtt": [ "EC2Instance", "AvailabilityZone" ] } } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Parameters: EnvType: Description: Environment type Default: test Type: String AllowedValues: - prod - test ConstraintDescription: must specify prod or test Conditions: IsProduction: !Equals - !Ref EnvType - prod Resources: EC2Instance: Type: AWS::EC2::Instance Properties: ImageId: ami-1234567890abcdef0 InstanceType: c5.xlarge MountPoint: Type: AWS::EC2::VolumeAttachment Condition: IsProduction Properties: InstanceId: !Ref EC2Instance VolumeId: !Ref NewVolume Device: /dev/sdh NewVolume: Type: AWS::EC2::Volume Condition: IsProduction Properties: Size: 100 AvailabilityZone: !GetAtt - EC2Instance - AvailabilityZone

多條件資源佈建

以下範例會在提供儲存貯體名稱時有條件地建立 S3 儲存貯體,並只在環境設定為 時連接儲存貯體政策prod。如果未指定儲存貯體名稱或環境為 test,則不會建立資源。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Parameters": { "EnvType": { "Type": "String", "AllowedValues": [ "prod", "test" ] }, "BucketName": { "Default": "", "Type": "String" } }, "Conditions": { "IsProduction": { "Fn::Equals": [ { "Ref": "EnvType" }, "prod" ] }, "CreateBucket": { "Fn::Not": [ { "Fn::Equals": [ { "Ref": "BucketName" }, "" ] } ] }, "CreateBucketPolicy": { "Fn::And": [ { "Condition": "IsProduction" }, { "Condition": "CreateBucket" } ] } }, "Resources": { "Bucket": { "Type": "AWS::S3::Bucket", "Condition": "CreateBucket", "Properties": { "BucketName": { "Ref": "BucketName" } } }, "Policy": { "Type": "AWS::S3::BucketPolicy", "Condition": "CreateBucketPolicy", "Properties": { "Bucket": { "Ref": "Bucket" }, "PolicyDocument": { ... } } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Parameters: EnvType: Type: String AllowedValues: - prod - test BucketName: Default: '' Type: String Conditions: IsProduction: !Equals - !Ref EnvType - prod CreateBucket: !Not - !Equals - !Ref BucketName - '' CreateBucketPolicy: !And - !Condition IsProduction - !Condition CreateBucket Resources: Bucket: Type: AWS::S3::Bucket Condition: CreateBucket Properties: BucketName: !Ref BucketName Policy: Type: AWS::S3::BucketPolicy Condition: CreateBucketPolicy Properties: Bucket: !Ref Bucket PolicyDocument: ...

在此範例中, CreateBucketPolicy條件示範如何使用 Condition金鑰參考其他條件。只有在 IsProductionCreateBucket條件都評估為 true 時,才會建立政策。

注意

如需使用條件的更複雜範例,請參閱 AWS CloudFormation 範本參考指南中的 Condition 屬性主題。