繁体   English   中英

如何在同一个 CloudFormation 模板中声明多个相似的资源组?

[英]How to declare multiple and similar groups of resources in the same CloudFormation template?

我有一个具有三层的服务,每一层都有一个包含一些实例的自动缩放组。 这些层的不同(在启动配置、实例数量等方面)由单个参数(“角色”)控制。 因此,我用参数调用模板如下:

{
  ...
  "Parameters": {
    ...
    "Role": {
      "AllowedValues": [
        "inbound",
        "filter",
        "outbound"
      ],
      ...
    },
  },
  "Conditions" : {
    "IsStaging" : { "Fn::Equals" : [ { "Ref": "Environment" }, "eu-staging"] },   
    "IsInbound" : { "Fn::Equals" : [ { "Ref": "Role" }, "inbound"] },
    "IsFilter" : { "Fn::Equals" : [ { "Ref": "Role" }, "filter"] },
    "IsOutbound" : { "Fn::Equals" : [ { "Ref": "Role" }, "outbound"] }
  },
  "Resources": {
    ...
    "SecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        ...
        "SecurityGroupEgress": [
          {"Fn::If": ["IsFilter", { "CidrIp": "10.243.74.0/24", "IpProtocol": "tcp", "FromPort": "9094", "ToPort": "9094" }, { "Ref": "AWS::NoValue" } ]}
        ],
        "Tags": [
          {
            "Key": "Name",
            "Value": { "Fn::Join" : [ "-", [ "smtp-service-security-group", {"Ref": "Role"} ] ] }
          },
          ...
        ],
      }
    },
  }
}

现在我想在同一个 CloudFormation 模板中声明这三个层(以便它与现有管道作业透明地集成,并受益于 CloudFormation 功能,例如回滚)。 此外,我想避免重写三次资源,每层一次,如下所示:

{
  ...
  "Parameters": {
    ...
    "Role": {
      "AllowedValues": [
        "inbound",
        "filter",
        "outbound"
      ],
      ...
    },
  },
  "Conditions" : {
    "IsStaging" : { "Fn::Equals" : [ { "Ref": "Environment" }, "eu-staging"] },   
    "IsInbound" : { "Fn::Equals" : [ { "Ref": "Role" }, "inbound"] },
    "IsFilter" : { "Fn::Equals" : [ { "Ref": "Role" }, "filter"] },
    "IsOutbound" : { "Fn::Equals" : [ { "Ref": "Role" }, "outbound"] }
  },
  "Resources": {
    ...
    "SecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        ...
        "SecurityGroupEgress": [
          {"Fn::If": ["IsInbound", { "CidrIp": "10.243.74.0/24", "IpProtocol": "tcp", "FromPort": "9094", "ToPort": "9094" }, { "Ref": "AWS::NoValue" } ]}
        ],
        "Tags": [
          {
            "Key": "Name",
            "Value": { "Fn::Join" : [ "-", [ "smtp-service-security-group", {"Ref": "Role"} ] ] }
          },
          ...
        ],
      }
    },

    ...
    "SecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        ...
        "SecurityGroupEgress": [
          {"Fn::If": ["IsFilter", { "CidrIp": "10.243.74.0/24", "IpProtocol": "tcp", "FromPort": "9094", "ToPort": "9094" }, { "Ref": "AWS::NoValue" } ]}
        ],
        "Tags": [
          {
            "Key": "Name",
            "Value": { "Fn::Join" : [ "-", [ "smtp-service-security-group", {"Ref": "Role"} ] ] }
          },
          ...
        ],
      }
    },

    ...
    "SecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        ...
        "SecurityGroupEgress": [
          {"Fn::If": ["IsOutbound", { "CidrIp": "10.243.74.0/24", "IpProtocol": "tcp", "FromPort": "9094", "ToPort": "9094" }, { "Ref": "AWS::NoValue" } ]}
        ],
        "Tags": [
          {
            "Key": "Name",
            "Value": { "Fn::Join" : [ "-", [ "smtp-service-security-group", {"Ref": "Role"} ] ] }
          },
          ...
        ],
      }
    },
  }
}

我怎样才能以不需要我为三层重写相同代码的方式做到这一点?

如果您只想使用一个模板,则不能在没有CloudFormation {CFN) 宏的情况下执行此操作。

处理重复的常用方法是使用嵌套堆栈 这将需要您将这三个服务的公共代码放在一个单独的模板中,并且它们将它们三次包含在一个父模板中,并带有它们的特定参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM