资源(Resources)
更新时间:2024-09-25
下述内容即将下线,后续参考最新版本的资源语法。
描述堆栈中每一个资源的属性和依赖关系。一个资源可以被其他资源和 Outputs 所引用。
语法
资源部分由资源 ID 和资源描述组成。所有资源描述都被括在括号里。如果您声明多个资源,则可用逗号将它们分隔开。以下代码段描述了 Resources 的语法结构:
Plain Text
1"Resources" : {
2 "Resource1" : {
3 "Type" : "资源类型",
4 "Condition": "是否创建此资源的条件",
5 "Properties" : {
6 资源属性
7 }
8 },
9 "resource2" : {
10 "Type" : "资源类型",
11 "Condition": "是否创建此资源的条件",
12 "Properties" : {
13 资源属性描述
14 }
15 }
16}
资源 ID
资源 ID 在模板中具有唯一性。可使用资源 ID 在模板的其他部分中引用资源。
资源类型
资源类型标识您正在声明的资源的类型。有关所有资源的列表,请参阅 云编排资源。
资源属性
资源属性是可以为资源指定的附加选项。例如,对于每个百度云BCC实例,必须为该实例指定一个 Image ID。如以下代码段所示:
Plain Text
1"Resources" : {
2 "bcc" : {
3 "Type" : "BCE::BCC::Instance",
4 "Properties" : {
5 "ImageId" : "m-kDwAHHal"
6 }
7 }
8}
如果资源不需要声明任何属性,那么您可以忽略该资源的属性部分。
Condition
使用 Condition 属性可以指定是否需要正真创建此资源。当只有 Condition 所指定的条件值为 true 时才创建此资源。
如以下代码段所示,根据 Env 的值判断否创建 bcc:
Plain Text
1{
2 "COSTemplateFormatVersion": "2019-08-07",
3 "Parameters": {
4 "Env": {
5 "Type": "String",
6 "Default": "online"
7 }
8 },
9 "Conditions": {
10 "CreateBcc": {
11 "Fn::Equals": [
12 "online",
13 {
14 "Ref": "Env"
15 }
16 ]
17 }
18 },
19 "Resources": {
20 "bcc": {
21 "Type": "BCE::BCC::Instance",
22 "Condition": "CreateBcc",
23 "Properties": {
24 "Billing": {
25 "PaymentTiming": "Postpaid"
26 },
27 "InstanceType": {
28 "Ref": "InstanceType"
29 },
30 "ImageId": {
31 "Ref": "ImageId"
32 },
33 "ZoneName": {
34 "Ref": "ZoneName"
35 },
36 "CpuCount": {
37 "Ref": "Cpu"
38 },
39 "MemoryCapacityInGB": {
40 "Ref": "Memory"
41 },
42 "CreateCdsList": [
43 {
44 "CdsSizeInGB": {
45 "Ref": "CdsSize"
46 },
47 "StorageType": {
48 "Ref": "CdsStorageType"
49 }
50 }
51 ]
52 }
53 }
54 }
55}
示例
以下示例显示的是典型的资源声明。
Plain Text
1{
2 "Resources": {
3 "bcc": {
4 "Type": "BCE::BCC::Instance",
5 "Properties": {
6 "Billing": {
7 "PaymentTiming": "Postpaid"
8 },
9 "InstanceType": {
10 "Ref": "InstanceType"
11 },
12 "ImageId": {
13 "Ref": "ImageId"
14 },
15 "ZoneName": {
16 "Ref": "ZoneName"
17 },
18 "CpuCount": {
19 "Ref": "Cpu"
20 },
21 "MemoryCapacityInGB": {
22 "Ref": "Memory"
23 },
24 "CreateCdsList": [
25 {
26 "CdsSizeInGB": {
27 "Ref": "CdsSize"
28 },
29 "StorageType": {
30 "Ref": "CdsStorageType"
31 }
32 }
33 ]
34 }
35 }
36 }
37}