In the Resources section, you can define the properties of each resource that is contained in a stack and the dependency relationships between resources. A resource can be referenced by other resources in the Resources section and outputs in the Outputs section. This topic describes information about the Resources section.
Resource types
Resource types are classified into regular resources and data source resources. Regular resources are further classified into Alibaba Cloud resources and custom resources. The following table describes the resource types.
Resource type | Description |
Regular resource (Resource) |
|
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
The declaration of a resource consists of a resource name and a resource description. A resource description 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:
Name of Resource 1:
Type: the resource type.
Condition: the condition based on which the resource is created.
Properties: the descriptions of resource properties.
Name of Resource 2:
Type: the resource type.
Condition: the condition based on which the resource is created.
Properties: the descriptions of resource properties.
Fields in the Resources section:
A resource name must be unique in a template. You can use the resource name 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 Resource Orchestration Service (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, ImageId is a resource property.
Sample code:
Resources:
ECSInstance:
Type: ALIYUN::ECS::Instance
Properties:
ImageId: m-25l0r****
If you do not need to declare properties for a resource, you can leave Properties empty.
Property values can be text strings, string lists, booleans, referenced parameters, or return values of functions. When you create a JSON template, you must take note of the following 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
When you delete a resource from a stack, one of the following situations may occur:
If you set the DeletionPolicy property of a resource to Delete, the system deletes the resource when you delete the stack.
If you set the DeletionPolicy property of a resource to Retain, the system retains the resource when you delete the stack.
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 ECSInstance resource 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 dependency is created. If you specify the DependsOn property for a resource, the system creates the resource only after its dependency specified in the DependsOn property is created.
You can set the Condition property of a dependency that is specified in the DependsOn property to False. A value of False specifies that the resource is created even if the dependency fails to be created.
The following sample code provides examples on how to configure dependencies.
Configure a single dependency:
DependsOn: ResourceName
Configure multiple dependencies:
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 the system can create a resource. The system can create the resource only when the condition specified by 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 process the template and replace the resource with multiplied resources. ROS uses the processed template to manage stacks.
For example, you set the
Count
property ofResource A
to 3 in the original template. After ROS processes the template,Resource A
is replaced with the following multiplied resources: ResourcesA[0]
,A[1]
, andA[2]
. The following sample code provides an example of the original template:Resources: A: Count: 3 ...
The following sample code provides an example of the processed template:
Resources: A[0]: ... A[1]: ... A[2]: ...
ImportantIf 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 processed and validated.If you specify the Count property for a resource, the resource name is changed. We recommend that you do not specify the
Count
property for existing resources to prevent the resources from being deleted.The value of the
Count
property must be a natural number. The property supports only the following functions:A processed template can contain up to 300 resources.
You can specify the
ALIYUN::Index
pseudo function inProperties
of a resource for which theCount
property is specified. When the system processes the template, the system replaces ALIYUN::Index with the values that correspond to multiplied resources. For example, the system replacesALIYUN::Index
forA[0]
with 0 and replacesALIYUN::Index
forA[1]
with 1. You can useALIYUN::Index
only in Properties of a resource for which the Count property is specified.If you specify the
Count
property for a resource and specify the resource in theDependsOn
property, the resource is replaced with multiplied resources. The following sample code provides an example of the original template:DependsOn: A
The following sample code provides an example of the processed template:
DependsOn: - A[0] - A[1] - A[2]
If you specify the
Count
property for a resource and specify the resource inRef
andFn::GetAtt
functions, the resource is replaced with multiplied resources. The following sample code provides an example of the original template:Ref: A Fn::GetAtt: - A - PropertyName
The following sample code provides an example of 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 the Count property together withFn::Select
andALIYUN::Index
functions.Fn::Select: - Ref: ALIYUN::Index - Ref: A
In this example,
Resource A
is used. You referenceResource A
inResource B
and set theCount
property ofResource B
to 2. The following sample code provides an example on a part of the expressions of ResourcesB[0]
andB[1]
after the template is processed:- Ref: A[0] - Ref: A[1]
If you specify the
Count
property for a resource, theDependsOn
property also supports expressions. The functions that are supported by the expressions of theDependsOn
property are the same as the functions that are supported by the expressions of theCount
property. Both properties support theALIYUN::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.
NoteIf 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, the same number of elastic IP addresses (EIPs) and ECS instances are created, and each EIP is bound to an ECS instance.
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
...: Null
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 of 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
...: Null
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