All Products
Search
Document Center

Elastic Compute Service:Create and manage an ECS instance by using CLI

Last Updated:Jan 30, 2024

If you are accustomed to using Alibaba Cloud CLI to manage Alibaba Cloud resources, you can use Cloud Shell to create and manage Elastic Compute Service (ECS) instances by using Alibaba Cloud CLI. This topic describes how to create and manage ECS instances by using Alibaba Cloud CLI and provides examples.

CLI usage

  1. Access the Cloud Shell client.

    Access the Cloud Shell client from the Alibaba Cloud Management Console

    Log on to the ECS console and click the Cloud Shell icon in the upper-right corner to go to the Cloud Shell console.

    Access the Cloud Shell client from an on-premises terminal

    1. Install CLI.

    2. Grant CLI permissions to access your Alibaba Cloud account.

      In this example, AccessKey pair-based authentication is used. For information about other authentication methods, see Configure credential.

      Note
      • For information about regions, see Regions and zones.

      • An AccessKey pair consists of an AccessKay ID and an AccessKey secret. For information about how to obtain an AccessKey pair, see Create an AccessKey pair.

      aliyun configure
      aliyun Access Key ID [None]: xxxxx  # AccessKey ID
      aliyun Access Key Secret [None]: xxxxx  # AccessKey Secret
      Default Region Id [None]: cn-hangzhou # The region ID.
      Default Output Format [json]: json (Only supports JSON) # Only the JSON format is supported.
      Default Language [zh|en]: en # Select English as the default language.

      After you complete the configurations, if the configured credential is valid, the following information is displayed:

      Configure Done!!!
      ..............888888888888888888888 ........=8888888888888888888D=..............
      ...........88888888888888888888888 ..........D8888888888888888888888I...........
      .........,8888888888888ZI: ...........................=Z88D8888888888D..........
      .........+88888888 ..........................................88888888D..........
      .........+88888888 .......Welcome to use Alibaba Cloud.......O8888888D..........
      .........+88888888 ............. ************* ..............O8888888D..........
      .........+88888888 .... Command Line Interface(Reloaded) ....O8888888D..........
      .........+88888888...........................................88888888D..........
      ..........D888888888888DO+. ..........................?ND888888888888D..........
      ...........O8888888888888888888888...........D8888888888888888888888=...........
      ............ .:D8888888888888888888.........78888888888888888888O ..............
  2. In Alibaba Cloud CLI, you can call an API operation to complete a task based on the following format requirements.

    Important
    • For information about the formats that fields of different data types must follow, see Parameter formats.

    • Before you call an API operation, we recommend that you read the instructions of the API operation.

    aliyun <product> <ApiName> 
    --parameter1 value1 \
    --parameter2 value2 \
    ...

Sample CLI requests

Important
  • The following sample requests are only for reference. Modify the request commands based on your business requirements.

  • For information about ECS API operations, see API Documentations.

Create an ECS instance

The following example describes how to create an ECS instance from an Alibaba Cloud Linux image in the China (Hangzhou) region by using Alibaba Cloud CLI.

  1. Make preparations.

    Before you create an ECS instance, make sure that you have created a virtual private cloud (VPC), a vSwitch, and a security group, and obtained their IDs.

    Note

    If the preceding resources are available and meet your requirements, skip this step.

    1. Call the CreateVpc operation to create a VPC.

      Example: Create a VPC

      In this example, a VPC is created in the China (Hangzhou) region and associated with the CIDR block 192.168.0.0/16.

      • Command

        aliyun vpc CreateVpc \
        --RegionId cn-hangzhou \
        --CidrBlock 192.168.0.0/16 
      • Command output

        {
                "RequestId": "EC94C73B-8103-4B86-B353-E65C7C9E****",
                "ResourceGroupId": "rg-acfmzw2jz2z****",
                "RouteTableId": "vtb-bp1jxpr9ji5wcn4yv****",
                "VRouterId": "vrt-bp1dyxemup2q4ouga****",
                "VpcId": "vpc-bp1d9v4763ym2hlzt****"
        }
    2. Call the CreateVSwitch operation to create a vSwitch in the VPC.

      Example: Create a vSwitch

      In this example, the CIDR block of the vSwitch is 192.168.0.0/24 and the VPC ID is vpc-bp1d9v4763ym2hlzt****.

      • Command

        aliyun vpc CreateVSwitch \
        --CidrBlock 192.168.0.0/24 \
        --VpcId vpc-bp1d9v4763ym2hlzt**** \
        --ZoneId=cn-hangzhou-i
      • Command output

        {
                "RequestId": "AF1787C4-0D81-44F0-A324-D5C54EA0****",
                "VSwitchId": "vsw-bp11hf5r945gewysp****"
        }
    3. You can call the CreateSecurityGroup operation to create a security group.

      Example: Create a security group

      • Command

        aliyun ecs CreateSecurityGroup \
        --RegionId cn-hangzhou \
        --VpcId vpc-bp1d9v4763ym2hlzt****
      • Command output

        {
                "RequestId": "B1C25C34-9B84-49E3-9E50-FB7D7970****",
                "SecurityGroupId": "sg-bp18z2q1jg4gq95t****"
        }
    4. You can call the AuthorizeSecurityGroup operation to add security group rules.

      Example: Add a security group rule

      In this example, an inbound rule that allows TCP traffic on port 22 is added to the security group whose ID is sg-bp18z2q1jg4gq95t****.

      • Command

        aliyun ecs AuthorizeSecurityGroup \
        --RegionId cn-hangzhou \
        --SecurityGroupId sg-bp18z2q1jg4gq95t**** \
        --IpProtocol tcp \
        --SourceCidrIp 0.0.0.0/0 \
        --PortRange 22/22
      • Command output

        {
         "RequestId": "FA8B1E61-C9C9-4D91-9628-64B8E2F4****"
        }
  2. Call the RunInstances operation to create a subscription ECS instance.

    • Example scenarios

      Parameter

      Description and example

      RegionId

      The ID of the region in which to create the instance. Example: cn-hangzhou.

      ImageId

      The ID of the image. We recommend that you select the aliyun_2_1903_x64_20G_alibase_20200324.vhd Alibaba Cloud Linux image.

      InstanceType

      The instance type. Examples:

      • For the applications of individuals, we recommend that you select the ecs.s6-c1m2.small instance type that has 1 vCPU and 2 GiB of memory.

      • For the applications of small and medium-sized enterprises, we recommend that you select the ecs.c5.large instance type that has 2 vCPUs and 4 GiB of memory.

      SecurityGroupId

      The security group ID. Obtain the value from the response of the CreateSecurityGroup operation.

      Example: sg-bp18z2q1jg4gq95t****.

      VSwitchId

      The vSwitch ID. Obtain the value from the response of the CreateVSwitch operation.

      Example: vsw-bp11hf5r945gewysp****.

      InstanceName

      The name of the instance.

      Example: ecs_cli_demo.

      InstanceChargeType

      The billing method of the instance. To create a subscription instance, set the value to PrePaid.

      Note

      Make sure that your account balance is sufficient.

      PeriodUnit

      The unit of the subscription duration. Example: Month.

      Period

      The subscription duration. Example: 1.

      InternetMaxBandwidthOut

      The maximum outbound public bandwidth. Example: 1.

      Password

      The logon password of the instance. Example: <yourPassword>.

      Note

      To ensure instance security, you must specify a complex password.

    • Command

      aliyun ecs RunInstances \
      --RegionId cn-hangzhou \
      --ImageId aliyun_2_1903_x64_20G_alibase_20200324.vhd \
      --InstanceType ecs.s6-c1m2.small \
      --SecurityGroupId sg-bp18z2q1jg4gq95t**** \
      --VSwitchId vsw-bp11hf5r945gewys**** \
      --InstanceName ecs_cli_demo \
      --InstanceChargeType PrePaid \
      --PeriodUnit Month \
      --Period 1 \
      --InternetMaxBandwidthOut 1 \
      --Password <yourPassword>
    • Command output

      {
       "InstanceIdSets": {
             "InstanceIdSet": [
                   "i-bp1ducce5hs1jm98****"
             ]
       },
       "OrderId": 22660647277****,
       "RequestId": "7F0166F9-9466-4AE1-8799-E68D6514****",
       "TradePrice": ****
      }

Connect to an instance

  1. You can call an operation to obtain the public IP address of an instance.

    In this example, the DescribeInstances operation is called to obtain the public IP address of the instance whose ID is i-bp1ducce5hs1jm98****.

    • Command

      aliyun ecs DescribeInstances \
      --RegionId cn-hangzhou \
      --InstanceIds '["i-bp1ducce5hs1jm98****"]'
    • Command output

      The PublicIpAddresses parameter indicates the public IP address of the instance.

      公网IP

  2. Connect to a Windows instance.

    ssh <Username>@<Public IP address>

    ssh登录

Start an instance

Call the StartInstance operation to start an ECS instance.

  • Example: Start an instance whose ID is i-bp1aq39j2yul5y01**** directly after a dry run, and do not perform troubleshooting during instance startup.

  • Command

  • aliyun ecs StartInstance \
    --InstanceId i-bp1aq39j2yul5y01**** \
    --InitLocalDisk false \
    --DryRun false
  • Command output

  • {
     "RequestId": "2DD09CBD-1F4D-4923-94C7-F3BD67137BBE"
    }

Query the details of ECS instances

You can call the DescribeInstances operation to query the details of one or more ECS instances.

Example 1: Query an ECS instance by instance ID

In this example, information about an instance whose ID is i-bp14a7xie8erwsvo**** is queried.

  • Command

    aliyun ecs DescribeInstances \
    --RegionId cn-hangzhou \
    --InstanceIds '["i-bp14a7xie8erwsvo****"]' \
    --output cols=InstanceId,InstanceName,Description,ImageId,Status rows=Instances.Instance[]
  • Command output

    InstanceId | InstanceName | Description | ImageId | Status
    ---------- | ------------ | ----------- | ------- | ------
    i-bp14a7xie8erwsvo**** | wpdemo | wp | m-bp12qhgxbmp5eh02**** | Running

Example 2: Query ECS instances by tag

In this example, information about ECS instances to which owner:zhangsan tags are added is queried.

  • Command

    aliyun ecs DescribeInstances \
    --RegionId cn-hangzhou \
    --Tag.1.Key owner \
    --Tag.1.Value zhangsan \
    --output cols=InstanceId,InstanceName,Description,ImageId,Status rows=Instances.Instance[]
  • Command output

    InstanceId | InstanceName | Description | ImageId | Status
    ---------- | ------------ | ----------- | ------- | ------
    i-bp1aq39j2yul5y01**** | namedemo01 | demo01 | m-bp12qhgxbmp5eh02**** | Stopped
    i-bp1dpvr406hq1jnw**** | namedemo02 | demo01 | centos_7_06_64_20G_alibase_20190711.vhd | Running

Example 3: Query an ECS instance by image ID

In this example, information about an ECS instance whose image has the m-bp12qhgxbmp5eh02**** tag is queried.

  • Command

    aliyun ecs DescribeInstances \
    --RegionId cn-hangzhou \
    --ImageId m-bp12qhgxbmp5eh02**** \
    --output cols=InstanceId,InstanceName,Description,ImageId,Status rows=Instances.Instance[]
  • Command output

    InstanceId | InstanceName | Description | ImageId | Status
    ---------- | ------------ | ----------- | ------- | ------
    i-bp14a7xie8erwsvo**** | demo01 | desc01 | m-bp12qhgxbmp5eh02**** | Running
    i-bp1aq39j2yul5y01**** | demo02 | desc02 | m-bp12qhgxbmp5eh02**** | Stopped

Example 4: Query ECS instances in a specified VPC

In this example, information about ECS instances that reside in a VPC whose ID is vpc-bp1vwnn14rqpyiczj**** and are connected to a vSwitch whose ID is vsw-bp1ddbrxdlrcbim46**** is queried.

  • Command

    aliyun ecs DescribeInstances \
    --RegionId cn-hangzhou \
    --VpcId vpc-bp1vwnn14rqpyiczj**** \
    --VSwitchId vsw-bp1ddbrxdlrcbim46**** \
    --output cols=InstanceId,InstanceName,ImageId,Status rows=Instances.Instance[]
  • Command output

    InstanceId | InstanceName | ImageId | Status
    ---------- | ------------ | ------- | ------
    i-bp14a7xie8erwsvo**** | namedemo01 | m-bp12qhgxbmp5eh02**** | Running
    i-bp1c271nqm264lwj**** | namedemo02 | P2VSImageLnx125 | Running
    i-bp18a6ub0vt1tvn1**** | namedemo03 | centos_7_02_64_20G_alibase_20170818.vhd | Running
    i-bp1aq39j2yul5y01**** | namedemo04 | m-bp12qhgxbmp5eh02**** | Stopped

Example 5: Query ECS instances by page

You can call the DescribeInstances operation to query ECS instances in the China (Hangzhou) region by page. Each page displays five entries.

  • Command

    aliyun ecs DescribeInstances \
    --RegionId cn-hangzhou \
    --PageNumber 2 \
    --PageSize 5 \
    --output cols=InstanceId,InstanceName,ImageId,Status rows=Instances.Instance[]
  • Command output

    InstanceId | InstanceName | ImageId | Status
    ---------- | ------------ | ------- | ------
    i-bp1akazu9o0rm7q0**** | demoname01 | centos_8_0_x64_20G_alibase_20191225.vhd | Running
    i-bp134jm1g6kqyiqu**** | demoname02 | m-bp1bc3g3b032o0ja**** | Running
    i-bp17qwke5y0v7hk2**** | demoname03 | centos_7_02_64_20G_alibase_20170818.vhd | Running
    i-bp18a6ub0vt1tvn1**** | demoname04 | centos_7_02_64_20G_alibase_20170818.vhd | Running
    i-bp1aq39j2yul5y01**** | demoname05 | m-bp12qhgxbmp5eh02**** | Stopped

Create a snapshot for a disk

Call the CreateSnapshot operation to create a snapshot based on a specified disk:

  • Example scenario: Create a snapshot for an ultra disk whose ID is d-bp14bjlwo3t3owin****. The snapshot name is demoname, the description is demo, and the retention period is 3 days.

  • Command

    aliyun ecs CreateSnapshot \
    --DiskId d-bp14bjlwo3t3owin**** \
    --SnapshotName demoname \
    --Description demo \
    --RetentionDays 3
  • Command output

    {
     "RequestId": "DFB0B01F-420D-4932-911E-7328920C2012",
     "SnapshotId": "s-bp1eyr9nxxoo9icj****"
    }

Create a custom image from an instance

Call the CreateImage operation to create a custom image from an ECS instance.

  • Example scenario: Create a custom image from an instance whose ID is i-bp1aq39j2yul5y01**** and that runs Alibaba Cloud Linux (Platform set to Aliyun) and is deployed in the China (Hangzhou) (cn-hangzhou) region.

  • Command

    aliyun ecs CreateImage \
    --RegionId cn-hangzhou \
    --InstanceId i-bp1aq39j2yul5y01**** \
    --ImageName demoimage \
    --Description demoimage \
    --Platform Aliyun
  • Command output

    {
     "ImageId": "m-bp1503ydxxrppctb****",
     "RequestId": "011AE447-20CE-4043-81AC-7AF2BBC45C38"
    }

Stop an instance

Call the StopInstance operation with ForceStop set to false and StoppedMode set to KeepCharing to stop an ECS instance in the Running (Running) state after a dry run. The instance is stopped in standard mode, and billing for the instance continues.

  • Example scenario: Stop an instance whose ID is i-bp1aq39j2yul5y01**** in the China (Hangzhou) (cn-hangzhou) region.

  • Command

    aliyun ecs StopInstance \
    --InstanceId i-bp1aq39j2yul5y01**** \
    --ForceStop false \
    --StoppedMode KeepCharging \
    --DryRun false
  • Command output

    {
        "RequestId": "121B5745-4983-57B1-BC97-C3A3536E3C3F"
    }