This quick start guide shows you how to create, view, and use an AccessKey pair in Resource Access Management (RAM) to access Alibaba Cloud resources with the Alibaba Cloud CLI.
As a security best practice, avoid using long-term credentials such as an AccessKey pair for programmatic access. For more information, see Best practices for programmatic access.
What is an AccessKey pair?
An AccessKey pair consists of an AccessKey ID and an AccessKey secret. AccessKey pairs are primarily used to authenticate an application or service when it makes API calls to Alibaba Cloud.
An AccessKey pair is a long-term credential associated with a specific RAM user. This long-term credential poses a high security risk if compromised.
Procedure
-
Create an AccessKey pair: Generate an AccessKey pair for a specified RAM user.
-
Use the AccessKey pair to make API calls: Configure the Alibaba Cloud CLI to use the AccessKey pair for direct or indirect API calls.
-
Clean up resources: Delete the local credential configuration, and then disable and delete the AccessKey pair.
Prerequisites
-
You have created a RAM user. If not, see Quick start: Create and grant permissions to a RAM user. Do not use your Alibaba Cloud account to create an AccessKey pair.
-
You need the required RAM permissions to create and view an AccessKey pair. We recommend granting the
AliyunRAMFullAccesssystem policy to the user performing these operations. Remove this policy after completing the tutorial. -
You have installed the Alibaba Cloud CLI. For more information, see Get started with Alibaba Cloud CLI.
Create an AccessKey pair
-
Log on to the RAM console. In the left-side navigation pane, choose .
-
On the Users page, find the target RAM user and click the username.
-
On the Authentication tab, click Create AccessKey.
NoteA RAM user can have a maximum of two AccessKey pairs. One is for active use, and the second should be created only when you need to rotate credentials.
-
In the confirmation dialog box, select CLI, select the I confirm that it is necessary to create an AccessKey checkbox, and then click Continue.
-
Follow the on-screen instructions to complete multi-factor authentication (MFA).
-
In the Create AccessKey dialog box, click Download CSV File to save the AccessKey ID and AccessKey secret. Then, click OK.
ImportantThe AccessKey secret is displayed only upon creation and cannot be retrieved later. You must store it in a secure location.
Use an AccessKey pair to call APIs
This tutorial uses the Alibaba Cloud CLI as an example. To learn how to use an AccessKey pair in your code, see the following topics:
Direct access
This method is simple but introduces risks such as hard-coded keys and excessive permissions, as the AccessKey pair is configured directly on the client. This approach is not recommended for production environments.
-
Grant the RAM user with the AccessKey pair read-only permissions to RAM by attaching the
AliyunRAMReadOnlyAccesspolicy.-
Log on to the RAM console. In the left-side navigation pane, choose .
-
Find the target RAM user and click Add Permissions in the Actions column.
-
In the Add Permissions panel, search for and select the
AliyunRAMReadOnlyAccesssystem policy, and then click OK. -
Return to the Users page and click the username of the target RAM user. On the tab, verify that the
AliyunRAMReadOnlyAccesspolicy is listed.
-
-
Open your local terminal and run the following command to configure your credentials interactively.
aliyun configure --profile AkProfile -
At the prompts, enter the AccessKey ID and AccessKey secret from the Create an AccessKey pair step. Then, set the default region and language.
Configuring profile 'AkProfile' in 'AK' authenticate mode... Access Key Id []: <yourAccessKeyID> Access Key Secret []: <yourAccessKeySecret> Default Region Id []: cn-shanghai Default Output Format [json]: json (Only support json) Default Language [zh|en] en: en Saving profile[AkProfile] ...Done. -
If the AccessKey pair and other configurations are correct, the terminal returns a success message. If an error occurs, delete the cached Alibaba Cloud CLI credential configuration, verify your settings, and then rerun the command from Step 2.
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 .............. -
Run the following command to test the API call.
aliyun ram ListUsers --profile AkProfileIf the configuration is successful, the command returns a list of RAM users. The output is similar to the following:
{ "IsTruncated": false, "RequestId": "B3CDEF9E-A3F4-58B0-80BE-54576991****", "Users": { "User": [ { "Comments": "", "CreateDate": "2025-10-29T02:47:52Z", "DisplayName": "username", "UpdateDate": "2025-10-29T02:47:52Z", "UserId": "20376656170607****", "UserName": "username" }, ... ] } }
Indirect access
This method uses the long-term AccessKey pair to obtain a temporary STS token for making API calls. Although the configuration is more complex, this approach is more secure because it reduces the risk of exposing the long-term credential and allows for dynamic permission management through roles.
-
Grant the RAM user with the AccessKey pair permission to call the STS
AssumeRoleoperation by attaching theAliyunSTSAssumeRoleAccesspolicy.-
Log on to the RAM console. In the left-side navigation pane, choose .
-
Find the target RAM user and click Add Permissions in the Actions column.
-
In the Add Permissions panel, search for and select the
AliyunSTSAssumeRoleAccesssystem policy, and then click OK. -
Return to the Users page and click the username of the target RAM user. On the tab, verify that the
AliyunSTSAssumeRoleAccesspolicy is listed.
-
-
Create a RAM role and obtain its role ARN.
-
In the RAM console, choose in the left-side navigation pane.
-
On the Roles page, click Create Role.
-
On the Create Role page, select Cloud Account for Principal Type, specify the Alibaba Cloud account, and then click OK.
-
In the Create Role panel, enter a role name, such as
cli-test-role, and click OK. -
On the role details page, find the ARN in the Basic Information section. Click Copy and save the ARN for later use.
-
-
Grant the RAM role read-only permissions to RAM by attaching the
AliyunRAMReadOnlyAccesspolicy.Go to the page, click Add Permissions in the Actions column for the role, and add the permission as described in Step 1.
-
Open your local terminal and run the following command to configure your credentials interactively.
aliyun configure --profile RamRoleArnProfile --mode RamRoleArn -
At the prompts, enter the AccessKey ID and AccessKey secret from the Create an AccessKey pair step, and the role ARN from Step 2. For other parameters, refer to the following example.
Configuring profile 'RamRoleArnProfile' in 'RamRoleArn' authenticate mode... Access Key Id []: <yourAccessKeyID> Access Key Secret []: <yourAccessKeySecret> Sts Region []: cn-shanghai Ram Role Arn []: acs:ram::012345678910****:role/cli-test-role Role Session Name []: cli-test-role External ID []: abcd1234 Expired Seconds [900]: 900 Default Region Id []: cn-shanghai Default Output Format [json]: json (Only support json) Default Language [zh|en] en: en Saving profile[RamRoleArnProfile] ...Done. -
If the AccessKey pair and other configurations are correct, the terminal returns a success message. If an error occurs, delete the cached Alibaba Cloud CLI credential configuration, verify your settings, and then rerun the command from Step 4.
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 .............. -
Run the following command to test the API call.
aliyun ram ListUsers --profile RamRoleArnProfileIf the configuration is successful, the command returns a list of RAM users. The output is similar to the following:
{ "IsTruncated": false, "RequestId": "B3CDEF9E-A3F4-58B0-80BE-54576991****", "Users": { "User": [ { "Comments": "", "CreateDate": "2025-10-29T02:47:52Z", "DisplayName": "username", "UpdateDate": "2025-10-29T02:47:52Z", "UserId": "20376656170607****", "UserName": "username" }, ... ] } }
Clean up resources
Delete cached CLI credentials
Run the appropriate command to delete the cached credential configuration.
-
If you followed the steps in Use an AccessKey pair directly, run the following command:
aliyun configure delete --profile AkProfile -
If you followed the steps in Use an AccessKey pair indirectly, run the following command:
aliyun configure delete --profile RamRoleArnProfile
Disable and delete the AccessKey pair
-
Log on to the RAM console. In the left-side navigation pane, choose .
-
Find the target RAM user for whom you created the AccessKey pair, and click the username.
-
Disable the AccessKey pair: On the Authentication tab, find the target AccessKey pair and click Disable in the Actions column. In the Disable dialog box, click Disable.
-
Move the AccessKey pair to the recycle bin: Click Delete in the Actions column for the target AccessKey pair. In the confirmation dialog box, enter the AccessKey ID, and then click Move to Recycle Bin.
-
Permanently delete the AccessKey pair: On the user details page, in the AccessKey Pair Recycle Bin section, find the target AccessKey pair and click Delete in the Actions column. In the confirmation dialog box, enter the AccessKey ID, and then click Delete.
References
For more information, see the following topics: