これは新しい AWS CloudFormation テンプレートリファレンスガイドです。ブックマークとリンクを更新してください。CloudFormation の開始方法については、『AWS CloudFormation ユーザーガイド』を参照してください。
Condition
属性
条件に基づいて CloudFormation でリソースを作成するには、まずテンプレートの Conditions
セクションで条件を宣言します。次に、Condition
キーと条件の論理 ID をリソースの属性として使用します。CloudFormation は、 条件が true に評価される場合にだけ リソースを作成します。これにより、テンプレートで定義した特定の条件またはパラメータに基づいてリソースの作成を制御できます。
テンプレートで条件を初めて使用する場合は、まず「AWS CloudFormation ユーザーガイド」の「CloudFormation テンプレートの Conditions 構文」トピックを確認することをお勧めします。
注記
条件付きリソースが作成されない場合、そのリソースに依存するリソースも、それぞれの条件に関係なく作成されません。
例
次のテンプレートでは、Amazon S3 バケットリソースに Condition
属性が使用されています。バケットは、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}