创建CloudFormation堆栈时检查前提条件

时间:2016-11-28 09:05:03

标签: amazon-web-services amazon-cloudformation

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

例如,我想将Stack的创建限制为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"
    }
  }
}

有没有更好的方法来引发错误?

1 个答案:

答案 0 :(得分:0)

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

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

无法真正想出一种检查一切的通用方法,这取决于具体情况。例如,如果您正在处理参数,则可以使用正则表达式或设置允许值:

“参数”:{   “InstanceTypeParameter”:{     “Type”:“String”,     “默认”:“t2.micro”,     “AllowedValues”:[“t2.micro”,“m1.small”,“m1.large”],     “描述”:“输入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

很多可能性。 :)