這是新的 AWS CloudFormation 範本參考指南。請更新您的書籤和連結。如需開始使用 CloudFormation 的說明,請參閱 AWS CloudFormation 使用者指南。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Condition
屬性
若要根據條件在 CloudFormation 中建立資源,請先在範本的 Conditions
區段中宣告您的條件。然後,使用 Condition
金鑰以及條件的邏輯 ID 做為資源上的屬性。CloudFormation 只會在條件評估為 true 時建立資源。這可讓您根據範本中定義的特定條件或參數來控制資源建立。
如果您是第一次在範本中使用條件,建議您先檢閱AWS CloudFormation 《 使用者指南》中的 CloudFormation 範本條件語法主題。
注意
如果未建立具有 條件的資源,則不會建立任何依賴該資源的資源,無論其自己的條件為何。
範例
下列範本包含具有 Condition
屬性的 Amazon S3 儲存貯體資源。只有在CreateBucket
條件評估為 時,才會建立儲存貯體true
。
JSON
{ "AWSTemplateFormatVersion" : "2010-09-09", "Parameters" : { "EnvType" : { "Type" : "String", "AllowedValues" : ["prod", "dev"], "Default" : "dev", "Description" : "Environment type" } }, "Conditions" : { "CreateBucket" : {"Fn::Equals" : [{"Ref" : "EnvType"}, "prod"]} }, "Resources" : { "MyBucket" : { "Type" : "AWS::S3::Bucket", "Condition" : "CreateBucket", "Properties" : { "BucketName" : {"Fn::Join" : ["-", ["mybucket", {"Ref" : "EnvType"}]]} } } } }
YAML
AWSTemplateFormatVersion: 2010-09-09 Parameters: EnvType: Type: String AllowedValues: - prod - dev Default: dev Description: Environment type Conditions: CreateBucket: !Equals [!Ref EnvType, prod] Resources: MyBucket: Type: AWS::S3::Bucket Condition: CreateBucket Properties: BucketName: !Sub mybucket-${EnvType}
使用多個條件
您可以使用 Fn::And、 Fn::Or和 等內部函數來結合多個條件Fn::Not,以建立更複雜的條件式邏輯。
JSON
{ "AWSTemplateFormatVersion" : "2010-09-09", "Parameters" : { "EnvType" : { "Type" : "String", "AllowedValues" : ["prod", "test", "dev"], "Default" : "dev", "Description" : "Environment type" }, "CreateResources" : { "Type" : "String", "AllowedValues" : ["true", "false"], "Default" : "true", "Description" : "Create resources flag" } }, "Conditions" : { "IsProd" : {"Fn::Equals" : [{"Ref" : "EnvType"}, "prod"]}, "IsTest" : {"Fn::Equals" : [{"Ref" : "EnvType"}, "test"]}, "CreateResourcesFlag" : {"Fn::Equals" : [{"Ref" : "CreateResources"}, "true"]}, "CreateProdResources" : {"Fn::And" : [{"Condition" : "IsProd"}, {"Condition" : "CreateResourcesFlag"}]}, "CreateTestOrDevResources" : {"Fn::And" : [{"Fn::Or" : [{"Condition" : "IsTest"}, {"Fn::Not" : [{"Condition" : "IsProd"}]}]}, {"Condition" : "CreateResourcesFlag"}]} }, "Resources" : { "ProdBucket" : { "Type" : "AWS::S3::Bucket", "Condition" : "CreateProdResources", "Properties" : { "BucketName" : {"Fn::Join" : ["-", ["prod-bucket", {"Ref" : "AWS::StackName"}]]} } }, "TestDevBucket" : { "Type" : "AWS::S3::Bucket", "Condition" : "CreateTestOrDevResources", "Properties" : { "BucketName" : {"Fn::Join" : ["-", [{"Ref" : "EnvType"}, "bucket", {"Ref" : "AWS::StackName"}]]} } } } }
YAML
AWSTemplateFormatVersion: 2010-09-09 Parameters: EnvType: Type: String AllowedValues: - prod - test - dev Default: dev Description: Environment type CreateResources: Type: String AllowedValues: - 'true' - 'false' Default: 'true' Description: Create resources flag Conditions: IsProd: !Equals [!Ref EnvType, prod] IsTest: !Equals [!Ref EnvType, test] CreateResourcesFlag: !Equals [!Ref CreateResources, 'true'] CreateProdResources: !And - !Condition IsProd - !Condition CreateResourcesFlag CreateTestOrDevResources: !And - !Or - !Condition IsTest - !Not [!Condition IsProd] - !Condition CreateResourcesFlag Resources: ProdBucket: Type: AWS::S3::Bucket Condition: CreateProdResources Properties: BucketName: !Sub prod-bucket-${AWS::StackName} TestDevBucket: Type: AWS::S3::Bucket Condition: CreateTestOrDevResources Properties: BucketName: !Sub ${EnvType}-bucket-${AWS::StackName}
在 條件AWS::AccountId
中使用
您可以使用 條件AWS::AccountId
中的虛擬參數,根據要部署堆疊 AWS 帳戶 的 來建立資源。這對於多帳戶部署或當您需要排除特定帳戶接收特定資源時非常有用。如需虛擬參數的詳細資訊,請參閱AWS CloudFormation 《 使用者指南》中的使用虛擬參數取得 AWS 值。
JSON
{ "AWSTemplateFormatVersion" : "2010-09-09", "Conditions" : { "ExcludeAccount1" : {"Fn::Not" : [{"Fn::Equals" : [{"Ref" : "AWS::AccountId"}, "111111111111"]}]}, "ExcludeAccount2" : {"Fn::Not" : [{"Fn::Equals" : [{"Ref" : "AWS::AccountId"}, "222222222222"]}]}, "ExcludeBothAccounts" : {"Fn::And" : [{"Condition" : "ExcludeAccount1"}, {"Condition" : "ExcludeAccount2"}]} }, "Resources" : { "StandardBucket" : { "Type" : "AWS::S3::Bucket", "Properties" : { "BucketName" : {"Fn::Join" : ["-", ["standard-bucket", {"Ref" : "AWS::StackName"}]]} } }, "RestrictedResource" : { "Type" : "AWS::SNS::Topic", "Condition" : "ExcludeBothAccounts", "Properties" : { "TopicName" : {"Fn::Join" : ["-", ["restricted-topic", {"Ref" : "AWS::StackName"}]]} } } } }
YAML
AWSTemplateFormatVersion: 2010-09-09 Conditions: ExcludeAccount1: !Not [!Equals [!Ref 'AWS::AccountId', '111111111111']] ExcludeAccount2: !Not [!Equals [!Ref 'AWS::AccountId', '222222222222']] ExcludeBothAccounts: !And - !Condition ExcludeAccount1 - !Condition ExcludeAccount2 Resources: StandardBucket: Type: AWS::S3::Bucket Properties: BucketName: !Sub standard-bucket-${AWS::StackName} RestrictedResource: Type: AWS::SNS::Topic Condition: ExcludeBothAccounts Properties: TopicName: !Sub restricted-topic-${AWS::StackName}