You can use Alibaba Cloud CLI commands to create and manage Elastic Compute Service (ECS) instances in the Cloud Shell console.

Log on to the Cloud Shell console

  1. Log on to the ECS console.
    Note If you have not created an Alibaba Cloud account, create one first. For more information, see Sign up with Alibaba Cloud.
  2. In the upper-right corner, click the Cloud Shell icon to go to the Cloud Shell console.
    Cloud Shell

Make preparations

Before you create an ECS instance, you must create a virtual private cloud (VPC) and a security group.

  1. Create a VPC.
    Create a VPC in the China (Hangzhou) region and specify 192.168.0.0/16 as the CIDR block of the VPC.
    API operationParameterExample
    CreateVpcRegionIdThe ID of the region. Example: cn-hangzhou.
    CidrBlockThe CIDR block of the VPC. Example: 192.168.0.0/16

    Run the following command to create a VPC:

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

    The following command output is returned:

    {
            "RequestId": "EC94C73B-8103-4B86-B353-E65C7C9E****",
            "ResourceGroupId": "rg-acfmzw2jz2z****",
            "RouteTableId": "vtb-bp1jxpr9ji5wcn4yv****",
            "VRouterId": "vrt-bp1dyxemup2q4ouga****",
            "VpcId": "vpc-bp1d9v4763ym2hlzt****"
    }
  2. Create a vSwitch.
    Create a vSwitch in the VPC and specify 192.168.0.0/24 as the CIDR block of the vSwitch.
    API operationParameterExample
    CreateVSwitchZoneIdThe ID of the zone. Example: cn-hangzhou-i.
    VpcIdThe ID of the VPC. Set the value to the VpcId value returned by the CreateVpc operation.

    Example: vpc-bp1d9v4763ym2hlzt****.

    CidrBlockThe CIDR block of the vSwitch. Example: 192.168.0.0/24.

    Run the following command to create a vSwitch:

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

    The following command output is returned:

    {
            "RequestId": "AF1787C4-0D81-44F0-A324-D5C54EA0****",
            "VSwitchId": "vsw-bp11hf5r945gewysp****"
    }
  3. Create a security group.
    API operationParameterExample
    CreateSecurityGroupRegionIdThe ID of the region. Example: cn-hangzhou.
    VpcIdThe ID of the VPC. Set the value to the VpcId value returned by the CreateVpc operation.

    Example: vpc-bp1d9v4763ym2hlzt****.

    Run the following command to create a security group:

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

    The following command output is returned:

    {
            "RequestId": "B1C25C34-9B84-49E3-9E50-FB7D7970****",
            "SecurityGroupId": "sg-bp18z2q1jg4gq95t****"
    }
  4. Add an inbound rule to the security group.
    API operationParameterExample
    AuthorizeSecurityGroupRegionIdThe region ID of the security group. Example: cn-hangzhou.
    SecurityGroupIdThe ID of the security group. Set the value to the SecurityGroupId value returned by the CreateSecurityGroup operation.

    Example: sg-bp18z2q1jg4gq95t****.

    IpProtocolThe protocol. Example: tcp.
    SourceCidrIpThe source CIDR block. Example: 0.0.0.0/0.
    PortRangeThe port range. Examples:
    • Linux instances: 22/22
    • Windows instances: 3389/3389
    Run the following command to add an inbound rule to the specified security group:
    aliyun ecs AuthorizeSecurityGroup  \
    --RegionId cn-hangzhou \
    --SecurityGroupId sg-bp18z2q1jg4gq95t**** \
    --IpProtocol tcp \
    --SourceCidrIp 0.0.0.0/0 \
    --PortRange 22/22
    The following command output is returned:
    {
            "RequestId": "FA8B1E61-C9C9-4D91-9628-64B8E2F4****"
    }

Create an ECS instance

Create a subscription ECS instance.

API operationParameterExample
RunInstancesRegionIdThe ID of the region. Example: cn-hangzhou.
ImageIdThe ID of the image. We recommend that you select the aliyun_2_1903_x64_20G_alibase_20200324.vhd Alibaba Cloud Linux image.
InstanceTypeThe instance type.
  • For individual applications, we recommend that you select the ecs.s6-c1m2.small instance type that has 1 vCPU and 2 GiB of memory.
  • For 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.
SecurityGroupIdThe ID of the security group. Set the value to the SecurityGroupId value returned by the CreateSecurityGroup operation.

Example: sg-bp18z2q1jg4gq95t****.

VSwitchIdThe ID of the vSwitch. Set the value to the VSwitchId value returned by the CreateVSwitch operation.

Example: vsw-bp11hf5r945gewysp****.

InstanceNameThe name of the instance.

Example: ecs_cli_demo.

InstanceChargeTypeThe billing method of the instance. To create a subscription instance, set the value to PrePaid.
Note Make sure that your account balance is sufficient.
PeriodUnitThe unit of the subscription duration. Example: Month.
PeriodThe subscription duration. Example: 1.
InternetMaxBandwidthOutThe maximum outbound public bandwidth. Unit: Mbit/s. Example: 1.
PasswordThe logon password of the instance: <Your password>.
Note You must specify a complex password to ensure instance security.

Run the following command to create a subscription instance:

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>

The following command output is returned:

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

Connect to the instance

This section describes how to connect to a Linux instance by using Cloud Shell. For information about how to connect to a Windows instance, see Connect to a Windows instance by using a username and password.

  1. Query the public IP address of the instance.
    API operationParameterExample
    DescribeInstancesRegionIdThe region ID of the instance. Example: cn-hangzhou.
    InstanceIdsThe ID of the instance. Set the value to the instance ID returned by the RunInstances operation.

    Example: '["i-bp1ducce5hs1jm98****"]'.

    Run the following command to query the public IP address of the instance:

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

    Find the following public IP address in the command output:

    Public IP address
  2. Use an SSH key pair to log on to the instance.
    Logon with an SSH key pair

Release an instance

You can manually release a subscription instance after it expires. If you do not renew an expired instance, the instance is automatically released.