- Documentation
Elastic Compute Service
Elastic Compute Service
API Reference
Key pairs
AttachKeyPair - Associates key pairs
Key pairs
AttachKeyPair - Associates key pairs
AttachKeyPair - Associates key pairs
Call AttachKeyPair to associate a key pair with one or more Linux instances.
Request description
- You can associate a key pair with an instance only when the instance is installed with the password plugin. Please install the password plugin first.
- Currently, you can associate a key pair with an instance only when the instance is in the Running or Stopped status.
- If you have already associated a key pair with an instance in the console, the newly associated key pair overwrites the existing key pair. However, this does not affect any key pairs that you have manually added within the instance.
- After you associate a key pair with an instance, you need to restart the instance for the key pair to take effect. If your instance is configured for password login, the password login method is automatically disabled after you associate a key pair with the instance and restart the instance.
Debugging
API Explorer
You can initiate calls online through API Explorer without paying attention to the signature generation process and quickly obtain the call results.
Request parameters
Only the request parameters specific to this operation and some common parameters are listed below. For more information, please refer to Common parameters. Action string required Sample value: AttachKeyPair
The action that you want to perform. Value: AttachKeyPair.
Version string required Sample value: 2020-04-01
The version of the API. Valid value: 2020-04-01.
ClientToken string Optional Sample value: 333456899ff****
The ClientToken is used to ensure the request idempotency. You can use the client to automatically generate a token, but you must make sure that the token is unique among different requests. This prevents repeated actions due to multiple client retries when the call of the API action times out or an internal server error occurs.
Value: Only ASCII characters are supported. The length is no more than 64 ASCII characters.
InstanceIds.N string[] required Sample value: InstanceIds.1=i-ahipakt2gcg95jpv****&InstanceIds.2=i-ahipakt2gdg95lbe****
The instance ID. A maximum of 100 instance IDs are supported.
- Argument - N: specifies the serial number of the instance.
- Separate multiple instance IDs with &.
KeyPairId string Optional Sample value: kp-ybqcopwrdwl8j1******
The unique ID of the key pair. You can call the DescribeKeyPairs operation to query and obtain it. - The KeyPairName and KeyPairId parameters cannot both be empty.
- If the KeyPairName and KeyPairId parameters are configured at the same time, KeyPairId takes precedence.
KeyPairName string Optional Sample value: ssh_key_pair
Name of the key pair.
- The KeyPairName and KeyPairId parameters cannot both be empty.
- If the KeyPairName and KeyPairId parameters are configured at the same time, KeyPairId takes precedence.
Response parameters
Only the response parameters specific to this operation are listed below. For more information, refer to Response structure. tip
The following error codes for the operation are returned only when invalid values are specified for request parameters. If valid values are specified for the request parameters, no error code is displayed, and a status code of 200 is returned. However, this does not mean that the operation on all key pairs is successful. To view the operation result on one key pair, see the information in the following response parameters. OperationDetails object[] Sample value: --
The operation details.
Error object Sample value: --
The error message for the key pair being operated on.
Code string Sample value: InvalidInstance.NotFound
The error code for the failure of a specific request during batch operations. Valid values:
- InvalidInstance.NotFound: The specified instance does not exist.
- InvalidKeyPairForWindows.Unsupported: The specified instance is based on the Windows operating system and does not support key pairs.
- InvalidResetAuthenticationAgent.NotFound: The specified reset-authentication plug-in does not exist.
- InvalidInstanceStatus: The current status of the instance does not support this operation.
- FlowLimitExceeded: You have exceeded the rate limit for resource requests.
tip
Each Code corresponds to a Message in sequence.
Message string Sample value: The specified instance does not exist.
The error information for the failure of a specific request during batch operations. Valid values:
- The specified instance does not exist.
- The OSType of this specified instance is Windows, which does not support key pairs.
- The specified reset-authentication agent does not install on the instance.
- The status of the specified instance does not support this request.
- You've reached the limit on request rate of resources.
tip
Each Code corresponds to a Message in sequence.
InstanceId string Sample value: i-ybo349sxoncm9t******
The ID of the instance on which the operation is performed.
Sample request
Sample response
Error codes
The table below lists the business-related error codes of the operation. For more information, refer to Common error codes.
Last updated: 2026.06.18 17:48:24