本文以TypeScript語言為例,為您介紹如何使用ROS CDK建立資源棧,然後使用阿里雲CLI查詢資源棧中的資源詳情。
前提條件
請確保您已經完成以下操作:
步驟一:初始化工程
每個ROS CDK應用都要求建立在一個獨立的工程目錄下,且該應用需要使用獨立工程目錄中模組的依賴項。所以在建立應用之前,需要先建立一個工程目錄並進行初始化。
執行以下命令,建立工程目錄並初始化工程。
mkdir demo
cd demo
ros-cdk init --language=typescript --generate-only=true步驟二:配置阿里雲憑證資訊
執行以下命令,配置阿里雲憑證資訊。
ros-cdk config根據介面提示輸入配置資訊。
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!
步驟三:安裝依賴
修改
package.json檔案,添加ECS依賴包(ros-cdk-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" } }執行以下命令,安裝依賴。
npm install
步驟四:建立資源棧
修改
lib/demo-stack.ts檔案,建立資源棧。您可以在代碼中定義VPC參數資訊,從而建立VPC資源。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, }); } }執行以下命令,產生模板檔案。
ros-cdk synth --json執行命令後,輸出以下內容:
{ "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" ] } } } }執行以下命令,建立資源棧並產生
stack.outputs.json檔案。ros-cdk deploy --sync=true --outputs-file=true執行命令後,輸出以下內容:
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****執行以下命令,查詢資源棧的輸出。
cat stack.outputs.json執行命令後,輸出以下內容:
{ "DemoStack": [ { "Description": "No description given", "OutputKey": "vpcId", "OutputValue": "vpc-2zefn3x56bka624ef****" } ] }
步驟五:查詢資源棧中的資源詳情
使用阿里雲CLI調用DescribeVpcAttribute介面,查詢VPC執行個體詳情。
vpcId=`cat stack.outputs.json |jq '.DemoStack[0].OutputValue'| sed 's/\"//g'`
aliyun vpc DescribeVpcAttribute --VpcId $vpcId執行命令後,輸出以下內容:
{
"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": ""
}步驟六(可選):刪除資源棧
執行以下命令,刪除資源棧。
ros-cdk destroy --sync=true根據介面提示,確認刪除。
The following stack(s) will be destroyed(Only deployed stacks will be displayed). DemoStack Please confirm.(Y/N) y執行命令後,輸出以下內容:
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****
更多參考
API參考頁面
CDK構造庫提供了一系列API,用於構建您的CDK應用程式。想要瞭解如何使用這些API及其特性,請參閱ROS CDK API參考。
命令簡介
想要瞭解如何使用CDK命令列及其使用樣本,請參閱ROS CDK命令簡介。
功能簡介
想要瞭解如何使用CDK輸出、偽參數等功能及其使用樣本,請參閱ROS CDK功能簡介。
GitHub倉庫
有關ROS CDK的官方GitHub存放庫,請參閱Resource-Orchestration-Service-Cloud-Development-Kit。在這裡,您可以提交問題,查看我們的許可證、發布記錄等。