A template defines cloud resources and their dependencies. Resource Orchestration Service (ROS) deploys templates as stacks. This guide covers writing, testing, and parameterizing templates.
Overview
After you understand ROS template structure, try writing your first template. Quick start for writing a template.
This guide starts with a simple VPC template, then covers resource dependencies and parameterization:
Write a template
Declare resources in the Resources section. Browse supported types and properties in the Resource type index. Quick start for writing a template. The following sample creates a VPC:
ROSTemplateFormatVersion: '2015-09-01'
Resources:
VPC:
Type: ALIYUN::ECS::VPC
Properties:
VpcName: myvpc
CidrBlock: 192.168.0.0/16
Outputs:
VpcId:
Value:
Ref: VPC
VRouterId:
Value:
Fn::GetAtt:
- VPC
- VRouterId
Test the template
Create a stack from the template to verify that it provisions resources as expected.
-
Log in to the ROS console.
-
Create a stack.
-
In the left-side navigation pane, click Stacks.
-
In the top navigation bar, select the region for the stack from the region drop-down list, for example, China (Hangzhou).
-
On the Stacks page, click Create Stack. In the Specify Template section, click Select an Existing Template.
Note-
If you select Create Template or ROS Infrastructure Composer, you are redirected to the corresponding page.
-
If you select Synchronize Template from Code Repository, see Manage stacks by using a code repository.
-
-
On the Select Template page, click Select an Existing Template in the Specify Template area. Set Template Import Method to Enter Template Content. In the Template Content section, select the ROS tab, paste the YAML template from the Write a template section, and then click Next.
-
On the Configure Parameters page, enter a Stack Name.
-
In the Configure Stack Settings section, configure settings such as Rollback on Failure, Timeout Period, Tags, Resource Group, Stack Policy, Maximum Concurrent Resources, Deletion Protection, RAM Role, Stack Event Callback URLs, and Manual Payment, and then click Next.
-
On the Check and Confirm page, click Preview Template Resources. In the Preview dialog box, review the ROS-validated resource names, types, and properties, and then click OK.
-
On the Check and Confirm page, click Create.
-
-
View the stack.
-
On the stack management page, click the Event tab to view the list of events for the resources in the template.
-
Click the Resources tab to view details of the created resources.
NoteClick a resource ID to open its console and verify the resource configuration.
-
Click the Output tab to view the outputs defined in the
Outputssection of the template.
-
Define multiple resources and their dependencies
A vSwitch must belong to a VPC. Define both resources and their dependency in a single template to provision complex environments in one stack.
-
Use functions to get the value of a resource's output property.
For example, if a VPC resource is defined in the
Resourcessection, you can use{"Fn::GetAtt": ["VPC", "VpcId"]}to get itsVpcIdoutput property. -
Use functions to get a resource ID or a parameter value.
For example, if a VPC resource is defined in the
Resourcessection, you can use{"Ref": "VPC"}to reference its resource ID.Note-
{"Ref": "VPC"}returns the same value as{"Fn::GetAtt": ["VPC", "VpcId"]}, but is more concise. -
RefandFn::GetAttimplicitly declare resource dependencies. To explicitly declare dependencies, use the DependsOn attribute.
-
-
Add a vSwitch resource.
The following template adds a vSwitch named
myvswin zonecn-beijing-fwith CIDR block192.168.0.0/24, referencing the VPC. TheOutputssection includes the vSwitch ID.ROSTemplateFormatVersion: '2015-09-01' Resources: VPC: Type: ALIYUN::ECS::VPC Properties: VpcName: myvpc CidrBlock: 192.168.0.0/16 VSwitch: Type: ALIYUN::ECS::VSwitch Properties: VpcId: Ref: VPC ZoneId: cn-beijing-f VSwitchName: myvsw CidrBlock: 192.168.0.0/24 Outputs: VpcId: Value: Fn::GetAtt: - VPC - VpcId VRouterId: Value: Fn::GetAtt: - VPC - VRouterId VSwitchId: Value: Ref: VSwitch
Define parameter values and properties
The Parameters section makes templates reusable. Define parameters to accept different values at stack creation without editing the template.
-
Define parameters
Hard-coding properties like
ZoneIdlimits a template to one configuration. Define parameters so each stack creation can supply different values. The following table describes the parameters in the sample template.-
Parameters in the template
Parameter
Description
ZoneIdSets the vSwitch availability zone.
VpcCidrBlockSets the VPC CIDR block. Default:
192.168.0.0/16.VSwitchCidrBlockSets the vSwitch CIDR block. Default:
192.168.0.0/24. -
Sample template code
ROSTemplateFormatVersion: '2015-09-01' Parameters: ZoneId: Type: String VpcCidrBlock: Type: String Default: 192.168.0.0/16 VSwitchCidrBlock: Type: String Default: 192.168.0.0/24 Resources: VPC: Type: ALIYUN::ECS::VPC Properties: VpcName: myvpc CidrBlock: Ref: VpcCidrBlock VSwitch: Type: ALIYUN::ECS::VSwitch Properties: VpcId: Ref: VPC ZoneId: Ref: ZoneId VSwitchName: myvsw CidrBlock: Ref: VSwitchCidrBlock Outputs: VpcId: Value: Ref: VPC VRouterId: Value: Fn::GetAtt: - VPC - VRouterId VSwitchId: Value: Ref: VSwitchWhen you create a stack, set parameter values in the ROS console.
ROS infers valid values from resource property associations. For example, because
ZoneIdmaps to the vSwitchZoneIdproperty, the console shows only zones that support vSwitches.
-
-
Dynamically set allowed values
The Parameters section supports properties that customize console behavior. The following table describes the properties in the sample template.
-
Parameter properties
Property
Description
AllowedValues
Restricts input to a predefined list, shown as a drop-down in the console. Example:
VpcCidrBlockallows10.0.0.0/8,172.16.0.0/12, and192.168.0.0/16.Label
Sets a display name for the parameter in the console. Example:
ZoneIdappears asAvailability Zone ID. -
Sample template code
ROSTemplateFormatVersion: '2015-09-01' Parameters: ZoneId: Type: String Label: Availability Zone ID VpcCidrBlock: Type: String Label: VPC CIDR Block Default: 192.168.0.0/16 AllowedValues: - 10.0.0.0/8 - 172.16.0.0/12 - 192.168.0.0/16 VSwitchCidrBlock: Type: String Label: vSwitch CIDR Block Default: 192.168.0.0/24When you create a stack, select parameter values from drop-down lists in the ROS console.
-
-
Dynamically present parameters based on associations
Use AssociationProperty and AssociationPropertyMetadata to let the console dynamically query and display allowed values for a parameter.
For example, if a template uses
VpcIdandvSwitchIdas parameters, the console auto-populates drop-down lists:ROSTemplateFormatVersion: '2015-09-01' Parameters: VpcId: Type: String AssociationProperty: ALIYUN::ECS::VPC::VPCId ZoneId: Type: String AssociationProperty: ALIYUN::ECS::ZoneId VSwitchId: Type: String AssociationProperty: ALIYUN::ECS::VSwitch::VSwitchId AssociationPropertyMetadata: ZoneId: ${ZoneId} VpcId: ${VpcId}When you create a stack, parameters are dynamically populated in the console.
-
Group similar parameters
Use the Metadata section to group related parameters for easier configuration in the console.
For example, group
ZoneId,VpcCidrBlock, andVSwitchCidrBlockinto two groups: Basic Settings (ZoneId) and Resource Settings (VpcCidrBlock,VSwitchCidrBlock):ROSTemplateFormatVersion: '2015-09-01' Parameters: ZoneId: Type: String VpcCidrBlock: Type: String Default: 192.168.0.0/16 VSwitchCidrBlock: Type: String Default: 192.168.0.0/24 Metadata: ALIYUN::ROS::Interface: ParameterGroups: - Parameters: - ZoneId Label: default: Basic Settings - Parameters: - VpcCidrBlock - VSwitchCidrBlock Label: default: Resource SettingsWhen you create a stack, parameters appear grouped in the console.