This topic describes the features of Resource Orchestration Service (ROS) Cloud Development Kit (CDK). In this topic, the TypeScript language is used as an example.
Parameters
ROS allows you to define the Parameters section in a template. The Parameters section makes the template more adaptive and reusable. You can run the following ROS CDK code to define the Parameters section.
For more information about the Parameters section, see Overview.
import * as ros from '@alicloud/ros-cdk-core';
import * as kms from '@alicloud/ros-cdk-kms';
export class DemoStack extends ros.Stack {
constructor(scope: ros.Construct, id: string, props?: ros.StackProps) {
super(scope, id, props);
new ros.RosInfo(this, ros.RosInfo.description, "This is the simple ros cdk app example.");
// The code that defines your stack goes here
const pendingWindowInDays = new ros.RosParameter(this, 'PendingWindowInDays', {
description: 'The waiting period, specified in number of days. During this period, you can cancel the CMK in PendingDeletion status. After the waiting period expires, you cannot cancel the deletion. The value must be between 7 and 30. Default value is 30.',
minLength: 7,
maxLength: 30,
defaultValue: 7,
type: ros.RosParameterType.NUMBER
});
const aliasName = new ros.RosParameter(this, 'AliasName', {
description: 'The display name of the key. You can use the alias to call APIs such as Encrypt, GenerateDataKey, and DescribeKey. - Not including the prefix, the minimum length of an alias is 1 and the maximum length is 255. - The prefix alias/ must be included.',
defaultValue: "alias/demo",
type: ros.RosParameterType.STRING
});
const keyUsage = new ros.RosParameter(this, 'KeyUsage', {
description: 'The intended use of the CMK. Default value: ENCRYPT/DECRYPT.',
defaultValue: "ENCRYPT/DECRYPT",
type: ros.RosParameterType.STRING,
allowedValues: [
"ENCRYPT/DECRYPT",
"SIGN/VERIFY"
]
});
const keyEnable = new ros.RosParameter(this, 'keyEnable', {
description: 'Specifies whether the key is enabled. Defaults to true.',
defaultValue: true,
type: ros.RosParameterType.BOOLEAN,
allowedValues: [
true,
false
]
});
const Key = new kms.Key(this, 'ROS-KMS-Key', {
keyUsage: keyUsage,
enable: keyEnable,
pendingWindowInDays: pendingWindowInDays
});
const Alias = new kms.Alias(this, 'ROS-KMS-Alias', {
keyId: Key.attrKeyId,
aliasName: aliasName
});
}
}
The following code shows a sample ROS template that is generated after you run the ROS CDK code:
{
"Description": "This is the simple ros cdk app example.",
"Metadata": {
"ALIYUN::ROS::Interface": {
"TemplateTags": [
"Create by ROS CDK"
]
}
},
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": {
"PendingWindowInDays": {
"Type": "Number",
"Default": 7,
"Description": "The waiting period, specified in number of days. During this period, you can cancel the CMK in PendingDeletion status. After the waiting period expires, you cannot cancel the deletion. The value must be between 7 and 30. Default value is 30.",
"MaxLength": 30,
"MinLength": 7
},
"AliasName": {
"Type": "String",
"Default": "alias/demo",
"Description": "The display name of the key. You can use the alias to call APIs such as Encrypt, GenerateDataKey, and DescribeKey. - Not including the prefix, the minimum length of an alias is 1 and the maximum length is 255. - The prefix alias/ must be included."
},
"KeyUsage": {
"Type": "String",
"Default": "ENCRYPT/DECRYPT",
"AllowedValues": [
"ENCRYPT/DECRYPT",
"SIGN/VERIFY"
],
"Description": "The intended use of the CMK. Default value: ENCRYPT/DECRYPT."
},
"keyEnable": {
"Type": "Boolean",
"Default": true,
"AllowedValues": [
true,
false
],
"Description": "Specifies whether the key is enabled. Defaults to true."
}
},
"Resources": {
"ROS-KMS-Key": {
"Type": "ALIYUN::KMS::Key",
"Properties": {
"Enable": {
"Ref": "keyEnable"
},
"KeyUsage": {
"Ref": "KeyUsage"
},
"PendingWindowInDays": {
"Ref": "PendingWindowInDays"
}
}
},
"ROS-KMS-Alias": {
"Type": "ALIYUN::KMS::Alias",
"Properties": {
"AliasName": {
"Ref": "AliasName"
},
"KeyId": {
"Fn::GetAtt": [
"ROS-KMS-Key",
"KeyId"
]
}
}
}
}
}
Functions
ROS allows you to configure functions to manage stacks. You can run the following ROS CDK code to configure the functions.
For more information about the functions, see Functions.
import * as ros from '@alicloud/ros-cdk-core';
import * as ecs from '@alicloud/ros-cdk-ecs';
export class DemoStack extends ros.Stack {
constructor(scope: ros.Construct, id: string, props?: ros.StackProps) {
super(scope, id, props);
new ros.RosInfo(this, ros.RosInfo.description, "This is the simple ros cdk app example.");
// The code that defines your stack goes here
const vpcName = new ros.RosParameter(this, 'VpcName', {
type: ros.RosParameterType.STRING,
defaultValue:"VPC1,VPC2,VPC3"
});
const vswName = new ros.RosParameter(this, 'VSWName', {
type: ros.RosParameterType.JSON,
defaultValue: {"VSWName": "VSW1"}
});
const vpc = new ecs.Vpc(this, 'ROS-VPC', {
vpcName: ros.Fn.select(0, ros.Fn.split(',', vpcName.valueAsString)),
cidrBlock: '10.0.0.0/8',
enableIpv6: false,
description: 'This is the description of VPC',
});
const vswitch = new ecs.VSwitch(this, 'ROS-VSwitch', {
vpcId: vpc.getAtt('VpcId'),
vSwitchName: ros.Fn.getJsonValue('VSWName', vswName.valueAsAny),
cidrBlock: '10.0.0.0/20',
zoneId: ros.Fn.select(0, ros.Fn.getAzs(ros.Fn.ref('ALIYUN::Region'))),
});
vswitch.addDependency(vpc);
const SubResultOutput = new ros.RosOutput(this, 'result', {
value: ros.Fn.sub('Var1: ${Var1}, Var2: ${Var2}, StackName: ${ALIYUN::StackName}, Region: ${ALIYUN::Region}', {
Var1: 'Var1Value',
Var2: 'Var2Value',
}),
description: 'test Sub function result',
});
const maxResultOutput = new ros.RosOutput(this, 'maxVal', {
value: ros.Fn.max([1, 777]),
description: 'test max function',
});
const minResultOutput = new ros.RosOutput(this, 'minVal', {
value: ros.Fn.min([1, 777]),
description: 'test min function',
});
const addResultOutput = new ros.RosOutput(this, 'testFunction1', {
value: ros.Fn.add([3.14, 8.4]),
description: 'number type add',
});
}
}
The following code shows a sample ROS template that is generated after you run the ROS CDK code:
{
"Description": "This is the simple ros cdk app example.",
"Metadata": {
"ALIYUN::ROS::Interface": {
"TemplateTags": [
"Create by ROS CDK"
]
}
},
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": {
"VpcName": {
"Type": "String",
"Default": "VPC1,VPC2,VPC3"
},
"VSWName": {
"Type": "Json",
"Default": {
"VSWName": "VSW1"
}
}
},
"Resources": {
"ROS-VPC": {
"Type": "ALIYUN::ECS::VPC",
"Properties": {
"CidrBlock": "10.0.0.0/8",
"Description": "This is the description of VPC",
"EnableIpv6": false,
"VpcName": {
"Fn::Select": [
0,
{
"Fn::Split": [
",",
{
"Ref": "VpcName"
}
]
}
]
}
}
},
"ROS-VSwitch": {
"Type": "ALIYUN::ECS::VSwitch",
"Properties": {
"CidrBlock": "10.0.0.0/20",
"VpcId": {
"Fn::GetAtt": [
"ROS-VPC",
"VpcId"
]
},
"ZoneId": {
"Fn::Select": [
0,
{
"Fn::GetAZs": {
"Ref": "ALIYUN::Region"
}
}
]
},
"VSwitchName": {
"Fn::GetJsonValue": [
"VSWName",
{
"Ref": "VSWName"
}
]
}
},
"DependsOn": [
"ROS-VPC"
]
}
},
"Outputs": {
"result": {
"Description": "test Sub function result",
"Value": {
"Fn::Sub": [
"Var1: ${Var1}, Var2: ${Var2}, StackName: ${ALIYUN::StackName}, Region: ${ALIYUN::Region}",
{
"Var1": "Var1Value",
"Var2": "Var2Value"
}
]
}
},
"maxVal": {
"Description": "test max function",
"Value": {
"Fn::Max": [
1,
777
]
}
},
"minVal": {
"Description": "test min function",
"Value": {
"Fn::Min": [
1,
777
]
}
},
"testFunction1": {
"Description": "number type add",
"Value": {
"Fn::Add": [
3.14,
8.4
]
}
}
}
}
Conditions
ROS allows you to define conditions in a template. The conditions automatically generate the output based on the values of parameters that you set when you create or update stacks. You can reference other conditions, parameter values, or mappings in each condition. You can run the following ROS CDK code to configure the conditions.
For more information about the conditions, see Conditions.
import * as ros from '@alicloud/ros-cdk-core';
import * as rds from '@alicloud/ros-cdk-rds';
export class DemoStack extends ros.Stack {
constructor(scope: ros.Construct, id: string, props?: ros.StackProps) {
super(scope, id, props);
new ros.RosInfo(this, ros.RosInfo.description, "This is the simple ros cdk app example.");
// The code that defines your stack goes here
const rdsDBInstanceEngineType = new ros.RosParameter(this, 'RdsDBInstanceEngineType', {
type: ros.RosParameterType.STRING,
defaultValue: 'sqlserver',
allowedValues: [
'sqlserver',
'mysql'
]
});
const rdsDBInstanceId = new ros.RosParameter(this, 'RdsDBInstanceId', {
type: ros.RosParameterType.STRING,
});
const rdsDBName = new ros.RosParameter(this, 'RdsDBName', {
type: ros.RosParameterType.STRING,
});
const rdsEngineTypeConditions = new ros.RosCondition(this, 'RdsEngineTypeConditions', {
expression: ros.Fn.conditionEquals('mysql', rdsDBInstanceEngineType.valueAsString),
});
const rdsDataBase = new rds.Database(this, 'Database', {
dbInstanceId: rdsDBInstanceId.valueAsString,
dbName: rdsDBName.valueAsString,
characterSetName: ros.Fn.conditionIf(rdsEngineTypeConditions.node.id, 'utf8', 'Chinese_PRC_CI_AS')
})
}
}
The following code shows a sample ROS template that is generated after you run the ROS CDK code:
{
"Description": "This is the simple ros cdk app example.",
"Metadata": {
"ALIYUN::ROS::Interface": {
"TemplateTags": [
"Create by ROS CDK"
]
}
},
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": {
"RdsDBInstanceEngineType": {
"Type": "String",
"Default": "sqlserver",
"AllowedValues": [
"sqlserver",
"mysql"
]
},
"RdsDBInstanceId": {
"Type": "String"
},
"RdsDBName": {
"Type": "String"
}
},
"Conditions": {
"RdsEngineTypeConditions": {
"Fn::Equals": [
"mysql",
{
"Ref": "RdsDBInstanceEngineType"
}
]
}
},
"Resources": {
"Database": {
"Type": "ALIYUN::RDS::Database",
"Properties": {
"CharacterSetName": {
"Fn::If": [
"RdsEngineTypeConditions",
"utf8",
"Chinese_PRC_CI_AS"
]
},
"DBInstanceId": {
"Ref": "RdsDBInstanceId"
},
"DBName": {
"Ref": "RdsDBName"
}
}
}
}
}
Mappings
ROS supports key-value mappings. You can specify keys in a template to obtain the mapped values. You can run the following ROS CDK code to configure the mappings.
For more information about the mappings, see Mappings.
import * as ros from '@alicloud/ros-cdk-core';
import * as ecs from '@alicloud/ros-cdk-ecs';
export class DemoStack extends ros.Stack {
constructor(scope: ros.Construct, id: string, props?: ros.StackProps) {
super(scope, id, props);
new ros.RosInfo(this, ros.RosInfo.description, "This is the simple ros cdk app example.");
// The code that defines your stack goes here
const region = new ros.RosParameter(this, 'region', {
type: ros.RosParameterType.STRING,
allowedValues: ['cn-hangzhou', 'cn-beijing'],
});
const vpcId = new ros.RosParameter(this, 'VpcId', {
type: ros.RosParameterType.STRING,
});
const vSwitchId = new ros.RosParameter(this, 'VSwitchId', {
type: ros.RosParameterType.STRING,
});
const securityGroupId = new ros.RosParameter(this, 'SecurityGroupId', {
type: ros.RosParameterType.STRING,
});
const ecsImageInfo = new ros.RosMapping(this, 'ECSImageInfo', {
mapping: {
"cn-hangzhou": {
"32": "m-25l0rcfjo",
"64": "m-25l0rcfj1"
},
"cn-beijing": {
"32": "m-25l0rcfj2",
"64": "m-25l0rcfj3"
}
},
});
const ecsGroups = new ecs.InstanceGroup(this,'ros-cdk-test-ecs',{
vpcId: vpcId.valueAsString,
vSwitchId: vSwitchId.valueAsString,
imageId: ecsImageInfo.findInMap(region.valueAsString, '64'),
maxAmount: 1,
securityGroupId: securityGroupId.valueAsString,
instanceType:'ecs.c6.large',
instanceName: 'test-ros-cdk-ecs',
});
}
}
The following code shows a sample ROS template that is generated after you run the ROS CDK code:
{
"Description": "This is the simple ros cdk app example.",
"Metadata": {
"ALIYUN::ROS::Interface": {
"TemplateTags": [
"Create by ROS CDK"
]
}
},
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": {
"region": {
"Type": "String",
"AllowedValues": [
"cn-hangzhou",
"cn-beijing"
]
},
"VpcId": {
"Type": "String"
},
"VSwitchId": {
"Type": "String"
},
"SecurityGroupId": {
"Type": "String"
}
},
"Mappings": {
"ECSImageInfo": {
"cn-hangzhou": {
"32": "m-25l0rcfjo",
"64": "m-25l0rcfj1"
},
"cn-beijing": {
"32": "m-25l0rcfj2",
"64": "m-25l0rcfj3"
}
}
},
"Resources": {
"ros-cdk-test-ecs": {
"Type": "ALIYUN::ECS::InstanceGroup",
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"ECSImageInfo",
{
"Ref": "region"
},
"64"
]
},
"InstanceType": "ecs.c6.large",
"MaxAmount": 1,
"AllocatePublicIP": true,
"AutoRenew": "False",
"AutoRenewPeriod": 1,
"InstanceChargeType": "PostPaid",
"InstanceName": "test-ros-cdk-ecs",
"InternetChargeType": "PayByTraffic",
"InternetMaxBandwidthIn": 200,
"InternetMaxBandwidthOut": 1,
"IoOptimized": "optimized",
"Period": 1,
"PeriodUnit": "Month",
"SecurityGroupId": {
"Ref": "SecurityGroupId"
},
"SystemDiskCategory": "cloud_efficiency",
"VpcId": {
"Ref": "VpcId"
},
"VSwitchId": {
"Ref": "VSwitchId"
}
}
}
}
}
Outputs
ROS allows you to define the Outputs section in a template based on the values that are returned after you call the GetStack API operation. You can run the following ROS CDK code to define the Outputs section.
For more information about the Outputs section, see Outputs.
import * as ros from '@alicloud/ros-cdk-core';
import * as ecs from '@alicloud/ros-cdk-ecs';
export class DemoStack extends ros.Stack {
constructor(scope: ros.Construct, id: string, props?: ros.StackProps) {
super(scope, id, props);
new ros.RosInfo(this, ros.RosInfo.description, "This is the simple ros cdk app example.");
// The code that defines your stack goes here
const vpc = new ecs.Vpc(this, 'vpc-from-ros-cdk', {
vpcName: 'test-ros-cdk',
cidrBlock: '10.0.0.0/8',
description: 'This is ros cdk test'
});
const vpcId = new ros.RosOutput(this, 'vpcId', {
value: vpc.attrVpcId,
description: 'VpcId',
});
const vRouterId = new ros.RosOutput(this, 'VRouterId', {
value: vpc.attrVRouterId,
description: 'VRouterId',
});
const routeTableId = new ros.RosOutput(this, 'RouteTableId', {
value: vpc.attrRouteTableId,
description: 'RouteTableId',
});
}
}
The following code shows a sample ROS template that is generated after you run the ROS CDK code:
{
"Description": "This is the simple ros cdk app example.",
"Metadata": {
"ALIYUN::ROS::Interface": {
"TemplateTags": [
"Create by ROS CDK"
]
}
},
"ROSTemplateFormatVersion": "2015-09-01",
"Resources": {
"vpc-from-ros-cdk": {
"Type": "ALIYUN::ECS::VPC",
"Properties": {
"CidrBlock": "10.0.0.0/8",
"Description": "This is ros cdk test",
"EnableIpv6": false,
"VpcName": "test-ros-cdk"
}
}
},
"Outputs": {
"vpcId": {
"Description": "VpcId",
"Value": {
"Fn::GetAtt": [
"vpc-from-ros-cdk",
"VpcId"
]
}
},
"VRouterId": {
"Description": "VRouterId",
"Value": {
"Fn::GetAtt": [
"vpc-from-ros-cdk",
"VRouterId"
]
}
},
"RouteTableId": {
"Description": "RouteTableId",
"Value": {
"Fn::GetAtt": [
"vpc-from-ros-cdk",
"RouteTableId"
]
}
}
}
}
Pseudo parameters
ROS allows you to configure pseudo parameters as fixed parameters in a template. You can reference pseudo parameters in the manner in which you reference user-defined parameters. The values of the parameters are determined while ROS is running. You can run the following ROS CDK code to configure the pseudo parameters.
For more information about the pseudo parameters, see Pseudo parameters.
import * as ros from '@alicloud/ros-cdk-core';
export class DemoStack extends ros.Stack {
constructor(scope: ros.Construct, id: string, props?: ros.StackProps) {
super(scope, id, props);
new ros.RosInfo(this, ros.RosInfo.description, "This is the simple ros cdk app example.");
// The code that defines your stack goes here
const StackName = new ros.RosOutput(this, 'StackName', {
value: ros.RosPseudo.stackName,
});
const accountId = new ros.RosOutput(this, 'accountId', {
value: ros.RosPseudo.accountId,
});
const region = new ros.RosOutput(this, 'region', {
value: ros.RosPseudo.region,
});
const stackId = new ros.RosOutput(this, 'stackId', {
value: ros.RosPseudo.stackId,
});
const tenantId = new ros.RosOutput(this, 'tenantId', {
value: ros.RosPseudo.tenantId,
});
}
}
The following code shows a sample ROS template that is generated after you run the ROS CDK code:
{
"Description": "This is the simple ros cdk app example.",
"Metadata": {
"ALIYUN::ROS::Interface": {
"TemplateTags": [
"Create by ROS CDK"
]
}
},
"ROSTemplateFormatVersion": "2015-09-01",
"Outputs": {
"StackName": {
"Value": {
"Ref": "ALIYUN::StackName"
}
},
"accountId": {
"Value": {
"Ref": "ALIYUN::AccountId"
}
},
"region": {
"Value": {
"Ref": "ALIYUN::Region"
}
},
"stackId": {
"Value": {
"Ref": "ALIYUN::StackId"
}
},
"tenantId": {
"Value": {
"Ref": "ALIYUN::TenantId"
}
}
}
}
Metadata
ROS allows you to configure metadata to group the parameters that are defined in the Parameters section and configure tags for each group. You can run the following ROS CDK code to configure the metadata.
For more information about the metadata, see Metadata.
import * as ros from '@alicloud/ros-cdk-core';
export class DemoStack extends ros.Stack {
constructor(scope: ros.Construct, id: string, props?: ros.StackProps) {
super(scope, id, props);
new ros.RosInfo(this, ros.RosInfo.description, "This is the simple ros cdk app example.");
// The code that defines your stack goes here
const vpcId = new ros.RosParameter(this, 'VpcId', {
type: ros.RosParameterType.STRING,
});
const metaData = new ros.RosInfo(this, ros.RosInfo.metadata, {
'ALIYUN::ROS::Interface': {
'ParameterGroups': {
"Parameters": [
vpcId.node.id
],
"Label": {
"default": {
"en": "Infrastructure Configuration"
}
}
},
'TemplateTags': ['Demo Template']
}
});
}
}
The following code shows a sample ROS template that is generated after you run the ROS CDK code:
{
"Description": "This is the simple ros cdk app example.",
"Metadata": {
"ALIYUN::ROS::Interface": {
"ParameterGroups": {
"Parameters": [
"VpcId"
],
"Label": {
"default": {
"en": "Infrastructure Configuration"
}
}
},
"TemplateTags": [
"Demo Template",
"Create by ROS CDK"
]
}
},
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": {
"VpcId": {
"Type": "String"
}
}
}
Tags
ROS allows you to configure tags for resources after you create the resources. You can run the following ROS CDK code to configure the tags.
import * as ros from '@alicloud/ros-cdk-core';
import * as ecs from '@alicloud/ros-cdk-ecs';
export class DemoStack extends ros.Stack {
constructor(scope: ros.Construct, id: string, props?: ros.StackProps) {
super(scope, id, props);
new ros.RosInfo(this, ros.RosInfo.description, "This is the simple ros cdk app example.");
// The code that defines your stack goes here
new ecs.Vpc(this, 'vpc-from-ros-cdk', {
vpcName: 'test-ros-cdk',
cidrBlock: '10.0.0.0/8',
description: 'This is ros cdk test'
});
this.tags.setTag("MySampleTag", "MyTagValue");
}
}
You can run the following command to query the details about the tags that are configured for a stack:
ros-cdk list-stacks
A similar output is returned:
The Stacks list is:
[
{
"Status": "CREATE_COMPLETE",
"StackType": "ROS",
"ResourceGroupId": "rg-acfm2xw4X5w****",
"StatusReason": "Stack CREATE completed successfully",
"CreateTime": "2021-12-27T11:09:22",
"RegionId": "cn-beijing",
"DisableRollback": false,
"StackName": "DemoStack",
"Tags": [
{
"Value": "rg-acfm2xw4X5w****",
"Key": "acs:rm:rgId"
},
{
"Value": "MyTagValue",
"Key": "MySampleTag"
}
],
"TimeoutInMinutes": 20,
"StackId": "7a2a9f34-ae88-41d0-aae0-51aeerE****"
}
]
Resources
ROS allows you to configure the DependsOn, Count, and Condition properties for resources in a template.
- DependsOn
The DependsOn property allows you to specify that the creation of a specific resource follows another. When you add a DependsOn property to a resource, the resource is created only after the resource that is specified in the DependsOn property is created. You can run the following ROS CDK code to specify the DependsOn property:
import * as ros from '@alicloud/ros-cdk-core'; import * as ecs from '@alicloud/ros-cdk-ecs'; export class DemoStack extends ros.Stack { constructor(scope: ros.Construct, id: string, props?: ros.StackProps) { super(scope, id, props); new ros.RosInfo(this, ros.RosInfo.description, "This is the simple ros cdk app example."); // The code that defines your stack goes here const vpc = new ecs.Vpc(this, 'ROS-VPC', { vpcName: 'VPC_Name', cidrBlock: '10.0.0.0/8', enableIpv6: false, description: 'This is the description of VPC', }); const vswitch = new ecs.VSwitch(this, 'ROS-VSwitch', { vpcId: vpc.getAtt('VpcId'), zoneId: 'cn-beijing-h', vSwitchName: 'VSwitch_Name', cidrBlock: '10.0.0.0/20' }); vswitch.addDependency(vpc); } }
The following code shows a sample ROS template that is generated after you run the ROS CDK code:
{ "Description": "This is the simple ros cdk app example.", "Metadata": { "ALIYUN::ROS::Interface": { "TemplateTags": [ "Create by ROS CDK" ] } }, "ROSTemplateFormatVersion": "2015-09-01", "Resources": { "ROS-VPC": { "Type": "ALIYUN::ECS::VPC", "Properties": { "CidrBlock": "10.0.0.0/8", "Description": "This is the description of VPC", "EnableIpv6": false, "VpcName": "VPC_Name" } }, "ROS-VSwitch": { "Type": "ALIYUN::ECS::VSwitch", "Properties": { "CidrBlock": "10.0.0.0/20", "VpcId": { "Fn::GetAtt": [ "ROS-VPC", "VpcId" ] }, "ZoneId": "cn-beijing-h", "VSwitchName": "VSwitch_Name" }, "DependsOn": [ "ROS-VPC" ] } } }
- Count
The Count property allows ROS to preprocess the template of a resource and expand the resource into multiple resources. You can use the processed template to manage stacks. You can run the following ROS CDK code to specify the Count property:
import * as ros from '@alicloud/ros-cdk-core'; import * as ecs from '@alicloud/ros-cdk-ecs'; export class DemoStack extends ros.Stack { constructor(scope: ros.Construct, id: string, props?: ros.StackProps) { super(scope, id, props); new ros.RosInfo(this, ros.RosInfo.description, "This is the simple ros cdk app example."); // The code that defines your stack goes here const vpc = new ecs.Vpc(this, 'ROS-VPC', { cidrBlock: '10.0.0.0/8', enableIpv6: false, }); vpc.addCount(2) const vsw =new ecs.VSwitch(this, `vsw-from-ros-cdk`, { vpcId: ros.Fn.select(ros.RosPseudo.index, vpc.attrVpcId), zoneId: ros.Fn.select(ros.RosPseudo.index, ros.Fn.getAzs(ros.RosPseudo.region)), cidrBlock: '10.0.0.0/16', }); vsw.addCount(2) } }
The following code shows a sample ROS template that is generated after you run the ROS CDK code:
{ "Description": "This is the simple ros cdk app example.", "Metadata": { "ALIYUN::ROS::Interface": { "TemplateTags": [ "Create by ROS CDK" ] } }, "ROSTemplateFormatVersion": "2015-09-01", "Resources": { "ROS-VPC": { "Type": "ALIYUN::ECS::VPC", "Properties": { "CidrBlock": "10.0.0.0/8", "EnableIpv6": false }, "Count": 2 }, "vsw-from-ros-cdk": { "Type": "ALIYUN::ECS::VSwitch", "Properties": { "CidrBlock": "10.0.0.0/16", "VpcId": { "Fn::Select": [ { "Ref": "ALIYUN::Index" }, { "Fn::GetAtt": [ "ROS-VPC", "VpcId" ] } ] }, "ZoneId": { "Fn::Select": [ { "Ref": "ALIYUN::Index" }, { "Fn::GetAZs": { "Ref": "ALIYUN::Region" } } ] } }, "Count": 2 } } }
- Condition
The Condition property specifies whether to create a resource. The resource can be created only if you set the Condition property to True. You can run the following ROS CDK code to specify the Condition property:
import * as ros from '@alicloud/ros-cdk-core'; import * as ecs from '@alicloud/ros-cdk-ecs'; export class DemoStack extends ros.Stack { constructor(scope: ros.Construct, id: string, props?: ros.StackProps) { super(scope, id, props); new ros.RosInfo(this, ros.RosInfo.description, "This is the simple ros cdk app example."); // The code that defines your stack goes here const createVPC = new ros.RosParameter(this, 'CreateVPC', { type: ros.RosParameterType.BOOLEAN, defaultValue: true, }); const isCreateVPC = new ros.RosCondition(this, 'IsCreateVPC', { expression: ros.Fn.conditionEquals(true, createVPC.valueAsBoolean), }); const vpc = new ecs.Vpc(this, 'ROS-VPC', { vpcName: 'VPC_Name', cidrBlock: '10.0.0.0/8', enableIpv6: false, description: 'This is the description of VPC', }); vpc.addCondition(isCreateVPC) } }
The following code shows a sample ROS template that is generated after you run the ROS CDK code:
{ "Description": "This is the simple ros cdk app example.", "Metadata": { "ALIYUN::ROS::Interface": { "TemplateTags": [ "Create by ROS CDK" ] } }, "ROSTemplateFormatVersion": "2015-09-01", "Parameters": { "CreateVPC": { "Type": "Boolean", "Default": true } }, "Conditions": { "IsCreateVPC": { "Fn::Equals": [ true, { "Ref": "CreateVPC" } ] } }, "Resources": { "ROS-VPC": { "Type": "ALIYUN::ECS::VPC", "Properties": { "CidrBlock": "10.0.0.0/8", "Description": "This is the description of VPC", "EnableIpv6": false, "VpcName": "VPC_Name" }, "Condition": "IsCreateVPC" } } }