【问题标题】:Check preconditions when creating a CloudFormation Stack创建 CloudFormation 堆栈时检查先决条件
【发布时间】:2016-12-19 22:28:03
【问题描述】:

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

例如,我想将 Stack 的创建限制在 us-east-1 区域。尽管以下代码有效,但 [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"
    }
  }
}

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

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

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

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

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

    “参数”:{ “实例类型参数”:{ “类型”:“字符串”, “默认”:“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

    很多可能性。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-12
      • 2018-06-05
      • 1970-01-01
      • 2019-12-29
      • 2018-11-24
      • 2018-10-01
      • 2019-12-13
      • 2020-11-09
      相关资源
      最近更新 更多