You need to enable JavaScript to run this app.
Elastic Compute Service

Elastic Compute Service

Copy page
Download PDF
Key pairs
AttachKeyPair - Associates key pairs
Copy page
Download PDF
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​
​
GET /?Action=AttachKeyPair&Version=2020-04-01&KeyPairName=ssh_key_pair1&InstanceIds.1=i-ahipakt2gcg95jpv****&InstanceIds.2=i-ahipakt2gdg95lbe**** HTTP/1.1​
Host: ecs.cn-beijing.volcengineapi.com​
Region: cn-beijing​
Service: ecs​
​
Sample response​
​
{​
"ResponseMetadata": {​
"RequestId": "202106111445100102252431000052****",​
"Action": "AttachKeyPair",​
"Version": "2020-04-01",​
"Service": "ecs",​
"Region": "cn-beijing"​
},​
"Result": {​
"OperationDetails": [​
{​
"InstanceId": "i-ahipakt2gcg95jpv****",​
"Error": null​
},​
{​
"InstanceId": "i-ahipakt2gdg95lbe****",​
"Error": {​
"Code": "InvalidInstance.NotFound",​
"Message": "The specified instance does not exist."​
}​
}​
]​
}​
}​
​
Error codes​
The table below lists the business-related error codes of the operation. For more information, refer to Common error codes.​
​
Status code​
Error code​
Error message​
Description​
400​
IdempotentParameterMismatch​
The request uses the same client token as a previous, but non-identical request. Do not reuse a client token with different requests, unless the requests are identical.​
The idempotent parameters are inconsistent.​
400​
InvalidClientToken.Malformed​
The specified ClientToken is malformed.​
The specified ClientToken is invalid.​
400​
LimitExceeded.MaximumInstanceIds​
You've reached the limit on the number of InstanceIds that you can set.​
The specified number of instance IDs exceeds the upper limit.​
400​
MissingParameter.InstanceIds​
The required parameter InstanceIds is not supplied.​
The parameter InstanceIds cannot be left empty.​
400​
MissingParameter.KeyPairNameAndKeyPairId​
The required parameters KeyPairName and KeyPairId are not supplied.​
The key pair name and ID cannot be both empty.​
404​
InvalidKeyPair.NotFound​
The specified KeyPair does not exist.​
The specified key pair does not exist.​
412​
IdempotentProcessing​
The request uses the same client token as a previous one that is still in process.​
The previous idempotent request is still in process. Please try again later.​
500​
InternalError​
An internal error has occurred.​
An internal error occurred. Try again. If multiple attempts fail, submit a ticket.​
​
​
Last updated: 2026.06.18 17:48:24