Get ECS image ID with Alibaba Cloud CLI
table of contents
Introduction
I'm Takagi from the Technical Sales Department, and I still get lost for about 10 minutes at the beginning.
So this time, I'm going to use the "Alibaba Cloud CLI" to get the image ID of Alibaba Cloud's virtual server environment "ECS (Elastic Compute Service)"!
When you want to build ECS using Terraform, it is absolutely necessary to specify this "image ID", but it seems that it is faster to obtain it using this "Alibaba Cloud CLI", so this time I will do it as a memorandum. I'll keep it.
*I will also write an article about Terraform properly someday. .
What is Alibaba Cloud CLI?
First, I would like to briefly explain this "Alibaba Cloud CLI", but if you are a regular user of AWS, I think there will be no problem in interpreting it as the Alibaba Cloud version of the "AWS CLI". Masu.
This tool is used to manage resources with the CLI rather than the browser, but just in case we have included a link to the official documentation
By the way, the Japanese version is currently being prepared (as of the publication of this article on January 18, 2024) , so I read it using a translation tool.
General flow
The flow is as follows, and this time we will use WSL (Almalinux 8) installed on a Windows 11 PC.
- Install/unzip with curl command
- Operation confirmation
- Create RAM user and get API key
- Creating a profile
- Get image ID
- Retrieving information using --output
- summary
Install/unzip with curl command
Open WSL and start Almalinux, then install and unzip it using the commands below.
curl -sL https://github.com/aliyun/aliyun-cli/releases/download/v3.0.188/aliyun-cli-linux-3.0.188-amd64.tgz | sudo tar xzC /usr/local/bin
By the way, detailed information is also available on Github, so please check that as well.
■ Github
■ Official manual
Operation confirmation
Run the command below to check the version of Alibaba Cloud CLI you have installed.
aliyun version
This time, "3.0.188" should be displayed, and if you can confirm this, it's OK!
RAM user creation
However, as it is, you will still not be able to obtain the necessary information via CLI, so first create a user with Administrator privileges.
Also, please note the following three pieces of information that will be output at that time.
- AccessKey ID
- AccessKey Secret
- Default Region ID
*You can also obtain it in CSV when creating a user!
Creating a profile
Once you have noted down the above three points, open Almalinux8 in WSL and execute the command below.
aliyun configure --profile default
Then, as shown in Figure 1 below, you will be asked for the information mentioned earlier in an interactive format, so enter it one by one.
■ Figure 1
■ Input order
- AccessKey ID → Enter personal information
- AccessKey Secret → Enter personal information
- Default Region ID → ap-northeast-1
- Default Language → en
At the end, if "Configure Done!!!" appears, you are done.
Get image ID
However, as it is, I don't know what command to actually execute, so I'll check help to see what options are available!
■ Execution command
aliyun --help
■ Execution result
Since it's long, I've excerpted some of it, but you can display a list of options and resources that can be used with the aliyun command, so you can specify the "ECS" resource that is our requirement, and then get help for a specific service. I'll investigate.
■ Execution command
aliyun ecs --help
■ Execution result
A list of available APIs will be displayed. Use "DescribeImages", which seems most applicable, to get the results in JSON format.
■ Execution command
aliyun ecs DescribeImages
■ Execution result
I've excerpted some of it because it's long, but the result in JSON format will be output like this. You can see that the information you wanted to know this time can be obtained from the "ImageId" part!
Personally, I think it's good to combine grep and ``extract only the parts you want'', but this time I'll use ``--output'', which is nicely summarized in the official documentation I will do it!
As a side note, you can also check the help for the "aliyun ecs DescribeImages" command as shown below, so if you actually want to do something with ECS from the CLI, you also check the help for the "aliyun ecs DescribeImages" command. Please use it as a reference !
■ Execution command
aliyun ecs DescribeImages help
■ Execution result
Retrieving information using --output
Alibaba Cloud CLI provides the "-- output" option to make the output results of the above command more intuitive and easy to understand. By specifying this option and the following three "Fields", you can , you can extract only the parts that interest you.
■ Field
⓵ | cols |
The name of the row that exists in the field. For example, using the results of "aliyun ecs DescribeImages" This includes "ImageId", "OSNameEn", and "Status". |
⓶ | rows |
Use JAMESPATH the path where the field you want to filter exists For example, "Images" and "Image" are the results of "aliyun ecs DescribeImages". |
⓷ | num |
It will output the number of rows starting from 0 on the left side of the result. The default is no output. |
Based on the above, the command executed is as follows.
If you put "num=true" at the end of this, the number of rows will be output on the left side.
■ Execution command
aliyun ecs DescribeImages --PageSize 100 --output cols="ImageId,OSNameEn,Status" rows="Images.Image[]" aliyun ecs DescribeImages --PageSize 100 --output cols="ImageId,OSNameEn,Status" rows=" Images.Image[]" num=true
■ Execution result
The "rows" part needs to be specified using JAMESPATH
This is a relief!
summary
What did you think?
Personally, it took me a while to read the documentation for the JAMESPATH part, so I had a little trouble with it, but now I can build resources with Terraform with confidence!
Lastly, we have opened an office in Shenzhen, and we support our customers' systems from design and construction to operation and maintenance through the services of various cloud vendors, including Chinese clouds such as Alibaba Cloud and Tencent Cloud. It is possible to do so.
In addition to reading this article, we hope that you will also be interested in Beyou Technology (Shenzhen) Co., Ltd.
I will continue to write mainly articles related to AlibabaCloud, so please check it out!