云编排服务COS

资源(Resources)

描述堆栈中每一个资源的属性和依赖关系。一个资源可以被其他资源和 Outputs 所引用。

语法

资源部分由资源 ID 和资源描述组成。所有资源描述都被括在括号里。如果您声明多个资源,则可用逗号将它们分隔开。以下代码段描述了 Resources 的语法结构:

"Resources" : {
 "Resource1" : {
 "Type" : "资源类型",
 "Condition": "是否创建此资源的条件",
 "Properties" : {
    资源属性
 }
 },
 "resource2" : {
 "Type" : "资源类型",
 "Condition": "是否创建此资源的条件",
 "Properties" : {
    资源属性描述
 }
 }
}

资源 ID

资源 ID 在模板中具有唯一性。可使用资源 ID 在模板的其他部分中引用资源。

资源类型

资源类型标识您正在声明的资源的类型。有关所有资源的列表,请参阅 云编排资源。

资源属性

资源属性是可以为资源指定的附加选项。例如,对于每个吉利云BCC实例,必须为该实例指定一个 Image ID。如以下代码段所示:

"Resources" : {
 "bcc" : {
 "Type" : "BCE::BCC::Instance",
 "Properties" : {
    "ImageId" : "m-kDwAHHal"
 }
 }
}

如果资源不需要声明任何属性,那么您可以忽略该资源的属性部分。

Condition

使用 Condition 属性可以指定是否需要正真创建此资源。当只有 Condition 所指定的条件值为 true 时才创建此资源。

如以下代码段所示,根据 Env 的值判断否创建 bcc:

{
  "COSTemplateFormatVersion": "2019-08-07",
  "Parameters": {
    "Env": {
      "Type": "String",
      "Default": "online"
    }
  },
  "Conditions": {
    "CreateBcc": {
      "Fn::Equals": [
        "online",
        {
          "Ref": "Env"
        }
      ]
    }
  },
  "Resources": {
    "bcc": {
      "Type": "BCE::BCC::Instance",
      "Condition": "CreateBcc",
      "Properties": {
        "Billing": {
          "PaymentTiming": "Postpaid"
        },
        "InstanceType": {
          "Ref": "InstanceType"
        },
        "ImageId": {
          "Ref": "ImageId"
        },
        "ZoneName": {
          "Ref": "ZoneName"
        },
        "CpuCount": {
          "Ref": "Cpu"
        },
        "MemoryCapacityInGB": {
          "Ref": "Memory"
        },
        "CreateCdsList": [
          {
            "CdsSizeInGB": {
              "Ref": "CdsSize"
            },
            "StorageType": {
              "Ref": "CdsStorageType"
            }
          }
        ]
      }
    }
  }
}

示例

以下示例显示的是典型的资源声明。

{
  "Resources": {
    "bcc": {
      "Type": "BCE::BCC::Instance",
      "Properties": {
        "Billing": {
          "PaymentTiming": "Postpaid"
        },
        "InstanceType": {
          "Ref": "InstanceType"
        },
        "ImageId": {
          "Ref": "ImageId"
        },
        "ZoneName": {
          "Ref": "ZoneName"
        },
        "CpuCount": {
          "Ref": "Cpu"
        },
        "MemoryCapacityInGB": {
          "Ref": "Memory"
        },
        "CreateCdsList": [
          {
            "CdsSizeInGB": {
              "Ref": "CdsSize"
            },
            "StorageType": {
              "Ref": "CdsStorageType"
            }
          }
        ]
      }
    }
  }
}
上一篇
条件(Conditions)
下一篇
输出(Outputs)