文档

使用ROS CDK在同一个工程内切换应用管理资源栈

更新时间:

本文以TypeScript语言为例,为您介绍如何使用ROS CDK在同一个工程内切换应用管理资源栈。

步骤一:初始化工程

每个ROS CDK应用都要求创建在一个独立的工程目录下,且该应用需要使用独立工程目录中模块的依赖项。所以在创建应用之前,需要先创建一个工程目录并进行初始化。

执行以下命令,创建工程目录并初始化工程。

mkdir demo
cd demo
ros-cdk init --language=typescript --generate-only=true

步骤二:配置阿里云凭证信息

  1. 执行以下命令,配置阿里云凭证信息。

    ros-cdk config
  2. 根据界面提示输入配置信息。

    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!

步骤三:安装依赖

  1. 修改package.json文件,添加ECS依赖包(ros-cdk-ecs)和OSS依赖包(ros-cdk-oss)。

    {
      "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.0.0"
      },
      "dependencies": {
        "@alicloud/ros-cdk-core": "^1.0.5",
        "@alicloud/ros-cdk-ecs": "^1.0.3",
        "@alicloud/ros-cdk-oss": "^1.0.3"
      }
    }
  2. 执行以下命令,安装依赖。

    npm install

步骤四:在同一个应用中管理多个资源栈

  1. 修改bin/demo.ts文件,以便在同一个应用中管理多个资源栈(资源栈DemoStack和资源栈DemoStack2)。

    #!/usr/bin/env node
    import * as ros from '@alicloud/ros-cdk-core';
    import { DemoStack } from '../lib/demo-stack';
    import { DemoStack2 } from '../lib/demo-stack';
    
    const app = new ros.App({outdir: './cdk.out'});
    new DemoStack(app, 'DemoStack');
    new DemoStack2(app, 'DemoStack2');
    app.synth();
  2. 修改lib/demo-stack.ts文件,在资源栈DemoStack中创建专有网络,在资源栈DemoStack2中创建对象存储OSS的存储空间。

    import * as ros from '@alicloud/ros-cdk-core';
    import * as ecs from '@alicloud/ros-cdk-ecs';
    import * as oss from '@alicloud/ros-cdk-oss';
    
    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'
            });
        }
    }
    
    
    export class DemoStack2 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 oss.Bucket(this, 'oss-from-ros-cdk', {
                bucketName: 'oss-from-ros-cdk'
                }
            )
        }
    }
  3. 生成模板文件。

    • 为资源栈DemoStack生成模板文件

      执行以下命令:

      ros-cdk synth DemoStack -j

      执行命令后,输出以下内容:

      {
        "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"
            }
          }
        }
      }
    • 为资源栈DemoStack2生成模板文件

      执行以下命令:

      ros-cdk synth DemoStack2 -j

      执行命令后,输出以下内容:

      {
        "Description": "This is the simple ros cdk app example.",
        "Metadata": {
          "ALIYUN::ROS::Interface": {
            "TemplateTags": [
              "Create by ROS CDK"
            ]
          }
        },
        "ROSTemplateFormatVersion": "2015-09-01",
        "Resources": {
          "oss-from-ros-cdk": {
            "Type": "ALIYUN::OSS::Bucket",
            "Properties": {
              "BucketName": "oss-from-ros-cdk",
              "AccessControl": "private",
              "DeletionForce": false
            }
          }
        }
      }
  4. 创建资源栈。

    • 创建资源栈DemoStack

      执行以下命令:

      ros-cdk deploy DemoStack --sync=true

      执行命令后,输出以下内容:

      DemoStack: deploying...
      |DemoStack               |2021-12-29T06:56:00 | CREATE_COMPLETE      | ALIYUN::ECS::VPC        | vpc-2ze18c8hsomme5ps**** | vpc-from-ros-cdk
      
      
       ✅ The deployment(sync deploy stack) has finished!
      status: CREATE_COMPLETE
      StatusReason: Stack CREATE completed successfully
      StackId: e002686b-d269-41df-bdf4-27221b1****
    • 创建资源栈DemoStack2

      执行以下命令:

      ros-cdk deploy DemoStack2 --sync=true

      执行命令后,输出以下内容:

      DemoStack2: deploying...
      |DemoStack2              |2021-12-29T06:56:24 | CREATE_COMPLETE      | ALIYUN::OSS::Bucket     | oss-from-ros-cdk | oss-from-ros-cdk
      
      
       ✅ The deployment(sync deploy stack) has finished!
      status: CREATE_COMPLETE
      StatusReason: Stack CREATE completed successfully
      StackId: 502a2cbe-3ab2-49fa-bcaf-602c3b88****
  5. 可选:删除资源栈。

    • 删除资源栈DemoStack

      1. 执行以下命令:

        ros-cdk destroy DemoStack --sync=true
      2. 根据界面提示,确认删除。

        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: e002686b-d269-41df-bdf4-27221****
    • 删除资源栈DemoStack2

      1. 执行以下命令:

        ros-cdk destroy DemoStack2 --sync=true
      2. 根据界面提示,确认删除。

        The following stack(s) will be destroyed(Only deployed stacks will be displayed).
        
        DemoStack2
        
        Please confirm.(Y/N)
        y

        执行命令后,输出以下内容:

        DemoStack2: destroying...
        |DemoStack2              | DELETE_COMPLETE      | ALIYUN::OSS::Bucket     |  | oss-from-ros-cdk
        
         
        ✅ The task(sync destroy stack) has finished!
        status: DELETE_COMPLETE
        StatusReason: Stack DELETE completed successfully
        StackId: 502a2cbe-3ab2-49fa-bcaf-602c3b88****

步骤五:切换应用管理资源栈

当您需要在同一个工程目录下使用多个应用(App)管理资源栈时,可以使用ros-cdk --app命令。

  1. 创建新应用。

    1. 在当前目录创建bin/test.ts文件,管理资源栈TestStack

      #!/usr/bin/env node
      import * as ros from '@alicloud/ros-cdk-core';
      import { TestStack } from '../lib/test-stack';
      
      const app = new ros.App({outdir: './cdk.out'});
      new TestStack(app, 'TestStack');
      app.synth();
    2. 在当前目录创建lib/test-stack.ts文件,在资源栈TestStack中创建ECS安全组。

      import * as ros from '@alicloud/ros-cdk-core';
      import * as ecs from '@alicloud/ros-cdk-ecs';
      
      export class TestStack 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 sg = new ecs.SecurityGroup(this, 'ros-cdk-test-sg');
      
          }
      }
  2. 使用新应用管理资源栈。

    1. 执行以下命令,生成模板文件。

      ros-cdk synth TestStack -j  --app "npx ts-node bin/test.ts"

      执行命令后,输出以下内容:

      {
        "Description": "This is the simple ros cdk app example.",
        "Metadata": {
          "ALIYUN::ROS::Interface": {
            "TemplateTags": [
              "Create by ROS CDK"
            ]
          }
        },
        "ROSTemplateFormatVersion": "2015-09-01",
        "Resources": {
          "ros-cdk-test-sg": {
            "Type": "ALIYUN::ECS::SecurityGroup"
          }
        }
      }
    2. 执行以下命令,创建资源栈TestStack

      ros-cdk deploy TestStack --sync=true --app "npx ts-node bin/test.ts"

      执行命令后,输出以下内容:

      TestStack: deploying...
      |TestStack               |2021-12-28T09:41:58 | CREATE_COMPLETE      | ALIYUN::ECS::SecurityGroup | sg-2ze7zn4sk5i9fgmg**** | ros-cdk-test-sg
      
      
       ✅ The deployment(sync deploy stack) has finished!
      status: CREATE_COMPLETE
      StatusReason: Stack CREATE completed successfully
      StackId: 1ef89784-a728-46b2-8ec0-bc0f14c9****
    3. 可选:删除资源栈TestStack

      1. 执行以下命令:

        ros-cdk destroy --sync=true --app "npx ts-node bin/test.ts"
      2. 根据界面提示,确认删除。

        The following stack(s) will be destroyed(Only deployed stacks will be displayed).
        
        TestStack
        
        Please confirm.(Y/N)
        y

        执行命令后,输出以下内容:

        TestStack: destroying...
        |TestStack               |2021-12-28T09:41:58 | DELETE_COMPLETE      | ALIYUN::ECS::SecurityGroup | sg-2ze7zn4sk5i9fgmg**** | ros-cdk-test-sg
        
        
         ✅ The task(sync destroy stack) has finished!
        status: DELETE_COMPLETE
        StatusReason: Stack DELETE completed successfully
        StackId: 1ef89784-a728-46b2-8ec0-bc0f14c9****
  • 本页导读 (1)