The Resources section describes the properties of each resource in a stack and the dependencies between them. You can reference a resource from other resources or in the Outputs section.
Resource entity types
Resource entities are categorized as regular resources and data source resources. Regular resources are divided into Alibaba Cloud resources and custom resources, as shown in the following table.
Resource entity type | Description |
Regular resource (Resource) |
|
Data source resource (DataSource) | Queries the resource data of an Alibaba Cloud service. For more information, see Data source resources. |
Syntax
The Resources section consists of a resource ID and a resource description. The resource description is enclosed in braces ({}). If you declare multiple resources, you must separate them with commas (,). The following code segment shows the syntax structure of the Resources section:
Resources:
Resource1 Name:
Type: Resource type
Condition: Condition for creating this resource
Properties: Resource property description
Resource2 Name:
Type: Resource type
Condition: Condition for creating this resource
Properties: Resource property descriptionThe parameters are described as follows:
The resource name must be unique within the template. You can use the resource name to reference the resource in other parts of the template.
The resource type (Type) indicates the type of resource that you are declaring. For example, ALIYUN::ECS::Instance represents an Alibaba Cloud ECS instance. For a list of all resource types that ROS supports, see Resource type index.
The resource properties (Properties) are additional options that you specify for a resource. For example, you must specify an Image ID for each Alibaba Cloud ECS instance.
Example
Resources:
ECSInstance:
Type: ALIYUN::ECS::Instance
Properties:
ImageId: m-25l0r****If a resource does not require any properties, you can omit the Properties section for that resource.
A property value can be a text string, a list of strings, a Boolean value, a referenced parameter, or a function return value. When you write a JSON template, you must follow these rules.
If the property value is a text string, enclose the value in double quotation marks (" ").
If the property value is a list of strings, enclose the list in square brackets ([]).
If the property value is an intrinsic function or a referenced parameter, enclose it in braces ({}).
These rules also apply when you combine text, lists, referenced parameters, and function return values. The following example shows how to declare different property value types:
{
"Properties": {
"String": "string",
"LiteralList": [
"value1",
"value2"
],
"Boolean": true,
"ReferenceForOneValue": {
"Ref": "ResourceID"
},
"FunctionResultWithFunctionParams": {
"Fn::Join": [
"%",
[
"Key=",
{
"Ref": "SomeParameter"
}
]
]
}
}
}DeletionPolicy
When you remove a resource, two scenarios are possible:
If you set DeletionPolicy to Delete, the resource itself is deleted when it is removed from the stack.
If you set DeletionPolicy to Retain, the resource is kept when it is removed from the stack.
For example, to keep an ECS instance when the stack is deleted, declare the policy as shown in the following code segment:
Resources:
ECSInstance:
Type: ALIYUN::ECS::Instance
Properties:
ImageId: m-25l0r****
DeletionPolicy: RetainIn this example, if the stack that corresponds to this template is deleted, the ECSInstance resource is kept.
If a resource has DeletionPolicy configured and you call the DeleteStack operation and include the resource in the RetainResources parameter, the following principles apply:
If Resource A has DeletionPolicy=Delete in the template, whether the resource is kept depends on whether it is included in RetainResources when you delete the stack.
If Resource A has DeletionPolicy=Retain in the template, the resource is always kept when you delete the stack, regardless of whether it is included in RetainResources.
DependsOn
In a template, use the DependsOn attribute to ensure that a resource is created only after another specified resource has been created. When you apply the DependsOn attribute to a resource, it will not be created until the resource referenced by DependsOn is fully provisioned.
The resource that DependsOn relies on can have its Condition evaluate to False. A value of False does not affect the creation of the dependent resource.
The following examples show two ways to use this attribute:
Dependency on a single resource:
DependsOn: ResourceNameDepend on multiple resources:
DependsOn: - ResourceName1 - ResourceName2
As shown in the following code segment, WebServer is created only after DatabaseServer is successfully 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.smallCondition
In a template, you can use the Condition attribute to specify whether to create a resource. The resource is created only if the condition value specified by Condition is True.
As shown in the following code segment, the creation of WebServer depends 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.smallCount
In a template, after you specify Count for a resource, ROS pre-processes the template and expands the resource into multiple resources. The processed template is then used when you perform operations on the stack.
For example, if a resource is named
Aand itsCountvalue is 3, the processed template will not contain theAresource. Instead, it will contain three resources:A[0],A[1], andA[2].Resources: A: Count: 3 ...After processing:
Resources: A[0]: ... A[1]: ... A[2]: ...ImportantIf the name of an expanded resource, such as
A[1], already exists in the original template, the pre-processing fails and the template does not pass validation.Avoid adding the
Countattribute to existing resources. This changes the resource name and triggers a delete operation.The final result of
Countmust be a natural number. Only the following functions are supported:The total number of resources in the processed template must not exceed the limit, which is currently 300.
In the
Propertiesof a resource withCountspecified, you can use the pseudo parameterALIYUN::Index. During pre-processing, this parameter is replaced with the corresponding number. For example, theALIYUN::Indexused byA[0]is replaced with 0, and theALIYUN::Indexused byA[1]is replaced with 1. You cannot useALIYUN::Indexelsewhere in the template.If a resource with
Countspecified appears inDependsOn, it is expanded.DependsOn: AAfter processing:
DependsOn: - A[0] - A[1] - A[2]If a resource with
Countspecified appears in theReforFn::GetAttfunction, it is expanded.Ref: A Fn::GetAtt: - A - PropertyNameAfter processing:
- Ref: A[0] - Ref: A[1] - Ref: A[2] - Fn::GetAtt: - A[0] - PropertyName - Fn::GetAtt: - A[1] - PropertyName - Fn::GetAtt: - A[2] - PropertyNameIf multiple resources use
Countand have reference relationships, you can use them withFn::SelectandALIYUN::Index.Fn::Select: - Ref: ALIYUN::Index - Ref: ATake
Aas an example.BreferencesA, and theCountforBis 2. After transformation, parts of the expressions inB[0]andB[1]are as follows:- Ref: A[0] - Ref: A[1]If a resource has the
Countattribute, theDependsOnattribute also supports expressions. The functions that can be used in theDependsOnexpression are the same as those for theCountattribute, and you can use theALIYUN::Indexpseudo parameter. The following table describes the format of the expression and the allowed calculation results.Format
Example
Allowed calculation result
Dictionary
Fn::Split: - ',' - Server1,Server2null
If the value is null, the DependsOn attribute is removed.
String
If the value is an empty string, the DependsOn attribute is removed.
If the value is a non-empty string, it must be a valid resource name.
List
If a list item is null or an empty string, the item is discarded.
If the list is empty, the DependsOn attribute is removed.
If the list is not empty, each item must be a string and a valid resource name.
List
- Server0 - Fn::Split: - ',' - Server1,Server2A list item can be a dictionary or a string. The allowed calculation results vary based on the item type:
If the list item is a dictionary, the expression is calculated. The allowed calculation results are as follows:
If the value is null or an empty string, the item is discarded.
Otherwise, it must be a string and a valid resource name.
If the list item is a string, it must be a valid resource name.
NoteIf the list is empty, the DependsOn attribute is removed.
String
Server0String
No additional processing is performed.
The following example template controls the concurrency for resources created with Count. The Count parameter specifies the number of resources to create, and the ParallelCount parameter specifies the maximum number of concurrent creations.
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 template varies based on the values of Count and ParallelCount:
If you set Count to 3 and ParallelCount to 1, the processed template is as follows:
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: NumberIf you set Count to 5 and ParallelCount to 2, the processed template is as follows:
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: NumberIf you set Count to 5 and ParallelCount to 3, the processed template is as follows:
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 is an example template for Count. The template creates a group of EIPs and an equal number of ECS instances, and then attaches each EIP 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
- EipAddressAfter processing:
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]
- EipAddressResource declaration example
The following is a typical resource declaration example:
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