快速入门-2分钟使用CADT API快速创建ECS

CADT API 包含了一组OpenAPI接口,为了快速了解集成API后如何快速创建ECS,下面通过一个集成demo cadt 命令行工具帮助大家快速体验效果。

操作前提

  • 请确保您已安装了Python,并且Python版本在3.6以上。

  • 安装Git,如未安装,请参考安装Git

  • 创建阿里云账号,并生成RAM 账号AK/SK。

操作步骤

  1. 快速安装CADT命令行工具。

    git clone https://code.aliyun.com/best-practice/cadt-cli.git
    cd cadt-cli
    sudo pip3 install -e . 
  2. 工具初始化。

    说明

    需要输入阿里云账号AK/SK,注意确保账号具有ECS管理权限,VPC创建权限,以及CADT管理权限,详情参见权限说明。

    % cadt --help
    2023-07-15 10:23:41 : [Errno 2] No such file or directory: './.cadt.config'
    To use cadt command line tools you need to specified aliyun access key and secret. 
    Enter your KEY ID: xxxxxxxxxx
    Enter your KEY Secret: xxxxxxxxxxxxxxx
    region [中国站账号输入:cn-hangzhou, 国际站账号输入: ap-southeast-1]:: cn-hangzhou
    2023-07-15 10:23:58 : your access key is stored in local file: ./.cadt.config. You should delete it after you work is done.
    Usage: cadt [OPTIONS] COMMAND [ARGS]...
    
    Options:
      --help  Show this message and exit.
    
    Commands:
      apply               创建应用,校验,询价,并输出结果,用户输入yes后执行部署
      batch-apply         批量创建应用,校验,询价,执行部署
      create-app          基于CADT上官方模板或私有模板创建新的应用
      delete-app          删除应用
      deploy              部署应用并创建资源
      download-app-image  获取应用的架构图
      get-app             获取指定应用的基本信息、校验结果、计费结果、部署结果
      get-template        获取模板图片及架构图相关信息
      list-apps           该接口提供当前用户下所有应用的列表。其中可选参数keyword定义应用名中包含的关键字
      list-templates      获取模板列表,包括模板名称、架构图地址、架构图序列化文件地址等
      release             该接口释放应用的资源
      step-apply          按顺序创建应用,校验,询价,执行部署
      validate            对应用下的资源输入的合法性进行校验,异步执行,校验结果调用GetApplication获取
      valuate             对应用下的资源进行询价,通过GetApplication获取询价结果
      valuate-template    基于CADT上官方模板或私有模板进行询价
  3. (可选)定义ECS模型YAML文件。

    1. 配置文件。

      template_id: UWWDJZTL4UYBVFPQ
      instances:
        # - node_type: vpc
        #   node_name: vpc
        #   id: vpc-uf6wipm4ow7ivck7sln4k
        # - node_type: vswitch
        #   node_name: vswitch
        #   id: vsw-uf6eld3lyrw6k5hobk80r
        # - node_type: security_group
        #   node_name: security_group
        #   id: sg-bp13sgmc9pqh07em6qnw
      
      variables:
          ${Amount}: "2"
          ${InstanceName}: "ECS001"
          ${InstanceType}: "ecs.c6.xlarge"
      #    ${ImageFamily}: ""
      #    ${ImageId}: ""
          ${SystemDiskCategory}: "cloud_essd"
          ${SystemDiskPerformanceLevel}: "PL0"
          ${SystemDiskSize}: "100"
          ${DataDisk}: '[{"size":"50","performance_level":"PL1","name":"disk1","category":"cloud_essd"}]'
          ${HostName}: "ecs0001"
          ${Description}: "ECS Test"
          ${EnablePublicIpAddress}: "False"
          ${InternetMaxBandwidth}: "1"
          ${UserData}: ""
          ${PrivateIpAddress}: ""
          ${ResourceGroupId}: ""
          ${Tags}: "{'成本标签':'基础设施','env':'prod','module':'ecs'}"
      
      configuration:
        enableMonitor: 0
        enableReport: 0
      
      output:
          ecs:
            - Private_ip
    2. 参数说明。

      • template_id :UWWDJZTL4UYBVFPQ

        CADT官方提供的ECS创建模板,该模型配置VPC,虚拟交换机,安全组,以及ECS,模型中的资源属性支持变量,具体参见单产品模板示例-ECS。

      • instances:网络相关参数

      • variables:模板变量传参

      • configuration:部署优化选项(默认)

      • output:输出

    3. 保存配置文件,例如保存为ecs.yaml。

  4. 执行部署。

    1. 直接指定模板ID和地域进行部署。

      cadt apply -t  UWWDJZTL4UYBVFPQ -a cn-hangzhou

      或者指定配置文件部署:

      cadt apply -f ecs.yaml 
    2. 输出结果。

      % cadt apply -f ecs.yml
      
      Welcome to use CADT : A Cloud Architecture Design Tool.
      Visit our website https://bpstudio.console.aliyun.com/ to learn more. 
      
      CCCCC     AAA   DDDDD   TTTTTTT 
      CC   C   AAAAA  DD  DD    TTT   
      CC      AA   AA DD   DD   TTT   
      CC   C  AAAAAAA DD   DD   TTT   
      CCCCC   AA   AA DDDDDD    TTT   
      
      
      2023-07-15 10:24:13 : CADT app cadt-app-EOSALPQAAV Created Successfully! App ID: VNYLA21W0DKLD1C6
      2023-07-15 10:24:13 : CADT validate app request call Successful! Request ID: FEC824CF-8230-52A8-85EE-754E7EDFCCDA
      2023-07-15 10:24:18 : app status: Verified_Success
      +----------------+-------------+----------------+-----------+--------+----------+
      | resource type  |    region   | specification  | lifecycle | Result |  Remark  |
      +----------------+-------------+----------------+-----------+--------+----------+
      |    vswitch     | cn-hangzhou | 192.168.0.0/24 |   Create  | Finish | 校验通过 |
      |     region     | cn-hangzhou |                |    None   | Finish | 校验通过 |
      | security_group | cn-hangzhou |     normal     |   Create  | Finish | 校验通过 |
      |      ecs       | cn-hangzhou |  ecs.c6.large  |   Create  | Finish | 校验通过 |
      |      vpc       | cn-hangzhou | 192.168.0.0/16 |   Create  | Finish | 校验通过 |
      +----------------+-------------+----------------+-----------+--------+----------+
      2023-07-15 10:24:18 : CADT valuate app request call Successful! Request ID: D663AA2F-BEB3-5625-983D-BF7A23222AFA
      2023-07-15 10:24:23 : app status: Valuating_Success
      +----------------+-------------+----------------+----------------+---------------+----------+--------+-------+-----------+-------+------------+--------+-----------+
      | resource type  |    region   |  instanceName  | specification  | originalPrice | onePrice | period | count | priceUnit | price | chargeType | remark | lifecycle |
      +----------------+-------------+----------------+----------------+---------------+----------+--------+-------+-----------+-------+------------+--------+-----------+
      |    vswitch     | cn-hangzhou |    vswitch     | 192.168.0.0/24 |      -1.0     |   -1.0   |   1    |   1   |    None   |  -1.0 |    Free    |        |   create  |
      |      vpc       | cn-hangzhou |      vpc       | 192.168.0.0/16 |      -1.0     |   -1.0   |   1    |   1   |    None   |  -1.0 |    Free    |        |   create  |
      | security_group | cn-hangzhou | security_group |     normal     |      -1.0     |   -1.0   |   1    |   1   |    None   |  -1.0 |    Free    |        |   create  |
      |      ecs       | cn-hangzhou |   webserver    |  ecs.c6.large  |     0.432     |   0.43   |   1    |   1   |   元/时   | 0.432 | PayAsYouGo |        |   create  |
      +----------------+-------------+----------------+----------------+---------------+----------+--------+-------+-----------+-------+------------+--------+-----------+
      Do you want to perform deploy action?
      Only 'yes' will be accepted to approve. 
      Enter your choice: yes
      2023-07-15 10:24:27 : CADT deploy app request call Successful! Request ID: 9E88565C-CDA3-5A58-A030-88EF1C02601E
      2023-07-15 10:25:38 : app status: Deployed_Success
      +----------------+----------------+---------------------------+-------------+-----------+---------+----------------------------+
      | recource type  | resource name  |        resource id        | charge type | lifecycle |  status |           remark           |
      +----------------+----------------+---------------------------+-------------+-----------+---------+----------------------------+
      |      vpc       |      vpc       | vpc-bp1i8iu55zgh0vodzmbj2 |     Free    |  OpenApi  | Running |                            |
      |    vswitch     |    vswitch     | vsw-bp1bwo7a39asrc5q0vy1d |     Free    |  OpenApi  | Running |                            |
      | security_group | security_group |  sg-bp1fgry9rui8t337fss0  |     Free    |  OpenApi  | Running |                            |
      |      ecs       |   webserver    |   i-bp10fogq1ga8uu4c8w92  |  PayAsYouGo |  OpenApi  | Running | Private_ip : 192.168.0.113 |
      +----------------+----------------+---------------------------+-------------+-----------+---------+----------------------------+