Introduction
This time, I'd like to try obtaining the image ID of the Alibaba Cloud virtual server environment "ECS (Elastic Compute Service)" using the "Alibaba Cloud CLI".
When you want to build ECS using Terraform, you need to specify this "image ID." However, it seems that using the "Alibaba Cloud CLI" to obtain it is faster, so I am writing this as a reminder.
What is “Alibaba Cloud CLI”?
First of all, let me briefly explain the Alibaba Cloud CLI. If you are a regular AWS user, I think you will have no problem interpreting it as the Alibaba Cloud version of the AWS CLI.
It is a tool used to manage resources via the CLI rather than through a browser, and I have included a link to the official documentation just in case you need more information.
General workflow
The workflow for this time is as follows, and I will use WSL (Almalinux 8) installed on a PC running Windows 11.
Installation / Decompression with curl command
Operation check
Creating RAM user and obtaining API key
Creating a profile
Obtaining Image ID
Obtaining information using --output
Summary
Installation / Decompression with curl command
After opening WSL and starting Almalinux, I will use the following commands to install and unzip the software.
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 there as well.
■ Github
■ Official Manual
Operation check
Run the following command to check the version of the Alibaba Cloud CLI you have installed.
1| aliyun version |
In this instance, "3.0.188" should be displayed, and it is OK if this is confirmed!
Creating RAM User
However, as it is, the necessary information cannot be obtained via the CLI yet, so I need to create a user with Administrator privileges for now.
In addition, please note the following three pieces of information that will be output at that time.
AccessKey ID
AccessKey Secret
Default Region ID
※ It is also possible to obtain the data in CSV format when creating a user!
Creating a profile
After noting the above three points, please open Almalinux8 in WSL and execute the following command:
1| aliyun configure --profile default |
Then, as shown in Figure 1 below, you will be asked for the information you have just provided in an interactive format. Please enter the information as prompted.
■ Figure 1
■ Input order
AccessKey ID → Enter your personal information
AccessKey Secret → Enter your personal information
Default Region ID → ap-northeast-1
Default Language → en
Finally, once you see 'Configure Done!!!' it's completed.
Obtaining Image ID
However, you may not know the actual command to execute at this point, so check help to see what options are available!
■ Command
1| aliyun --help p
■ Result
I've excerpted some parts as the list is long, but you can check the available options and resources with the aliyun command. The resource of "ECS" is specified for the requirement this time and for specific services, you can use help for further investigation.
■ Command
1| aliyun ecs --help p
■ Result
The list of available APIs is displayed here, and use "DescribeImages" that seems to be the most applicable among them to obtain the results in JSON format.
■ Command
1| aliyun ecs DescribeImages h
■ Result
It's been excerpted for brevity, but you'll see output in JSON format like this. The information wanted this time is obtained in the 'ImageId' section!
Personally, I feel it's also good to combine it with grep to 'extract only the desired parts', but this time, I will use --output from the official documentation to retrieve the necessary parts in a nice way!
As an aside, you can also check the help for the aliyun ecs DescribeImages command as follows. So, if you want to perform something on ECS from the CLI, consider referring to other parameters like the following!
If you have any further questions or need assistance with anything else, feel free to ask!
■ Command
1| aliyun ecs DescribeImages help 0
■ Result
Retrieving information using --output
The Alibaba Cloud CLI provides a "-- output" option to make the output of the above commands more intuitive and easier to understand, and by specifying this option and the following three "fields," you can extract only the parts you are interested in.
■ Field
⓵ | cols | Regarding the name of the rows present in the field, taking "aliyun ecs DescribeImages" as an example, "ImageId", "OSNameEn", and "Status" would be included in the result. |
⓶ | rows | Use JAMESPATH to specify the path where the fields you want to filter. For example, "Images" or "Image" will be included in the result of "aliyun ecs DescribeImages". |
⓷ | num | It outputs the number of rows starting from 0 on the left side of the result. There is no output by default. |
Based on the above, the executed command is as follows:
By adding "num=true" at the end, line numbers will be outputted on the left side.
■ 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
■ Result
The "rows" part must be specified using JAMESPATH, but the results are much easier to see! This brings some relief!
Summary
The part about JAMESPATH took me a bit longer to read through personally, so I struggled a bit, but now I can confidently build resources with Terraform!
Finally, our company has established an office in Shenzhen, and through various cloud vendors including Chinese cloud providers like AlibabaCloud and Tencent Cloud, we are capable of supporting our customers' systems from design and construction to operation and maintenance.
We hope that along with this article, you will also be interested in our office in China!
We will continue to focus on articles related to AlibabaCloud. You are more than welcome to check them out!
This blog post is translated from a blog post written by Takagi on our Japanese website Beyond Co..
Comments