Skip to content

send_ssh_public_key

send_ssh_public_key(
    instance_id: InstanceId,
    instance_os_user: InstanceOSUser,
    ssh_public_key: SSHPublicKey,
    *,
    config_overrides: Optional[EC2InstanceConnectClientConfig] = None,
    availability_zone: Optional[AvailabilityZone] = None,
) -> SendSSHPublicKeyResponse

Pushes an SSH public key to the specified EC2 instance for use by the specified user. The key remains for 60 seconds. For more information, see Connect to your Linux instance using EC2 Instance Connect in the Amazon EC2 User Guide.

Parameters:

  • instance_id (InstanceId) –

    The ID of the EC2 instance.

  • instance_os_user (InstanceOSUser) –

    The OS user on the EC2 instance for whom the key can be used to authenticate.

  • ssh_public_key (SSHPublicKey) –

    The public key material. To use the public key, you must have the matching private key.

  • availability_zone (Optional[AvailabilityZone], default: None ) –

    The Availability Zone in which the EC2 instance was launched.

Raises:

  • AuthException –

    Either your AWS credentials are not valid or you do not have access to the EC2 instance.

  • EC2InstanceNotFoundException –

    The specified instance was not found.

  • EC2InstanceStateInvalidException –

    Unable to connect because the instance is not in a valid state. Connecting to a stopped or terminated instance is not supported. If the instance is stopped, start your instance, and try to connect again.

  • EC2InstanceUnavailableException –

    The instance is currently unavailable. Wait a few minutes and try again.

  • InvalidArgsException –

    One of the parameters is not valid.

  • ServiceException –

    The service encountered an error. Follow the instructions in the error message and try again.

  • ThrottlingException –

    The requests were made too frequently and have been throttled. Wait a while and try again. To increase the limit on your request frequency, contact AWS Support.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Examples:

To push an SSH key to an EC2 instance The following example pushes a sample SSH public key to the EC2 instance i-abcd1234 in AZ us-west-2b for use by the instance OS user ec2-user.

>>> client.send_ssh_public_key(instance_id='i-abcd1234', instance_os_user='ec2-user', ssh_public_key='ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3FlHqj2eqCdrGHuA6dRjfZXQ4HX5lXEIRHaNbxEwE5Te7xNF7StwhrDtiV7IdT5fDqbRyGw/szPj3xGkNTVoElCZ2dDFb2qYZ1WLIpZwj/UhO9l2mgfjR56UojjQut5Jvn2KZ1OcyrNO0J83kCaJCV7JoVbXY79FBMUccYNY45zmv9+1FMCfY6i2jdIhwR6+yLk8oubL8lIPyq7X+6b9S0yKCkB7Peml1DvghlybpAIUrC9vofHt6XP4V1i0bImw1IlljQS+DUmULRFSccATDscCX9ajnj7Crhm0HAZC0tBPXpFdHkPwL3yzYo546SCS9LKEwz62ymxxbL9k7h09t', availability_zone='us-west-2a')
Source code in docs_src/capo_ec2_instance_connect/_services/ec2_instance_connect.py
def send_ssh_public_key(
    self,
    instance_id: "capo_ec2_instance_connect.types.instance_id.InstanceId",
    instance_os_user: "capo_ec2_instance_connect.types.instance_os_user.InstanceOSUser",
    ssh_public_key: "capo_ec2_instance_connect.types.ssh_public_key.SSHPublicKey",
    *,
    config_overrides: Optional[EC2InstanceConnectClientConfig] = None,
    availability_zone: Optional[
        "capo_ec2_instance_connect.types.availability_zone.AvailabilityZone"
    ] = None,
) -> "capo_ec2_instance_connect.types.send_ssh_public_key_response.SendSSHPublicKeyResponse":
    """<p>Pushes an SSH public key to the specified EC2 instance for use by the specified user. The key remains for 60 seconds. For more information, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Connect-using-EC2-Instance-Connect.html">Connect to your Linux instance using EC2 Instance Connect</a> in the <i>Amazon EC2 User Guide</i>.</p>

    Args:
        instance_id: <p>The ID of the EC2 instance.</p>
        instance_os_user: <p>The OS user on the EC2 instance for whom the key can be used to authenticate.</p>
        ssh_public_key: <p>The public key material. To use the public key, you must have the matching private key.</p>
        availability_zone: <p>The Availability Zone in which the EC2 instance was launched.</p>

    Raises:
        capo_ec2_instance_connect.errors.auth_exception.AuthException: <p>Either your AWS credentials are not valid or you do not have access to the EC2 instance.</p>
        capo_ec2_instance_connect.errors.ec2_instance_not_found_exception.EC2InstanceNotFoundException: <p>The specified instance was not found.</p>
        capo_ec2_instance_connect.errors.ec2_instance_state_invalid_exception.EC2InstanceStateInvalidException: <p>Unable to connect because the instance is not in a valid state. Connecting to a stopped or terminated instance is not supported. If the instance is stopped, start your instance, and try to connect again.</p>
        capo_ec2_instance_connect.errors.ec2_instance_unavailable_exception.EC2InstanceUnavailableException: <p>The instance is currently unavailable. Wait a few minutes and try again.</p>
        capo_ec2_instance_connect.errors.invalid_args_exception.InvalidArgsException: <p>One of the parameters is not valid.</p>
        capo_ec2_instance_connect.errors.service_exception.ServiceException: <p>The service encountered an error. Follow the instructions in the error message and try again.</p>
        capo_ec2_instance_connect.errors.throttling_exception.ThrottlingException: <p>The requests were made too frequently and have been throttled. Wait a while and try again. To increase the limit on your request frequency, contact AWS Support.</p>
        capo_ec2_instance_connect.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        To push an SSH key to an EC2 instance
        The following example pushes a sample SSH public key to the EC2 instance i-abcd1234 in AZ us-west-2b for use by the instance OS user ec2-user.

        >>> client.send_ssh_public_key(instance_id='i-abcd1234', instance_os_user='ec2-user', ssh_public_key='ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3FlHqj2eqCdrGHuA6dRjfZXQ4HX5lXEIRHaNbxEwE5Te7xNF7StwhrDtiV7IdT5fDqbRyGw/szPj3xGkNTVoElCZ2dDFb2qYZ1WLIpZwj/UhO9l2mgfjR56UojjQut5Jvn2KZ1OcyrNO0J83kCaJCV7JoVbXY79FBMUccYNY45zmv9+1FMCfY6i2jdIhwR6+yLk8oubL8lIPyq7X+6b9S0yKCkB7Peml1DvghlybpAIUrC9vofHt6XP4V1i0bImw1IlljQS+DUmULRFSccATDscCX9ajnj7Crhm0HAZC0tBPXpFdHkPwL3yzYo546SCS9LKEwz62ymxxbL9k7h09t', availability_zone='us-west-2a')
    """

    def _handler(
        req: "OperationRequest[capo_ec2_instance_connect.types.send_ssh_public_key_request.SendSSHPublicKeyRequest]",
    ) -> OperationResponse[
        "capo_ec2_instance_connect.types.send_ssh_public_key_response.SendSSHPublicKeyResponse"
    ]:
        import capo_ec2_instance_connect._operations.awsec2_instance_connect_service.send_ssh_public_key

        output, http_response = (
            capo_ec2_instance_connect._operations.awsec2_instance_connect_service.send_ssh_public_key.send_ssh_public_key(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("SendSSHPublicKey")
    )
    input_: capo_ec2_instance_connect.types.send_ssh_public_key_request.SendSSHPublicKeyRequest = {
        "instance_id": instance_id,
        "instance_os_user": instance_os_user,
        "ssh_public_key": ssh_public_key,
    }
    if availability_zone is not None:
        input_["availability_zone"] = availability_zone

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output