In the Resources section, you can define the properties of each resource that is contained in a stack and the dependencies between resources. A resource can be referenced by other resources and output items. This topic describes the information about the Resources section.

Resource types

Resource types are classified into regular resources and data source resources. Regular resources can be further classified into Alibaba Cloud resources and custom resources. The following table describes the resource types.

Resource type Description
Regular resource (Resource)
  • You can create Alibaba Cloud resources, and update and delete the resources. Alibaba Cloud resources must start with ALIYUN::.
  • You can create custom resources based on the logic that you define, and define how the resources are updated and deleted. Custom resources must start with Custom:: or ALIYUN::ROS::CustomResource.

    For more information, see Overview.

Data source resource (DataSource) You can use data source resources to query the resource data of Alibaba Cloud services.

For more information, see Data source resources.

Syntax

A resource consists of an ID and a description. All resource descriptions must be enclosed in braces ({ }). If you declare multiple resources, you must separate the resources by commas (,). The following sample code provides an example on the syntax of the Resources section:

"Resources" : {
    "Resource 1 ID" : {
        "Type" : "The resource type",
        "Condition": "The condition based on which the resource is created", 
        "Properties" : {
            The description of the properties of the resource
        }
    },
    "Resource 2 ID" : {
        "Type" : "The resource type",
        "Condition": "The condition based on which the resource is created", 
        "Properties" : {
            The description of the properties of the resource
        }
    }
}
Fields in the Resources section:
  • A resource ID must be unique in a template. You can use the resource ID to reference the resource in other sections of a template.
  • Type: the type of the resource that you declare. For example, if you set Type to ALIYUN::ECS::Instance, the resource that you declare is an Elastic Compute Service (ECS) instance. For more information about resource types that are supported by ROS, see List of resource types by service.
  • Properties: the additional options that you specify for the resource. For example, you must specify an image ID for each ECS instance. In this case, the image ID is a resource property.

Example:

"Resources" : {
  "ECSInstance" : {
    "Type" : "ALIYUN::ECS::Instance",
    "Properties" : {
      "ImageId" : "m-25l0r****"
    }
  }
}

If you do not need to declare properties for a resource, leave Properties empty.

Property values can be text strings, string lists, booleans, referenced parameters, or return values of functions. The following information describes the rules for property values:
  • Text strings must be enclosed in double quotation marks (" ").
  • String lists must be enclosed in brackets ([ ]).
  • Referenced parameters or return values of built-in functions must be enclosed in braces ({ }).
The preceding rules also apply when you combine text strings, string lists, referenced parameters, and return values of functions as property values.

The following sample code provides an example on how to declare different types of property values:

"Properties" : {
    "String" : "string",
    "LiteralList" : [ "value1", "value2" ],
    "Boolean" : "true"
    "ReferenceForOneValue" :  { "Ref" : "ResourceID" } ,
    "FunctionResultWithFunctionParams" : {
        "Fn::Join" : [ "%", [ "Key=", { "Ref" : "SomeParameter" } ] ] }
}

DeletionPolicy

You can specify the DeletionPolicy property in a template to declare that the system retains a specific resource when the stack that contains the resource is deleted. The following sample code provides an example on how to declare the DeletionPolicy property to retain an ECS instance when the stack that contains the instance is deleted:
"Resources" : {
  "ECSInstance" : {
    "Type" : "ALIYUN::ECS::Instance",
    "Properties" : {
      "ImageId" : "m-25l0r****"
    },
    "DeletionPolicy" : "Retain"
  }
}

In the preceding example, the system retains the resource ECSInstance when the stack that is created based on the template is deleted.

DependsOn

You can specify the DependsOn property in a template to create a specific resource only after its dependent resources are created. If you specify the DependsOn property for a resource, the system creates the resource only after its dependent resources specified in the DependsOn property are created.

Notice You can set the Condition property of a dependent resource that is specified in the DependsOn property to False. A value of False does not affect the result when the system creates a resource after its dependent resource is created.
The following sample code provides examples on how to configure dependent resources:
  • Configure a single dependent resource.
    "DependsOn": "ResourceName"
  • Configure multiple dependent resources.
    "DependsOn": [
                   "ResourceName1",
                   "ResourceName2"
                 ]   
The following sample code provides an example on how to specify the DependsOn property. In this example, WebServer is created only after DatabaseServer is created.
{
  "ROSTemplateFormatVersion" : "2015-09-01",
  "Resources" : {
    "WebServer": {
      "Type": "ALIYUN::ECS::Instance",
      "DependsOn": "DatabseServer"
    },
    "DatabseServer": {
      "Type": "ALIYUN::ECS::Instance",
      "Properties": {
        "ImageId" : "m-25l0r****",
        "InstanceType": "ecs.t1.small"
      }
    }
  }
}        

Condition

You can specify the Condition property in a template to determine whether to create a resource. The system can create the resource only when the condition that you specify for the Condition property is met.

The following sample code provides an example on how to specify the Condition property. In this example, whether WebServer is created is determined based on the value of MaxAmount.
{
  "ROSTemplateFormatVersion" : "2015-09-01",
  "Parameters": {
    "MaxAmount": {
      "Type": "Number",
      "Default": 1
    }
  },
  "Conditions": {
    "CreateWebServer": {"Fn::Not": {"Fn::Equals": [0, {"Ref": "MaxAmount"}]}}
  }
  "Resources" : {
    "WebServer": {
      "Type": "ALIYUN::ECS::InstanceGroup",
      "Condition": "CreateWebServer",
      "Properties": {
        "ImageId" : "m-25l0rc****",
        "InstanceType": "ecs.t1.small"
        "MaxAmount": {"Ref": "MaxAmount"}
      }
    },
    "DatabseServer": {
      "Type": "ALIYUN::ECS::Instance",
      "Properties": {
        "ImageId" : "m-25l0r****",
        "InstanceType": "ecs.t1.small"
      }
    }
  }
}

Count

You can specify the Count property for a resource in a template to preprocess the template and replace the resource with multiplied resources. When you use a template for which the Count property is specified to perform operations on a stack, the systems uses the processed template.
  • For example, you set the Count property of Resource A to 3 in the original template. After ROS preprocesses the template, Resource A is replaced with the following multiplied resources: Resources A[0], A[1], and A[2]. The following sample code provides an example on the original template:
    "Resources": {
      "A": {
        "Count": 3,
        ...
      },
      ...
    }

    The following sample code provides an example on the processed template:

    "Resources": {
      "A[0]": {
        ...
      },
      "A[1]": {
        ...
      },
      "A[2]": {
        ...
      },
      ...
    }
    Notice If the name of a multiplied resource, such as A[1], is the same as the name of an existing resource that is defined in the original template, the template fails to be preprocessed and validated.

    If you specify Count for a resource, the resource name is changed. We recommend that you do not add the Count property to existing resources to prevent deletion of the resources.

  • The value of the Count property must be a natural number. The property value supports only the following functions. You can invoke only the Ref function in Count to reference parameters.
  • The maximum number of resources in a processed template cannot exceed 300.
  • You can specify the ALIYUN::Index pseudo parameter in Properties of a resource for which the Count property is specified. When the system preprocesses the template, the system replaces ALIYUN::Index with the values that correspond to expanded resources. For example, the system replaces ALIYUN::Index that is applied to A[0] with 0 and replaces ALIYUN::Index that is applied to A[1] with 1. You can use ALIYUN::Index only in Properties of a template.
  • If you specify Count for a resource and specify the resource in DependsOn, the resource is replaced with multiplied resources. The following sample code provides an example on the original template:
    "DependsOn": "A"

    The following sample code provides an example on the processed template:

    "DependsOn": ["A[0]", "A[1]", "A[2]"]
  • If you specify Count for a resource and specify the resource in Ref and Fn::GetAtt, the resource is replaced with multiplied resources. The following sample code provides an example on the original template:
    {
      "Ref": "A"
    }
    
    
    {
      "Fn::GetAtt": ["A", "PropertyName"]
    }

    The following sample code provides an example on the processed template:

    [
      {
        "Ref": "A[0]"
      },
      {
        "Ref": "A[1]"
      },
      {
        "Ref": "A[2]"
      }
    ]
    
    
    [
      {
        "Fn::GetAtt": [
          "A[0]",
          "PropertyName"
        ]
      },
      {
        "Fn::GetAtt": [
          "A[1]",
          "PropertyName"
        ]
      },
      {
        "Fn::GetAtt": [
          "A[2]",
          "PropertyName"
        ]
      }
    ]

    If you specify the Count property for multiple resources and the resources are referenced by each other, we recommend that you use Count together with Fn::Select and ALIYUN::Index.

    {
      "Fn::Select": [
        {
          "Ref": "ALIYUN::Index"
        },
        {
          "Ref": "A"
        }
      ]
    }

    In this example, Resource A is used. You reference Resource A in Resource B, and set the Count property of Resource B to 2. The following sample code provides an example on a part of the expressions of Resources B[0] and B[1] after the template is processed:

    {
      "Ref": "A[0]"
    }
    
    {
      "Ref": "A[1]"
    }
  • If you specify the Count property for a resource, the DependsOn property also supports expressions. The functions that are supported by the expressions of the DependsOn property are the same as the functions that are supported by the expressions of the Count property. Both properties also support the ALIYUN::Index pseudo parameter. The following table describes the formats and the calculation results of the expressions.
    Format Example Calculation result
    Dictionary
    {
      "Fn::Split": [",", "Server1,Server2"]
    }
    null.

    If the calculation result is null, the system removes the DependsOn property.

    A string.
    • If the calculation result is an empty string, the system removes the DependsOn property.
    • If the calculation result is not an empty string, the calculation result must be a valid resource name.
    A list.
    • If the items in the list are null or empty strings, the system removes the items.
    • If the list is empty, the system removes the DependsOn property.
    • If the list is not empty, each item in the list must be a string and a valid resource name.
    List
    [
      "Server0",
      {
        "Fn::Split": [",", "Server1,Server2"]
      }
    ]
    The items in a list can be dictionaries or strings. Calculation results vary based on the item types. The following information describes the differences:
    • If the items are dictionaries, the system calculates the expressions and returns the following calculation results.
      • If the items are null or empty strings, the system removes the items.
      • If the items are not null or empty strings, the items must be strings and valid resource names.
    • If the items are strings, the items must be valid resource names.
    Note If the list is empty, the system removes the DependsOn property.
    String
    Server0
    A string.

    The system does not process a string.

    The following template shows how to specify Count to control the number of resources that can be created, and specify ParallelCount to control the maximum number of resources that can be created at a time.

    {
      "ROSTemplateFormatVersion": "2015-09-01",
      "Parameters": {
        "Count": {
          "Type": "Number"
        },
        "ParallelCount": {
          "Type": "Number"
        }
      },
      "Resources": {
        "WaitConditionHandle": {
          "Type": "ALIYUN::ROS::WaitConditionHandle",
          "Count": {
            "Ref": "Count"
          },
          "DependsOn": {
            "Fn::Select": [
              {
                "Fn::Calculate": [
                  "1-{0}//{1}",
                  0,
                  [
                    {
                      "Fn::Min": [
                        {
                          "Ref": "ALIYUN::Index"
                        },
                        {
                          "Ref": "ParallelCount"
                        }
                      ]
                    },
                    {
                      "Ref": "ParallelCount"
                    }
                  ]
                ]
              },
              [
                {
                  "Fn::Replace": [
                    {
                      "index": {
                        "Fn::Calculate": [
                          "{0}-{1}",
                          0,
                          [
                            {
                              "Ref": "ALIYUN::Index"
                            },
                            {
                              "Ref": "ParallelCount"
                            }
                          ]
                        ]
                      }
                    },
                    "WaitConditionHandle[index]"
                  ]
                }
              ]
            ]
          }
        }
      }
    }

    The processed templates that the system generates vary based on the values that you specify for Count and ParallelCount.

    • If you set Count to 3 and ParallelCount to 1, the system generates the following processed template:
      {
        "ROSTemplateFormatVersion": "2015-09-01",
        "Resources": {
          "WaitConditionHandle[0]": {
            "Type": "ALIYUN::ROS::WaitConditionHandle"
          },
          "WaitConditionHandle[1]": {
            "Type": "ALIYUN::ROS::WaitConditionHandle",
            "DependsOn": "WaitConditionHandle[0]"
          },
          "WaitConditionHandle[2]": {
            "Type": "ALIYUN::ROS::WaitConditionHandle",
            "DependsOn": "WaitConditionHandle[1]"
          }
        },
        "Parameters": {
          "Count": {
            "Type": "Number"
          },
          "ParallelCount": {
            "Type": "Number"
          }
        }
      }
    • If you set Count to 5 and ParallelCount to 2, the system generates the following processed template:
      {
        "ROSTemplateFormatVersion": "2015-09-01",
        "Resources": {
          "WaitConditionHandle[0]": {
            "Type": "ALIYUN::ROS::WaitConditionHandle"
          },
          "WaitConditionHandle[1]": {
            "Type": "ALIYUN::ROS::WaitConditionHandle"
          },
          "WaitConditionHandle[2]": {
            "Type": "ALIYUN::ROS::WaitConditionHandle",
            "DependsOn": "WaitConditionHandle[0]"
          },
          "WaitConditionHandle[3]": {
            "Type": "ALIYUN::ROS::WaitConditionHandle",
            "DependsOn": "WaitConditionHandle[1]"
          },
          "WaitConditionHandle[4]": {
            "Type": "ALIYUN::ROS::WaitConditionHandle",
            "DependsOn": "WaitConditionHandle[2]"
          }
        },
        "Parameters": {
          "Count": {
            "Type": "Number"
          },
          "ParallelCount": {
            "Type": "Number"
          }
        }
      }
    • If you set Count to 5 and ParallelCount to 3, the system generates the following processed template:
      {
        "ROSTemplateFormatVersion": "2015-09-01",
        "Resources": {
          "WaitConditionHandle[0]": {
            "Type": "ALIYUN::ROS::WaitConditionHandle"
          },
          "WaitConditionHandle[1]": {
            "Type": "ALIYUN::ROS::WaitConditionHandle"
          },
          "WaitConditionHandle[2]": {
            "Type": "ALIYUN::ROS::WaitConditionHandle"
          },
          "WaitConditionHandle[3]": {
            "Type": "ALIYUN::ROS::WaitConditionHandle",
            "DependsOn": "WaitConditionHandle[0]"
          },
          "WaitConditionHandle[4]": {
            "Type": "ALIYUN::ROS::WaitConditionHandle",
            "DependsOn": "WaitConditionHandle[1]"
          }
        },
        "Parameters": {
          "Count": {
            "Type": "Number"
          },
          "ParallelCount": {
            "Type": "Number"
          }
        }
      }

The following sample code provides an example on how to specify the Count property in the original template. In this template, a group of Elastic IP addresses (EIPs) and ECS instances are created, and each EIP is bound to an ECS instance. The same number of EIPs and ECS instances are created.

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Parameters": {
    "Count": {
      "Type": "Number"
    }
  },
  "Resources": {
    "Eip": {
      "Type": "ALIYUN::VPC::EIP",
      "Count": {
        "Ref": "Count"
      },
      "Properties": {
        "Bandwidth": 5
      }
    },
    "Servers": {
      "Type": "ALIYUN::ECS::InstanceGroup",
      "Properties": {
        "MinAmount": {
          "Ref": "Count"
        },
        "MaxAmount": {
          "Ref": "Count"
        },
        "AllocatePublicIP": false,
        ...
      }
    },
    "EipBind": {
      "Type": "ALIYUN::VPC::EIPAssociation",
      "Count": {
        "Ref": "Count"
      },
      "Properties": {
        "InstanceId": {
          "Fn::Select": [
            {
              "Ref": "ALIYUN::Index"
            },
            {
              "Fn::GetAtt": [
                "Servers",
                "InstanceIds"
              ]
            }
          ]
        },
        "AllocationId": {
          "Fn::Select": [
            {
              "Ref": "ALIYUN::Index"
            },
            {
              "Ref": "Eip"
            }
          ]
        }
      }
    }
  },
  "Outputs": {
    "InstanceIds": {
      "Value": {
        "Fn::GetAtt": [
          "Servers",
          "InstanceIds"
        ]
      }
    },
    "AllocationIds": {
      "Value": {
        "Ref": "Eip"
      }
    },
    "EipAddresses": {
      "Value": {
        "Fn::GetAtt": [
          "Eip",
          "EipAddress"
        ]
      }
    }
  }
}

The following sample code provides an example on the processed template:

{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Parameters": {
    "Count": {
      "Type": "Number"
    }
  },
  "Resources": {
    "Eip[0]": {
      "Type": "ALIYUN::VPC::EIP",
      "Properties": {
        "Bandwidth": 5
      }
    },
    "Eip[1]": {
      "Type": "ALIYUN::VPC::EIP",
      "Properties": {
        "Bandwidth": 5
      }
    },
    "Servers": {
      "Type": "ALIYUN::ECS::InstanceGroup",
      "Properties": {
        "MinAmount": {
          "Ref": "Count"
        },
        "MaxAmount": {
          "Ref": "Count"
        },
        "AllocatePublicIP": false,
        ...
      }
    },
    "EipBind[0]": {
      "Type": "ALIYUN::VPC::EIPAssociation",
      "Properties": {
        "InstanceId": {
          "Fn::Select": [
            0,
            {
              "Fn::GetAtt": [
                "Servers",
                "InstanceIds"
              ]
            }
          ]
        },
        "AllocationId": {
          "Ref": "Eip[0]"
        }
      }
    },
    "EipBind[1]": {
      "Type": "ALIYUN::VPC::EIPAssociation",
      "Properties": {
        "InstanceId": {
          "Fn::Select": [
            1,
            {
              "Fn::GetAtt": [
                "Servers",
                "InstanceIds"
              ]
            }
          ]
        },
        "AllocationId": {
          "Ref": "Eip[1]"
        }
      }
    }
  },
  "Outputs": {
    "InstanceIds": {
      "Value": {
        "Fn::GetAtt": [
          "Servers",
          "InstanceIds"
        ]
      }
    },
    "AllocationIds": {
      "Value": [
        {
          "Ref": "Eip[0]"
        },
        {
          "Ref": "Eip[1]"
        }
      ]
    },
    "EipAddresses": {
      "Value": [
        {
          "Fn::GetAtt": [
            "Eip[0]",
            "EipAddress"
          ]
        },
        {
          "Fn::GetAtt": [
            "Eip[1]",
            "EipAddress"
          ]
        }
      ]
    }
  }
}

Resource declaration example

The following example shows how to declare resources:

"Resources" : {
    "WebServer": {
      "Type": "ALIYUN::ECS::Instance",
      "Properties": {
        "ImageId" : "m-25l0r****",
        "InstanceType": "ecs.t1.small",
        "SecurityGroupId": "sg-25zwc****",
        "ZoneId": "cn-beijing-b",
        "Tags": [{
            "Key": "Department1",
            "Value": "HumanResource"
        },{
            "Key": "Department2",
            "Value": "Finance"
        }
        ]
      }
    },
    "ScalingConfiguration": {
      "Type": "ALIYUN::ESS::ScalingConfiguration",
      "Properties": {
        "ImageId": "ubuntu_14_04_64_20G_aliaegis_2015****.vhd",
        "InstanceType": "ecs.t1.small",
        "InstanceId": "i-25xhh****",
        "InternetChargeType": "PayByTraffic",
        "InternetMaxBandwidthIn": 1,
        "InternetMaxBandwidthOut": 20,
        "SystemDisk_Category": "cloud",
        "ScalingGroupId": "bwhtvpcBcKYac9fe3vd0****",
        "SecurityGroupId": "sg-25zwc****",
        "DiskMappings": [
            {
                "Size": 10
            },
            {
                "Category": "cloud",
                "Size": 10
            }
        ]
      }
    }
}