

새로운 *CloudFormation 템플릿 참조 안내서*입니다. 북마크와 링크를 업데이트하세요. CloudFormation을 시작하는 데 도움이 필요한 경우 [AWS CloudFormation 사용 설명서](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)를 참조하세요.

# `Condition` 속성
<a name="aws-attribute-condition"></a>

조건에 따라 CloudFormation에서 리소스를 생성하려면 먼저 템플릿의 `Conditions` 섹션에서 조건을 선언합니다. 그런 다음 조건의 논리적 ID와 함께 `Condition` 키를 리소스의 속성으로 사용합니다. 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>

다음 템플릿에는 `Condition` 속성이 있는 Amazon S3 버킷 리소스가 포함되어 있습니다. 버킷은 `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}
```