Get started with Alibaba Cloud CLI

Updated at:
Copy as MD

After installing Alibaba Cloud CLI, configure your identity credentials to start making API calls. This topic uses ECS as an example to show you how to install the CLI and run your first command.

Solution overview

To call cloud product APIs using Alibaba Cloud CLI, follow these three steps:

  1. Step 1: Install the Alibaba Cloud CLI

  2. Step 2: Configure identity credentials

  3. Step 3: Run a command

Prerequisites

Procedure

Step 1: Install the Alibaba Cloud CLI

On Linux, run the following one-click installation command:

/bin/bash -c "$(curl -fsSL https://aliyuncli.alicdn.com/install.sh)"

For installation instructions on other operating systems, see Install or update CLI.

After installation, run the following command to check the version:

aliyun version

Keep the CLI up to date.

Note

If you prefer not to install the CLI locally, use Cloud Shell. It comes with the Alibaba Cloud CLI preinstalled and automatically authenticates your browser session. You can start from Step 3.

Step 2: Configure identity credentials

The Alibaba Cloud CLI supports multiple identity authentication methods. Use OAuth authentication when possible. If you use an AccessKey, do not hard code it in your source code.

OAuth authentication

OAuth completes authentication by logging in through your browser. You do not need to manually create or manage keys.

Note

On servers without a graphical interface, add the --no-browser parameter to the configuration command, open the sign-in URL printed by the CLI in a browser on any device (computer or phone) to authorize, then paste the full redirected URL from the browser's address bar back into the terminal.

Important

RAM administrators must complete the following tasks in advance: create a CLI OAuth application and assign identities to users who need CLI access. For details, see Step 1 and Step 2 in Configure OAuth authentication for Alibaba Cloud CLI. Users of an Alibaba Cloud account (the account used during registration) do not need to perform these tasks.

  1. Run the following command to start OAuth configuration:

    aliyun configure --mode OAuth --profile default

    The --profile default option saves these credentials under the profile name default. The CLI supports multiple named profiles. Use --profile mydev to switch between different credentials.

  2. The CLI prompts you to select a sign-in site. Press Enter to accept the default setting.

  3. The CLI automatically opens your browser to complete authorization. Your terminal displays output similar to the following:

    Please open the following URL in your browser to authorize:
    https://signin.aliyun.com/oauth2/v1/auth?response_type=code&client_id=40381819545577*****&redirect_uri=http%3A%2F%2F127.0.0.1%3A12345%2Fcli%2Fcallback&state=CdI6CcOsxQUiCuFl&code_challenge=vDr1QWI5TI9q6UMMs2djJzxCmcAySjrYK6UD4Gu2res&code_challenge_method=S256
    If the browser does not open automatically, use the following URL to complete the login process:
    
    SignIn url: https://signin.aliyun.com/oauth2/v1/auth?response_type=code&client_id=40381819545577*****&redirect_uri=http%3A%2F%2F127.0.0.1%3A12345%2Fcli%2Fcallback&state=CdI6CcOsxQUiCuFl&code_challenge=vDr1QWI5TI9q6UMMs2djJzxCmcAySjrYK6UD4Gu2res&code_challenge_method=S256
    
    Now you can login to your account with OAuth configuration in the browser.

    If the browser does not open automatically, copy the Sign In URL shown in the terminal and open it manually in your browser to log in.

  4. After successful authorization, return to the terminal and enter your default region ID and terminal language. Example terminal output:

    Default Region Id []: cn-hangzhou
    Default Output Format [json]: json (Only support json)
    Default Language [zh|en] en: zh
    Saving profile[oauth-1] ...Done.
    
    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 ..............
  5. Verify that your credentials are valid:

    aliyun sts GetCallerIdentity

    If the response includes AccountId and Arn, your credentials are valid.

    Note

    If you receive a Forbidden.NoPermission error, your current user lacks sufficient permissions. Contact your organization’s RAM administrator to grant the required permissions to your RAM user. If you are using an Alibaba Cloud account, check that your account status is normal.

AccessKey authentication

Warning

An AccessKey is a long-term credential. Do not hard code it in your source code or configuration files. Pass it through environment variables instead. Use AccessKeys only in personal development and test environments. For production environments, use OAuth. For more information, see Secure credential usage practices.

  1. If you already have an AccessKey, use your existing AccessKey ID and AccessKey secret and skip this step. If you do not have one, go to the RAM console to create a RAM user and generate an AccessKey. For details, see Create a RAM user.

  2. Run the following command to start AccessKey configuration:

    aliyun configure --mode AK --profile default

    The --profile default option saves these credentials under the profile name default. The CLI supports multiple named profiles. Use --profile mydev to switch between different credentials.

  3. Enter your AccessKey ID, AccessKey secret, default region ID (such as cn-hangzhou), and terminal language when prompted.

  4. Verify that your credentials are valid:

    aliyun sts GetCallerIdentity

    If the response includes AccountId and Arn, your credentials are valid. If you receive an error, check that your AccessKey ID and secret are correct.

    Note

    If you receive a Forbidden.NoPermission error, your current RAM user lacks sufficient permissions. Contact your organization’s RAM administrator to grant the required permissions. If you are using an Alibaba Cloud account, check that your account status is normal.

The Alibaba Cloud CLI also supports other credential types, such as Security Token Service tokens for temporary authorization scenarios. For details, see Configure and manage identity credentials.

Step 3: Run a command

Run the following command to list available regions:

aliyun ecs DescribeRegions --AcceptLanguage zh-CN

The --AcceptLanguage zh-CN option specifies that the response language is Chinese.

The following excerpt lists China (Hangzhou), China (Shanghai), and China (Beijing). The LocalName values are in Chinese because the command specifies --AcceptLanguage zh-CN:

{
  "Regions": {
    "Region": [
      {
        "RegionId": "cn-hangzhou",
        "RegionEndpoint": "ecs.cn-hangzhou.aliyuncs.com",
        "LocalName": "华东1(杭州)"
      },
      {
        "RegionId": "cn-shanghai",
        "RegionEndpoint": "ecs.cn-shanghai.aliyuncs.com",
        "LocalName": "华东2(上海)"
      },
      {
        "RegionId": "cn-beijing",
        "RegionEndpoint": "ecs.cn-beijing.aliyuncs.com",
        "LocalName": "华北2(北京)"
      }
    ]
  },
  "RequestId": "5BEAEC76-E9B4-47B2-8D24-A3C31D5D2EEF"
}

The RequestId is a unique identifier for each API call and is used for troubleshooting.

FAQ

Why do I get an “Unauthorized call” error during OAuth configuration?

Your current account does not have the RAM management permissions required to create an OAuth application. Switch to a RAM administrator account or ask your administrator to create the OAuth application and assign identities. For specific permission requirements, see OAuth credentials.

How do I know if a cloud product supports the CLI?

Run aliyun --help-all to view the complete list of cloud products. Run aliyun ecs --help-all to view the complete list of operations for ECS. You can also visit OpenAPI Portal CLI Homepage and search by product.

Note

Supported AI Agent environments can automatically enable AI mode, which may truncate help. Use --help-all to view complete lists.

Can I use OAuth authentication on a server without a graphical interface?

Yes. Add the --no-browser parameter to the configuration command (for example, aliyun configure --mode OAuth --no-browser --profile <ProfileName>), open the sign-in URL in a browser on any device and authorize, then paste the full redirected URL from the browser's address bar back into the terminal. The callback page may fail to load, which is expected.

References