All Products
Search
Document Center

Elastic Compute Service:AttachKeyPair

Last Updated:Sep 11, 2026

Attaches an SSH key pair to one or more Linux instances.

Operation description

Operation description

When you call this operation, take note of the following items:

  • Windows instances do not support SSH key pairs.

  • After you attach an SSH key pair, authentication by using a username and password is disabled.

  • If an instance is in the Running state, restart the instance by calling RebootInstance for the SSH key pair to take effect.

  • If an instance is in the Stopped state, start the instance by calling StartInstance for the SSH key pair to take effect.

  • If an SSH key pair is already attached to an instance, the new SSH key pair automatically replaces the original one.

  • This is a batch operation that supports partial success. An HTTP 200 response indicates only that the request has been processed, not that all instances are successfully attached. Check the FailCount value and the Code and Success fields of each element in Results.Result[] to determine the result for each instance:
    • Request-level errors: Errors such as invalid parameters, nonexistent key pair, or more than 50 instances are intercepted before batch execution. The error is returned at the top level of the response with a non-200 HTTP status code, and the entire batch request fails.

    • Instance-level errors: Errors such as a Windows instance, an unsupported instance state, or an unsupported instance type affect only the corresponding instance. The error code is recorded in the corresponding Results.Result[] element, and the overall request still returns HTTP 200.

Try it now

Try this API in OpenAPI Explorer, no manual signing needed. Successful calls auto-generate SDK code matching your parameters. Download it with built-in credential security for local usage.

Test

RAM authorization

The table below describes the authorization required to call this API. You can define it in a Resource Access Management (RAM) policy. The table's columns are detailed below:

  • Action: The actions can be used in the Action element of RAM permission policy statements to grant permissions to perform the operation.

  • API: The API that you can call to perform the action.

  • Access level: The predefined level of access granted for each API. Valid values: create, list, get, update, and delete.

  • Resource type: The type of the resource that supports authorization to perform the action. It indicates if the action supports resource-level permission. The specified resource must be compatible with the action. Otherwise, the policy will be ineffective.

    • For APIs with resource-level permissions, required resource types are marked with an asterisk (*). Specify the corresponding Alibaba Cloud Resource Name (ARN) in the Resource element of the policy.

    • For APIs without resource-level permissions, it is shown as All Resources. Use an asterisk (*) in the Resource element of the policy.

  • Condition key: The condition keys defined by the service. The key allows for granular control, applying to either actions alone or actions associated with specific resources. In addition to service-specific condition keys, Alibaba Cloud provides a set of common condition keys applicable across all RAM-supported services.

  • Dependent action: The dependent actions required to run the action. To complete the action, the RAM user or the RAM role must have the permissions to perform all dependent actions.

Action

Access level

Resource type

Condition key

Dependent action

ecs:AttachKeyPair

update

*Instance

acs:ecs:{#regionId}:{#accountId}:instance/{#instanceId}

*KeyPair

acs:ecs:{#regionId}:{#accountId}:keypair/{#keypairName}

None None

Request parameters

Parameter

Type

Required

Description

Example

RegionId

string

Yes

The region ID of the SSH key pair. You can call DescribeRegions to query the most recent region list.

cn-hangzhou

KeyPairName

string

Yes

The name of the SSH key pair.

testKeyPairName

InstanceIds

string

Yes

The IDs of the instances to which you want to attach the SSH key pair. The value can be a JSON array that consists of up to 50 instance IDs. Separate the IDs with commas (,).

["i-bp1gtjxuuvwj17zr****", "i-bp17b7zrsbjwvmfy****", … "i-bp1h6jmbefj1ytos****"]

Response elements

Element

Type

Description

Example

object

KeyPairName

string

The name of the key pair.

testKeyPairName

RequestId

string

The request ID.

473469C7-AA6F-4DC5-B3DB-A3*****83E

TotalCount

string

The total number of instances to which the key pair is attached.

2

FailCount

string

The number of instances to which the key pair failed to be attached.

1

Results

object

Result

array<object>

The collection of results that indicate whether the key pair is attached to each instance.

object

The result.

Code

string

The operation status code. A value of 200 indicates that the operation is successful.

200

Message

string

The operation message. If Code is 200, the message is successful.

successful

InstanceId

string

The instance ID.

i-m5eg7be9ndloji64****

Success

string

Indicates whether the operation is successful.

true

Examples

Success response

JSON format

{
  "KeyPairName": "testKeyPairName",
  "RequestId": "473469C7-AA6F-4DC5-B3DB-A3*****83E",
  "TotalCount": "2",
  "FailCount": "1",
  "Results": {
    "Result": [
      {
        "Code": "200",
        "Message": "successful",
        "InstanceId": "i-m5eg7be9ndloji64****",
        "Success": "true"
      }
    ]
  }
}

Error codes

HTTP status code

Error code

Error message

Description

400 InvalidKeyPairName.NotFound The specified KeyPairName does not exist in our records. The specified KeyPairName parameter does not exist.
400 InvalidInstanceIds.ValueNotSupported The specified parameter InstanceIds is not valid. The specified InstanceIds parameter is invalid.
400 DependencyViolation.IoOptimize The specified parameter InstanceIds is not valid. The specified instance ID is invalid. The specified instance is not an I/O optimized instance.
403 DependencyViolation.WindowsInstance The instance creating is windows, cannot use ssh key pair to login The specified instance is a Windows instance and does not support logons using SSH key pairs.
403 InstanceKeyPairLimitExceeded The specified instances are beyond the permitted range.

See Error Codes for a complete list.

Release notes

See Release Notes for a complete list.