edit-icon download-icon

Resources

Last Updated: May 16, 2018

Resources in templates describe the attributes of every resource and the dependency between resources in resource stacks. You can reference a resource when defining other resources and the Outputs parameters in the same template.

Syntax

Resources consist of resource ID and resource description. If you define multiple resources, use commas (,) to separate them. The following code segment describes the syntax structure of Resources.

  1. "Resources" : {
  2. "Resource 1 ID":{
  3. "Type": "Resource type".
  4. "Condition": "Whether the condition of the resource is to be created".
  5. "Properties" : {
  6. Resource property description
  7. }
  8. },
  9. "Resource 2 ID": {
  10. "Type": "Resource type".
  11. "Condition": "Whether the condition of the resource is to be created".
  12. "Properties" : {
  13. Resource property description
  14. }
  15. }
  16. }

Resource ID

Resource ID is a reference ID of a particular resource. It is unique in a template and refers to a particular resource in the template sections.

Type

Resource type identifies the type of a resource defined in a template. For example, ALIYUN::ECS::Instance refers to Alibaba Cloud ECS instances. For the list of all resource types, see Resource type list.

Properties

Resource properties are additional options that can be specified for resources. For example, an image ID must be specified for each Alibaba Cloud ECS instance.

See the following code segment.

  1. "Resources" : {
  2. "ECSInstance" : {
  3. "Type" : "ALIYUN::ECS::Instance",
  4. "Properties" : {
  5. "ImageId" : "m-25l0rcfjo"
  6. }
  7. }
  8. }

If it is not required to define any property for the resource, it can be skipped.

A property value can be a text string, string list, Boolean value, referenced parameter value, or value returned by a function. If the value is a text string, it is included in double quotation marks (“ ”). If the value is a string list of any type, it is included in square brackets ([ ]). If the value is an internal function or referenced parameter, it is included in parentheses ({ }).

The following example shows how to specify different property values.

  1. "Properties" : {
  2. "String" : "string",
  3. "LiteralList" : [ "value1", "value2" ],
  4. "Boolean" : "true"
  5. "ReferenceForOneValue" : { "Ref" : "ResourceID" } ,
  6. "FunctionResultWithFunctionParams" : {
  7. "Fn::Join" : [ "%", [ "Key=", { "Ref" : "SomeParameter" } ] ] }
  8. }

DeletionPolicy

With DeletionPolicy, you can specify that the system retains a specific resource when the stack of this resource is deleted.

If you want to retain the ECS instance when the resource stack is deleted, see the following code segment.

  1. "Resources" : {
  2. "ECSInstance" : {
  3. "Type" : "ALIYUN::ECS::Instance",
  4. "Properties" : {
  5. "ImageId" : "m-25l0rcfjo"
  6. },
  7. "DeletionPolicy" : "Retain"
  8. }
  9. }

DependsOn

DependsOn is used to define that a resource is to be created after another specific resource has been created.

The WebServer is to be created only after the DatabaseServer is created successfully, as described in the following code segment.

  1. {
  2. "ROSTemplateFormatVersion" : "2015-09-01",
  3. "Resources" : {
  4. "WebServer": {
  5. "Type": "ALIYUN::ECS::Instance",
  6. "DependsOn": "DatabaseServer"
  7. },
  8. "DatabaseServer": {
  9. "Type": "ALIYUN::ECS::Instance",
  10. "Properties": {
  11. "ImageId" : "m-25l0rcfjo",
  12. "InstanceType": "ecs.t1.small"
  13. }
  14. }
  15. }
  16. }

Condition

Condition is used to determine whether to create a resource. A resource is created only when the value specified by Condition is true.

As shown in the following code segment, whether to create WebServer is determined by the value of MaxAmount.

  1. {
  2. "ROSTemplateFormatVersion" : "2015-09-01",
  3. "Parameters": {
  4. "MaxAmount": {
  5. "Type": "Number",
  6. "Default": 1
  7. }
  8. },
  9. "Conditions": {
  10. "CreateWebServer": {"Fn::Not": {"Fn::Equals": [0, {"Ref": "MaxAmount"}]}}
  11. }
  12. "Resources" : {
  13. "WebServer": {
  14. "Type": "ALIYUN::ECS::InstanceGroup",
  15. "Condition": "CreateWebServer",
  16. "Properties": {
  17. "ImageId" : "m-25l0rcfjo",
  18. "InstanceType": "ecs.t1.small"
  19. "MaxAmount": {"Ref": "MaxAmount"}
  20. }
  21. },
  22. "DatabseServer": {
  23. "Type": "ALIYUN::ECS::Instance",
  24. "Properties": {
  25. "ImageId" : "m-25l0rcfjo",
  26. "InstanceType": "ecs.t1.small"
  27. }
  28. }
  29. }
  30. }

Example

The following example shows a typical resource statement.

  1. "Resources" : {
  2. "WebServer": {
  3. "Type": "ALIYUN::ECS::Instance",
  4. "Properties": {
  5. "ImageId" : "m-25l0rcfjo",
  6. "InstanceType": "ecs.t1.small",
  7. "SecurityGroupId": "sg-25zwc3se0",
  8. "ZoneId": "cn-beijing-b",
  9. "Tags": [{
  10. "Key": "Department1",
  11. "Value": "HumanResource"
  12. },{
  13. "Key": "Department2",
  14. "Value": "Finance"
  15. }
  16. ]
  17. }
  18. },
  19. "ScalingConfiguration": {
  20. "Type": "ALIYUN::ESS::ScalingConfiguration",
  21. "Properties": {
  22. "ImageId": "ubuntu1404_64_20G_aliaegis_20150325.vhd",
  23. "InstanceType": "ecs.t1.small",
  24. "InstanceId": "i-25xhhcqbu",
  25. "InternetChargeType": "PayByTraffic",
  26. "InternetMaxBandwidthIn": 1,
  27. "InternetMaxBandwidthOut": 20,
  28. "SystemDisk_Category": "cloud",
  29. "ScalingGroupId": "bwhtvpcBcKYac9fe3vd0kv7E",
  30. "SecurityGroupId": "sg-25zwc3se0",
  31. "DiskMappings": [
  32. {
  33. "Size": 10
  34. },
  35. {
  36. "Category": "cloud",
  37. "Size": 10
  38. }
  39. ]
  40. }
  41. }
  42. }
Thank you! We've received your feedback.