本文以TypeScript语言为例,为您介绍ROS CDK功能。

参数

ROS支持使用参数(Parameters)提高模板的灵活性和可复用性,您可以通过以下代码在ROS CDK中设置参数。

关于参数(Parameters)的更多信息,请参见概览

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
    });
  }
}

您可以解析ROS CDK代码,生成如下ROS模板:

{
  "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"
          ]
        }
      }
    }
  }
}

函数

ROS支持使用函数(Functions)管理资源栈,您可以通过以下代码在ROS CDK中设置函数。

关于函数(Functions)的更多信息,请参见函数(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',
        });
    }
}

您可以解析ROS CDK代码,生成如下ROS模板:

{
  "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
        ]
      }
    }
  }
}

条件

ROS支持在模板中定义条件(Conditions),根据您在创建或更新资源栈时,指定的输入参数值进行计算。在每个条件中,都可以引用其他条件、参数值或映射。您可以通过以下代码在ROS CDK中设置条件。

关于条件(Conditions)的更多信息,请参见条件(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')
        })
    }
}

您可以解析ROS CDK代码,生成如下ROS模板:

{
  "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"
        }
      }
    }
  }
}

映射

ROS支持Key-Value形式的映射(Mappings)表,您可以指定Key从而获取映射表的Value。您可以通过以下代码在ROS CDK中设置映射。

关于映射(Mappings)的更多信息,请参见映射(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',
        });
    }
}

您可以解析ROS CDK代码,生成如下ROS模板:

{
  "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"
        }
      }
    }
  }
}

输出

ROS支持在输出(Outputs)中定义在调用查询资源栈接口时返回的值。您可以通过以下代码在ROS CDK中设置输出。

关于输出(Outputs)的更多信息,请参见输出(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',
        });
    }
}

您可以解析ROS CDK代码,生成如下ROS模板:

{
  "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"
        ]
      }
    }
  }
}

伪参数

ROS支持将伪参数(Pseudo parameters)作为模板的固定参数。它们可以和用户定义的参数一样被引用,其值在ROS运行时确定。您可以通过以下代码在ROS CDK中设置伪参数。

关于伪参数(Pseudo parameters)的更多信息,请参见伪参数(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,
    });
  }
}

您可以解析ROS CDK代码,生成如下ROS模板:

{
  "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"
      }
    }
  }
}

元数据

ROS支持使用元数据(Metadata)对Parameters中定义的参数进行分组,并且为每一组分别定义标签。您可以通过以下代码在ROS CDK中设置元数据。

关于元数据(Metadata)的更多信息,请参见元数据(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": {
                            "zh-cn": "基础资源配置",
                            "en": "Infrastructure Configuration"
                        }
                    }
                },
                'TemplateTags': ['Demo Template']
            }
        });
    }
}

您可以解析ROS CDK代码,生成如下ROS模板:

{
  "Description": "This is the simple ros cdk app example.",
  "Metadata": {
    "ALIYUN::ROS::Interface": {
      "ParameterGroups": {
        "Parameters": [
          "VpcId"
        ],
        "Label": {
          "default": {
            "zh-cn": "基础资源配置",
            "en": "Infrastructure Configuration"
          }
        }
      },
      "TemplateTags": [
        "Demo Template",
        "Create by ROS CDK"
      ]
    }
  },
  "ROSTemplateFormatVersion": "2015-09-01",
  "Parameters": {
    "VpcId": {
      "Type": "String"
    }
  }
}

标签

ROS支持为创建的资源绑定标签。您可以通过以下代码在ROS CDK中设置标签。

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");
  }
}

您可以执行以下命令,查询资源栈绑定的标签详情。

ros-cdk list-stacks

返回结果如下:

✅ 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****"
	}
]

资源

ROS支持为模板中的资源设置DependsOn、Count和Condition属性,具体如下:

  • DependsOn

    设置DependsOn属性,可以指定特定资源紧跟着另一个资源后创建。为某个资源添加DependsOn属性后,该资源仅在DependsOn属性中指定的资源之后创建。您可以通过以下代码在ROS CDK中设置DependsOn属性。

    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);
      }
    }

    您可以解析ROS CDK代码,生成如下ROS模板:

    {
      "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

    为资源指定Count属性后,ROS会对模板进行预处理,把该资源展开成多个资源。 操作资源栈时使用处理后的模板。您可以通过以下代码在ROS CDK中设置Count属性。

    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)
        }
    }

    您可以解析ROS CDK代码,生成如下ROS模板:

    {
      "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

    使用Condition属性可以指定是否需要创建资源。只有Condition所指定的条件值为True时,才会创建此资源。您可以通过以下代码在ROS CDK中设置Condition属性。

    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)
        }
    }

    您可以解析ROS CDK代码,生成如下ROS模板:

    {
      "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"
        }
      }
    }