This topic provides an example on how to use Cloud Development Toolkit (CDK) of Resource Orchestration Service (ROS) to create a stack and use Alibaba Cloud CLI to query resource details of the stack. In this example, the TypeScript programming language is used.
Prerequisites
The following requirements are met:
Alibaba Cloud CLI is installed and configured. For more information, see What is Alibaba Cloud CLI?
ROS CDK is installed. For more information, see Install ROS CDK.
The JSON parsing toolkit
jq
is installed. For more information, see jq.
Step 1: Initialize a project
Each ROS CDK application must be created in a separate project directory. The application uses the dependencies of modules in the directory. Before you create an application, you must create a project directory and initialize the project.
Run the following commands to create a project directory and initialize the project:
mkdir demo
cd demo
ros-cdk init --language=typescript --generate-only=true
Step 2: Configure an Alibaba Cloud credential
Run the following command to configure an Alibaba Cloud credential:
ros-cdk config
Follow on-screen instructions to configure the credential parameters.
ros-cdk config endpoint(optional, default:https://ros.aliyuncs.com): defaultRegionId(optional, default:cn-hangzhou):cn-beijing [1] AK [2] StsToken [3] RamRoleArn [4] EcsRamRole [0] CANCEL Authenticate mode [1...4 / 0]: 1 accessKeyId:************************ accessKeySecret:****************************** ✅ Your cdk configuration has been saved successfully!
Step 3: Install dependencies
Modify the
package.json
file and add the ros-cdk-ecs dependency package of Elastic Compute Service (ECS).{ "name": "demo", "version": "0.1.0", "bin": { "demo": "bin/demo.js" }, "scripts": { "build": "tsc", "test": "jest" }, "devDependencies": { "@types/jest": "^25.2.1", "@types/node": "10.17.5", "typescript": "^3.9.7", "jest": "^25.5.0", "ts-jest": "^25.3.1", "ts-node": "^8.1.0", "babel-jest": "^26.6.3", "@babel/core": "^7.12.9", "@babel/preset-env": "7.12.7", "@babel/preset-typescript": "^7.12.7", "@alicloud/ros-cdk-assert": "^1.4.0" }, "dependencies": { "@alicloud/ros-cdk-core": "^1.4.0", "@alicloud/ros-cdk-ecs": "^1.4.0" } }
Run the following command to install the dependencies:
npm install
Step 4: Create a stack
Modify the
lib/demo-stack.ts
file to create a stack. You can define virtual private cloud (VPC) parameters in the code to create VPC resources.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, }); } }
Run the following command to generate a template file:
ros-cdk synth --json
Command output:
{ "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": { "Value": { "Fn::GetAtt": [ "vpc-from-ros-cdk", "VpcId" ] } } } }
Run the following command to create a stack and generate the
stack.outputs.json
file:ros-cdk deploy --sync=true --outputs-file=true
Command output:
DemoStack: deploying... |DemoStack |2021-12-28T10:57:20 | CREATE_COMPLETE | ALIYUN::ECS::VPC | vpc-2zefn3x56bka624k6**** | vpc-from-ros-cdk Outputs: Key: vpcId Value: vpc-2zefn3x56bka624k6**** Description: No description given ✅ The deployment(sync deploy stack) has finished! status: CREATE_COMPLETE StatusReason: Stack CREATE completed successfully StackId: 9dbd63f5-db09-4c16-b018-d3a3a7be****
Run the following command to query the output information about the stack:
cat stack.outputs.json
Command output:
{ "DemoStack": [ { "Description": "No description given", "OutputKey": "vpcId", "OutputValue": "vpc-2zefn3x56bka624ef****" } ] }
Step 5: Query resource details of the stack
Use Alibaba Cloud CLI to call the DescribeVpcAttribute operation to query the details of the VPC in the stack.
vpcId=`cat stack.outputs.json |jq '.DemoStack[0].OutputValue'| sed 's/\"//g'`
aliyun vpc DescribeVpcAttribute --VpcId $vpcId
Command output:
{
"Description": "This is ros cdk test",
"ClassicLinkEnabled": false,
"ResourceGroupId": "rg-acfm2xwmef****",
"SecondaryCidrBlocks": {
"SecondaryCidrBlock": []
},
"CidrBlock": "10.0.0.0/8",
"UserCidrs": {
"UserCidr": []
},
"NetworkAclNum": 0,
"VRouterId": "vrt-2zej8djrl78424efa****",
"OwnerId": 1754580XXXXXX,
"CloudResources": {
"CloudResourceSetType": [
{
"ResourceCount": 1,
"ResourceType": "VRouter"
},
{
"ResourceCount": 1,
"ResourceType": "RouteTable"
}
]
},
"Status": "Available",
"IsDefault": false,
"RequestId": "3A742895-A93F-5F67-BA81-CCEE88EGAD324",
"SupportIpv4Gateway": false,
"Ipv4GatewayId": "",
"VSwitchIds": {
"VSwitchId": []
},
"VpcId": "vpc-2zefn3x56bka624ef****",
"CreationTime": "2021-12-28T10:57:21Z",
"VpcName": "test-ros-cdk",
"RegionId": "cn-beijing",
"Ipv6CidrBlock": ""
}
Step 6: (Optional) Delete the stack
Run the following command to delete the stack:
ros-cdk destroy --sync=true
Follow on-screen instructions to confirm the deletion operation.
The following stack(s) will be destroyed(Only deployed stacks will be displayed). DemoStack Please confirm.(Y/N) y
Command output:
DemoStack: destroying... |DemoStack | DELETE_COMPLETE | ALIYUN::ECS::VPC | | vpc-from-ros-cdk ✅ The task(sync destroy stack) has finished! status: DELETE_COMPLETE StatusReason: Stack DELETE completed successfully StackId: 9dbd63f5-db09-4c16-b018-d3a3a7be****
References
API reference page
The CDK construct library provides a collection of API operations for you to build your CDK applications. For more information about how to use the API operations and the features of the API operations, go to the Ros Cdk References page.
CDK command
For more information about how to use CDK commands and the usage examples, see ROS CDK commands.
CDK feature
For more information about how to use CDK features such as outputs and pseudo parameters and the usage examples, see ROS CDK features.
GitHub repository
For more information about the official GitHub repository of ROS CDK, visit Resource-Orchestration-Service-Cloud-Development-Kit. On the website, you can submit issues and view licenses and release notes.