2016-11-28 68 views
0

如果在创建CloudFormation堆栈时未满足某些先决条件,是否可以检查先决条件并引发错误?创建CloudFormation堆栈时检查先决条件

例如,我想限制创建一个堆栈到us-east-1 Region。虽然下面的代码工作中,[FAILED]消息违背了协议栈的最终状态,这始终是CREATE_COMPLETE不分地区:

{ 
    "Conditions": { 
    "ValidRegion": { 
     "Fn::Equals": [ 
     { 
      "Ref": "AWS::Region" 
     }, 
     "us-east-1" 
     ] 
    } 
    }, 
    "Description": "Certificate for Global services", 
    "Outputs": { 
    "GlobalCertificateArn": { 
     "Description": "Certificate ARN", 
     "Value": { 
     "Fn::If": [ 
      "ValidRegion", 
      { 
      "Ref": "GlobalCertificate" 
      }, 
      "[FAILED] Failed to create certificate for Global services. Create this stack in us-east-1." 
     ] 
     } 
    } 
    }, 
    "Parameters": { 
    "Domain": { 
     "Description": "Domain name of this website", 
     "Type": "String" 
    } 
    }, 
    "Resources": { 
    "GlobalCertificate": { 
     "Condition": "ValidRegion", 
     "Properties": { 
     "DomainName": { 
      "Ref": "Domain" 
     } 
     }, 
     "Type": "AWS::CertificateManager::Certificate" 
    } 
    } 
} 

有没有更好的方式来产生一个错误?

回答

0

对于您的示例,最好使用AWS伪参数。只需创建一个条件来检查“AWS :: Region”是否等于us-east-1。

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html

真的不能想出一个通用的方法来检查一切,它取决于形势。例如,如果你正在处理参数,你可以使用正则表达式或设置允许值:

“参数”:{ “InstanceTypeParameter”:{ “类型”:“字符串”, “默认”: “t2.micro”, “AllowedValues”:[“t2.micro”,“m1.small”,“m1.large”], “Description”:“输入t1.micro,m1.small或m1.large 。默认是t1.micro。“ }}

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html

或者你可以结合映射,其中包含你的期望值,与条件。

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/mappings-section-structure.html

很多的可能性。 :)