A template serves as the blueprint for your infrastructure and architecture. You can define cloud service resources and their dependency relationships in a template to deploy the resources by using Resource Orchestration Service (ROS). This topic describes how to create a template. In this example, the template is used to create a virtual private cloud (VPC) and a vSwitch.
Prerequisites
You are familiar with the requirements for template structures. For more information, see Template structure.
Usage notes
You can refer to the documentation of a resource type to view the details of resource properties. For more information, see List of resource types by service.
The documentation of a resource type describes the details of each resource property, such as the type, whether required, and whether editable. If a property is required, you must declare the property in the Properties part of the Resources section of a template. If a property is optional, you can leave the property empty. If a property is editable, you can modify the property in a new template after the property is specified for a resource in a stack template. Then, you can use the new template to update the stack and the property. If a property is not editable, you cannot modify the property.
Create a template
To create a template, you must declare the resources that you want to create in the Resources section of the template. You can refer to List of resource types by service to view all resource types that are supported by ROS. Then, you can refer to the documentation of a desired resource type to view the supported properties and return values. To create a template, you can go to the Stacks or Templates page in the ROS console. When you create a template, the ROS console displays instructions and validates the template content. For more information, see Online template editor.
View details of the desired resource types.
Refer to List of resource types by service to find the resource types that are suitable for your deployment scenario. In this example, the ALIYUN::ECS::VPC and ALIYUN::ECS::VSwitch resource types that are used to create a VPC and a vSwitch are suitable.
Refer to the documentation of each resource type and view the resource properties. For more information, see Usage notes.
In the Parameters section, define the parameters of the template.
You can define the VpcCidrBlock parameter of the VPC, and the ZoneId, VSwitchName, and VSwitchCidrBlock parameters of the vSwitch. The parameters defined in the Parameters section provide parameter values in the ROS console. For more information, see ALIYUN::ECS::VPC and ALIYUN::ECS::VSwitch. If the vSwitch is associated with the VPC, you must make sure that vSwitch CIDR block belongs to the VPC CIDR block.
Parameters: VpcCidrBlock: Type: String Default: 192.168.0.0/16 AllowedValues: - 10.0.0.0/8 - 172.16.0.0/12 - 192.168.0.0/16 ZoneId: Type: String AssociationProperty: ALIYUN::ECS::Instance::ZoneId VSwitchName: Type: String VSwitchCidrBlock: Type: String Default: 192.168.0.0/24 AllowedValues: - 10.0.0.0/24 - 172.16.0.0/24 - 192.168.0.0/24
In the Resources section, define the resources that you want to create.
The VpcId, ZoneId, CidrBlock properties of the vSwitch are required. Other properties of the vSwitch and the VPC are optional. For more information, see ALIYUN::ECS::VPC and ALIYUN::ECS::VSwitch. You can use the Ref function to reference the parameters defined in the Parameters section for properties in the Resources section. You can also directly specify values for properties in the Resources section. For more information, see Ref.
Resources: Vpc: Type: ALIYUN::ECS::VPC Properties: VpcName: MyTest CidrBlock: Ref: VpcCidrBlock VSwitch: Type: ALIYUN::ECS::VSwitch Properties: VpcId: Ref: Vpc ZoneId: Ref: ZoneId VSwitchName: Ref: VSwitchName CidrBlock: Ref: VSwitchCidrBlock
In the Outputs section, define the outputs of the template.
You can define VpcId as an output of the VPC and VSwitchId as an output of the vSwitch. For more information, see ALIYUN::ECS::VPC and ALIYUN::ECS::VSwitch. You can invoke the Fn::GetAtt built-in function to obtain the property values of a resource. For more information, see Fn::GetAtt.
Outputs: VpcId: Value: Fn::GetAtt: - Vpc - VpcId VSwitchId: Value: Fn::GetAtt: - VSwitch - VSwitchId
Follow the template structure to create a complete template.
ROSTemplateFormatVersion: '2015-09-01' Parameters: VSwitchName: Type: String VSwitchCidrBlock: Default: 192.168.0.0/24 Type: String AllowedValues: - 10.0.0.0/24 - 172.16.0.0/24 - 192.168.0.0/24 VpcCidrBlock: Default: 192.168.0.0/16 Type: String AllowedValues: - 10.0.0.0/8 - 172.16.0.0/12 - 192.168.0.0/16 ZoneId: AssociationProperty: ALIYUN::ECS::Instance::ZoneId Type: String Resources: VSwitch: Type: ALIYUN::ECS::VSwitch Properties: VSwitchName: Ref: VSwitchName VpcId: Ref: Vpc CidrBlock: Ref: VSwitchCidrBlock ZoneId: Ref: ZoneId Vpc: Type: ALIYUN::ECS::VPC Properties: VpcName: MyTest CidrBlock: Ref: VpcCidrBlock Outputs: VpcId: Value: Fn::GetAtt: - Vpc - VpcId VSwitchId: Value: Fn::GetAtt: - VSwitch - VSwitchId
What to do next
Use the created template to create a stack or save the created template as your private template. For more information, see Getting started and Create a template.
References
You can refer to the following topics to configure advanced settings for templates:
Get started with templates: Define a template.
Metadata: Define the visual layout of a template.
Mappings: Define the mappings of a template.
Conditions and Functions: Define conditions by using built-in functions.
Rules: Define the rules of a template.
You can refer to the following topics to view the best practices of templates: