Skip to content

Instantly share code, notes, and snippets.

@dobaduc
Last active September 4, 2018 12:36
Show Gist options
  • Save dobaduc/f21e98d802616ba2c97d to your computer and use it in GitHub Desktop.
Save dobaduc/f21e98d802616ba2c97d to your computer and use it in GitHub Desktop.
Getting started with Aliyun ECS API

Installation

Check out the guideline here: https://github.com/aliyun/aliyun-cli ( using Google translator)

Install python pip if necessary

curl https://bootstrap.pypa.io/get-pip.py | sudo python

Install aliyuncli

sudo pip install aliyuncli

Install some sdk packages:

sudo pip install "aliyun-python-sdk-ecs"
sudo pip install "aliyun-python-sdk-rds"
sudo pip install "aliyun-python-sdk-oss"
sudo pip install "aliyun-python-sdk-slb"

Confirm all SDKs available via aliyuncli

aliyuncli

The ouput will look like below

ecs                                       | oss
rds                                       | slb

Configure access Key ID and access key secret (Find them in your console menu):

aliyuncli configure
# Aliyun Access Key ID [None]: <Your access key ID>
# Aliyun Access Key Secret [None]: <Your access key secret>
# Default Region Id [None]: cn-hangzhou
# Default output format [None]: json

NOTE: Without specifying the default region ID, you won't be able to run any commands because the CLI doesn't know what API endpoint it should connect to.

New let's check all available ECS commands and see some help

aliyuncli ecs

Find out how to use CreateInstance command by typing

aliyuncli ecs CreateInstance help

The help content is not so helpful as it doesn't tell which are the required parameters... Luckily we can check the API documentation to see that following parameters are required to create a new instance:

  • RegionId
  • ImageId
  • InstanceType
  • SecurityGroupId

Since we have no idea what are availabe for use, check available zones, images, instance types and security groups like below (NOTE: All LocalNames or Descriptions are not understandable because they are in Chinese)

aliyuncli ecs DescribeRegions
aliyuncli ecs DescribeImages
aliyuncli ecs DescribeInstanceTypes

By having a quick look at what kind of instances are available in each regions. There are many differences between the regions' image lists:

$ aliyuncli ecs DescribeImages --RegionId cn-hangzhou | grep -e "ImageId"
    "ImageId": "ubuntu1404_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "ubuntu1404_32_40G_aliaegis_20160222.vhd", 
    "ImageId": "ubuntu1204_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "ubuntu1204_32_40G_aliaegis_20160222.vhd", 
    "ImageId": "suse11sp3_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "suse11sp2_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "suse11sp1_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "gentoo13_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "debian750_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "debian750_32_40G_aliaegis_20160222.vhd", 
$ aliyuncli ecs DescribeImages --RegionId ap-southeast-1 | grep -e "ImageId"
    "ImageId": "centos7u0_64_40G_aliaegis_20160120.vhd", 
    "ImageId": "win2012_64_dataCtr_R2_en_40G_alibase_20151212.vhd", 
    "ImageId": "win2012_64_dataCtr_R2_cn_40G_alibase_20151212.vhd", 
    "ImageId": "win2008_64_ent_r2_en_40G_alibase_20151212.vhd", 
    "ImageId": "win2008_64_ent_r2_cn_40G_alibase_20151212.vhd", 
    "ImageId": "win2008_32_stand_sp2_cn_40G_alibase_20151212.vhd", 
    "ImageId": "opensuse1301_64_40G_aliaegis_20160120.vhd", 
    "ImageId": "opensuse1301_32_40G_aliaegis_20160120.vhd", 
    "ImageId": "freebsd1001_64_40G_aliaegis_20160120.vhd", 
    "ImageId": "centos5u8_64_40G_aliaegis_20160120.vhd", 
$ aliyuncli ecs DescribeImages --RegionId cn-shenzhen | grep -e "ImageId"
    "ImageId": "ubuntu1404_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "ubuntu1404_32_40G_aliaegis_20160222.vhd", 
    "ImageId": "ubuntu1204_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "ubuntu1204_32_40G_aliaegis_20160222.vhd", 
    "ImageId": "suse11sp3_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "suse11sp2_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "suse11sp1_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "gentoo13_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "debian750_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "debian750_32_40G_aliaegis_20160222.vhd", 
$ aliyuncli ecs DescribeImages --RegionId cn-beijing | grep -e "ImageId"
    "ImageId": "ubuntu1404_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "ubuntu1404_32_40G_aliaegis_20160222.vhd", 
    "ImageId": "ubuntu1204_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "ubuntu1204_32_40G_aliaegis_20160222.vhd", 
    "ImageId": "suse11sp3_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "suse11sp2_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "suse11sp1_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "gentoo13_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "debian750_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "debian750_32_40G_aliaegis_20160222.vhd", 
$ aliyuncli ecs DescribeImages --RegionId cn-shanghai | grep -e "ImageId"
    "ImageId": "ubuntu1404_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "ubuntu1404_32_40G_aliaegis_20160222.vhd", 
    "ImageId": "ubuntu1204_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "ubuntu1204_32_40G_aliaegis_20160222.vhd", 
    "ImageId": "suse11sp3_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "suse11sp2_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "suse11sp1_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "gentoo13_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "debian750_64_40G_aliaegis_20160222.vhd", 
    "ImageId": "debian750_32_40G_aliaegis_20160222.vhd", 
$ aliyuncli ecs DescribeImages --RegionId cn-hongkong | grep -e "ImageId"
    "ImageId": "centos7u0_64_40G_aliaegis_20160120.vhd", 
    "ImageId": "win2012_64_dataCtr_R2_en_40G_alibase_20151212.vhd", 
    "ImageId": "win2012_64_dataCtr_R2_cn_40G_alibase_20151212.vhd", 
    "ImageId": "win2008_64_ent_r2_en_40G_alibase_20151212.vhd", 
    "ImageId": "win2008_64_ent_r2_cn_40G_alibase_20151212.vhd", 
    "ImageId": "win2008_32_stand_sp2_cn_40G_alibase_20151212.vhd", 
    "ImageId": "opensuse1301_64_40G_aliaegis_20160120.vhd", 
    "ImageId": "opensuse1301_32_40G_aliaegis_20160120.vhd", 
    "ImageId": "freebsd1001_64_40G_aliaegis_20160120.vhd", 
    "ImageId": "centos5u8_64_40G_aliaegis_20160120.vhd", 
$ aliyuncli ecs DescribeImages --RegionId us-west-1 | grep -e "ImageId"
    "ImageId": "centos7u0_64_40G_aliaegis_20160120.vhd", 
    "ImageId": "win2012_64_dataCtr_R2_en_40G_alibase_20151212.vhd", 
    "ImageId": "win2012_64_dataCtr_R2_cn_40G_alibase_20151212.vhd", 
    "ImageId": "win2008_64_ent_r2_en_40G_alibase_20151212.vhd", 
    "ImageId": "win2008_64_ent_r2_cn_40G_alibase_20151212.vhd", 
    "ImageId": "win2008_32_stand_sp2_cn_40G_alibase_20151212.vhd", 
    "ImageId": "opensuse1301_64_40G_aliaegis_20160120.vhd", 
    "ImageId": "opensuse1301_32_40G_aliaegis_20160120.vhd", 
    "ImageId": "freebsd1001_64_40G_aliaegis_20160120.vhd", 
    "ImageId": "centos5u8_64_40G_aliaegis_20160120.vhd",

I am curious whether the results above means that I can't create an ECS instance running CentOS in Shanghai? The answer is NO. From the web console of int.aliyun.com, I was able to create it! So it seems like aliyuncli doesn't give us the correct information.

Create an empty security group in order to create the new instance. We can get back to configure it later:

aliyuncli ecs CreateSecurityGroup

# The response will look like below
{
    "SecurityGroupId": "sg-23ewnvyt4",
    "RequestId": "A113C5FA-4C46-46C9-A02B-70CCCFD6AD9F"
}

Now we are ready to create a new ECS instance!!

aliyuncli ecs CreateInstance \
  --ImageId centos7u0_64_20G_aliaegis_20150130.vhd \
  --InstanceType ecs.t1.small \
  --InstanceName generated-by-cli \
  --Description "This is pretty cool" \
  --SecurityGroupId sg-23ewnvyt4

# The output will be the created instance's information:
{
    "InstanceId": "i-23yi8tnhj",
    "RequestId": "36D8C4EC-2E38-4468-B4F4-AE658B90E1C3"
}

Now confirm the instance's information:

aliyuncli ecs DescribeInstanceAttribute --InstanceId i-23yi8tnhj

# OR: aliyuncli ecs DescribeInstanceStatus

The instance status is now "Stopped". Starting it is quite straight forward

aliyuncli ecs StartInstance --InstanceId i-23yi8tnhj

Curious about what can be changed later? Try this

aliyuncli ecs | grep Modify

Modify root password:

aliyuncli ecs ModifyInstanceAttribute --Password "<Your root password>" --InstanceId "i-23yi8tnhj"
aliyuncli ecs StopInstance --InstanceId "i-23yi8tnhj" && aliyuncli ecs StartInstance --InstanceId "i-23yi8tnhj"

Try modify some instance attributes:

# Give the instance a public IP
aliyuncli ecs AllocatePublicIpAddress --InstanceId i-23yi8tnhj

# The IP address will be returned in the response:
{
    "IpAddress": "xx.yy.zz.tt", 
    "RequestId": "CC9A1897-64AA-46BA-8B99-1A59EC255562"
}

# TODO: Add missing commands here

Confirm disk spec & modify

aliyuncli ecs DescribeDisks --InstanceId i-23yi8tnhj

TO BE CONTINUED...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment