새로운 AWS CloudFormation 템플릿 참조 안내서입니다. 북마크와 링크를 업데이트하세요. CloudFormation을 시작하는 데 도움이 필요한 경우 AWS CloudFormation 사용 설명서를 참조하세요.
Condition
속성
조건에 따라 CloudFormation에서 리소스를 생성하려면 먼저 템플릿의 Conditions
섹션에서 조건을 선언합니다. 그런 다음 조건의 논리적 ID와 함께 Condition
키를 리소스의 속성으로 사용합니다. CloudFormation은 조건이 true로 평가될 때만 리소스를 생성합니다. 이를 통해 템플릿에서 정의한 특정 기준 또는 파라미터를 기반으로 리소스 생성을 제어할 수 있습니다.
템플릿에서 조건을 처음 사용하는 경우 먼저 AWS CloudFormation 사용자 가이드의 CloudFormation 템플릿 Conditions 구문 주제를 검토하는 것이 좋습니다.
참고
조건이 있는 리소스가 생성되지 않은 경우 해당 리소스에 의존하는 모든 리소스도 자체 조건과 관계없이 생성되지 않습니다.
예시
다음 템플릿에는 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}