You can call this operation to attach an SSH key pair to one or more Linux instances.

Description

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

  • SSH key pairs are not supported on Windows instances.
  • If an SSH key pair is attached to an instance, the username and password authentication is disabled for the instance.
  • If an instance is in the Running (Running) state, call the RebootInstance operation to restart the instance for the SSH key pair to take effect.
  • If an instance is in the Stopped (Stopped) state, call the StartInstance operation to start the instance for the SSH key pair to take effect.
  • If an instance already has an SSH key pair attached, the new SSH key pair will automatically replace the original one.

Debugging

OpenAPI Explorer automatically calculates the signature value. For your convenience, we recommend that you call this operation in OpenAPI Explorer. OpenAPI Explorer dynamically generates the sample code of the operation for different SDKs.

Request parameters

Parameter Type Required Example Description
Action String Yes AttachKeyPair

The operation that you want to perform. Set the value to AttachKeyPair.

InstanceIds String Yes ["i-bp1gtjxuuvwj17zr****", "i-bp17b7zrsbjwvmfy****", … "i-bp1h6jmbefj1ytos****"]

The IDs of 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 multiple instance IDs with commas (,).

KeyPairName String Yes testKeyPairName

The name of the SSH key pair.

RegionId String Yes cn-hangzhou

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

Response parameters

Parameter Type Example Description
FailCount String 0

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

KeyPairName String testKeyPairName

The name of the key pair.

RequestId String 473469C7-AA6F-4DC5-B3DB-A3DC0DE3C83E

The ID of the request.

Results Array of Result

An array consisting of result data.

Result
Code String 200

The operation status code returned. 200 indicates that the operation is successful.

InstanceId String i-m5eg7be9ndloji64****

The ID of the instance.

Message String successful

The operation information returned. When the value of Code is 200, the value of Message becomes successful.

Success String true

Indicates whether the operation is successful.

TotalCount String 2

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

Examples

Sample requests

https://ecs.aliyuncs.com/?Action=AttachKeyPair
&InstanceIds=["i-bp1gtjxuuvwj17zr****", "i-bp17b7zrsbjwvmfy****", … "i-bp1h6jmbefj1ytos****"]
&KeyPairName=testKeyPairName
&RegionId=cn-hangzhou
&<Common request parameters>

Sample success responses

XML format

<AttachKeyPairResponse>
      <TotalCount>2</TotalCount>
      <RequestId>4ADF7A06-66BD-4FBF-A2ED-2364E41D8C06</RequestId>
      <Results>
            <Result>
                  <Message>successful</Message>
                  <InstanceId>i-m5eg7be9ndloji64****</InstanceId>
                  <Success>true</Success>
                  <Code>200</Code>
            </Result>
            <Result>
                  <Message>successful</Message>
                  <InstanceId>i-m5e25x2mwr0hk33d****</InstanceId>
                  <Success>true</Success>
                  <Code>200</Code>
            </Result>
      </Results>
      <FailCount>0</FailCount>
</AttachKeyPairResponse>

JSON format

{
    "TotalCount": 2,
    "RequestId": "4ADF7A06-66BD-4FBF-A2ED-2364E41D8C06",
    "Results": {
        "Result": [
            {
                "Message": "successful",
                "InstanceId": "i-m5eg7be9ndloji64****",
                "Success": true,
                "Code": "200"
            },
            {
                "Message": "successful",
                "InstanceId": "i-m5e25x2mwr0hk33d****",
                "Success": true,
                "Code": "200"
            }
        ]
    },
    "FailCount": 0
}

Error codes

HTTP status code Error code Error message Description
400 InvalidKeyPairName.NotFound The specified KeyPairName does not exist in our records. The error message returned because the specified key pair name does not exist.
403 DependencyViolation.WindowsInstance The instance creating is windows, cannot use ssh key pair to login The error message returned because the specified instance is a Windows instance and does not support logons with SSH key pairs.
400 InvalidInstanceIds.ValueNotSupported The specified parameter InstanceIds is not valid. The error message returned because the specified InstanceIds parameter is invalid.
400 DependencyViolation.IoOptimize The specified parameter InstanceIds is not valid. The error message returned because the specified instance is not an I/O optimized instance.

For a list of error codes, visit the API Error Center.