

これは新しい CloudFormation テンプレートリファレンスガイドです。ブックマークとリンクを更新してください。CloudFormation の開始方法については、『[AWS CloudFormation ユーザーガイド](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)』を参照してください。

# `Condition` 属性
<a name="aws-attribute-condition"></a>

条件に基づいて CloudFormation でリソースを作成するには、まずテンプレートの `Conditions` セクションで条件を宣言します。次に、`Condition` キーと条件の論理 ID をリソースの属性として使用します。CloudFormation は、 条件が true に評価される場合にだけ リソースを作成します。これにより、テンプレートで定義した特定の条件またはパラメータに基づいてリソースの作成を制御できます。

テンプレートで条件を初めて使用する場合は、まず「*AWS CloudFormation ユーザーガイド*」の「[CloudFormation テンプレートの Conditions 構文](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html)」トピックを確認することをお勧めします。

**注記**  
条件付きリソースが作成されない場合、そのリソースに依存するリソースも、それぞれの条件に関係なく作成されません。

## 例
<a name="aws-attribute-condition-example"></a>

次のテンプレートでは、Amazon S3 バケットリソースに `Condition` 属性が使用されています。バケットは、`CreateBucket` 条件が `true` に評価された場合にのみ作成されます。

### JSON
<a name="aws-attribute-condition-example.json"></a>

```
{
   "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
<a name="aws-attribute-condition-example.yaml"></a>

```
 1. AWSTemplateFormatVersion: 2010-09-09
 2. Parameters:
 3.   EnvType:
 4.     Type: String
 5.     AllowedValues:
 6.       - prod
 7.       - dev
 8.     Default: dev
 9.     Description: Environment type
10. Conditions:
11.   CreateBucket: !Equals [!Ref EnvType, prod]
12. Resources:
13.   MyBucket:
14.     Type: AWS::S3::Bucket
15.     Condition: CreateBucket
16.     Properties:
17.       BucketName: !Sub mybucket-${EnvType}
```

## 複数の条件の使用
<a name="aws-attribute-condition-multiple"></a>

[`Fn::And`](intrinsic-function-reference-conditions.md#intrinsic-function-reference-conditions-and)、[`Fn::Or`](intrinsic-function-reference-conditions.md#intrinsic-function-reference-conditions-or)、[`Fn::Not`](intrinsic-function-reference-conditions.md#intrinsic-function-reference-conditions-not) などの組み込み関数を使用して複数の条件を組み合わせ、より複雑な条件付きロジックを作成できます。

### JSON
<a name="aws-attribute-condition-multiple.json"></a>

```
{
   "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
<a name="aws-attribute-condition-multiple.yaml"></a>

```
 1. AWSTemplateFormatVersion: 2010-09-09
 2. Parameters:
 3.   EnvType:
 4.     Type: String
 5.     AllowedValues:
 6.       - prod
 7.       - test
 8.       - dev
 9.     Default: dev
10.     Description: Environment type
11.   CreateResources:
12.     Type: String
13.     AllowedValues:
14.       - 'true'
15.       - 'false'
16.     Default: 'true'
17.     Description: Create resources flag
18. Conditions:
19.   IsProd: !Equals [!Ref EnvType, prod]
20.   IsTest: !Equals [!Ref EnvType, test]
21.   CreateResourcesFlag: !Equals [!Ref CreateResources, 'true']
22.   CreateProdResources: !And
23.     - !Condition IsProd
24.     - !Condition CreateResourcesFlag
25.   CreateTestOrDevResources: !And
26.     - !Or
27.       - !Condition IsTest
28.       - !Not [!Condition IsProd]
29.     - !Condition CreateResourcesFlag
30. Resources:
31.   ProdBucket:
32.     Type: AWS::S3::Bucket
33.     Condition: CreateProdResources
34.     Properties:
35.       BucketName: !Sub prod-bucket-${AWS::StackName}
36.   TestDevBucket:
37.     Type: AWS::S3::Bucket
38.     Condition: CreateTestOrDevResources
39.     Properties:
40.       BucketName: !Sub ${EnvType}-bucket-${AWS::StackName}
```

## 条件での `AWS::AccountId` の使用
<a name="aws-attribute-condition-account"></a>

条件で `AWS::AccountId` などの擬似パラメータを使用して、スタックがデプロイされている AWS アカウント に基づいてリソースを作成できます。これは、マルチアカウントデプロイや、特定のアカウントを特定のリソースの受信から除外する必要がある場合に役立ちます。擬似パラメータの詳細については、「*AWS CloudFormation ユーザーガイド*」の「[擬似パラメータを使用して AWS 値を取得する](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html)」を参照してください。

### JSON
<a name="aws-attribute-condition-account.json"></a>

```
{
   "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
<a name="aws-attribute-condition-account.yaml"></a>

```
 1. AWSTemplateFormatVersion: 2010-09-09
 2. Conditions:
 3.   ExcludeAccount1: !Not [!Equals [!Ref 'AWS::AccountId', '111111111111']]
 4.   ExcludeAccount2: !Not [!Equals [!Ref 'AWS::AccountId', '222222222222']]
 5.   ExcludeBothAccounts: !And
 6.     - !Condition ExcludeAccount1
 7.     - !Condition ExcludeAccount2
 8. Resources:
 9.   StandardBucket:
10.     Type: AWS::S3::Bucket
11.     Properties:
12.       BucketName: !Sub standard-bucket-${AWS::StackName}
13.   RestrictedResource:
14.     Type: AWS::SNS::Topic
15.     Condition: ExcludeBothAccounts
16.     Properties:
17.       TopicName: !Sub restricted-topic-${AWS::StackName}
```