Skip to content

create_key_pair

create_key_pair(
    *,
    config_overrides: Optional[EC2ClientConfig] = None,
    key_name: Optional[String] = None,
    key_type: Optional[KeyType] = None,
    tag_specifications: Optional[TagSpecificationList] = None,
    key_format: Optional[KeyFormat] = None,
    dry_run: Optional[Boolean] = None,
) -> KeyPair

Creates an ED25519 or 2048-bit RSA key pair with the specified name and in the specified format. Amazon EC2 stores the public key and displays the private key for you to save to a file. The private key is returned as an unencrypted PEM encoded PKCS#1 private key or an unencrypted PPK formatted private key for use with PuTTY. If a key with the specified name already exists, Amazon EC2 returns an error.

The key pair returned to you is available only in the Amazon Web Services Region in which you create it. If you prefer, you can create your own key pair using a third-party tool and upload it to any Region using ImportKeyPair.

You can have up to 5,000 key pairs per Amazon Web Services Region.

For more information, see Amazon EC2 key pairs in the Amazon EC2 User Guide.

Parameters:

  • key_name (Optional[String], default: None ) –

    A unique name for the key pair.

    Constraints: Up to 255 ASCII characters

  • key_type (Optional[KeyType], default: None ) –

    The type of key pair. Note that ED25519 keys are not supported for Windows instances.

    Default: rsa

  • tag_specifications (Optional[TagSpecificationList], default: None ) –

    The tags to apply to the new key pair.

  • key_format (Optional[KeyFormat], default: None ) –

    The format of the key pair.

    Default: pem

  • dry_run (Optional[Boolean], default: None ) –

    Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is DryRunOperation. Otherwise, it is UnauthorizedOperation.

Raises:

Examples:

To create a key pair This example creates a key pair named my-key-pair.

>>> client.create_key_pair(key_name='my-key-pair')
Source code in docs_src/capo_ec2/_services/ec2.py
def create_key_pair(
    self,
    *,
    config_overrides: Optional[EC2ClientConfig] = None,
    key_name: Optional["capo_ec2.types.string.String"] = None,
    key_type: Optional["capo_ec2.types.key_type.KeyType"] = None,
    tag_specifications: Optional[
        "capo_ec2.types.tag_specification_list.TagSpecificationList"
    ] = None,
    key_format: Optional["capo_ec2.types.key_format.KeyFormat"] = None,
    dry_run: Optional["capo_ec2.types.boolean.Boolean"] = None,
) -> "capo_ec2.types.key_pair.KeyPair":
    """<p>Creates an ED25519 or 2048-bit RSA key pair with the specified name and in the specified format. Amazon EC2 stores the public key and displays the private key for you to save to a file. The private key is returned as an unencrypted PEM encoded PKCS#1 private key or an unencrypted PPK formatted private key for use with PuTTY. If a key with the specified name already exists, Amazon EC2 returns an error.</p> <p>The key pair returned to you is available only in the Amazon Web Services Region in which you create it. If you prefer, you can create your own key pair using a third-party tool and upload it to any Region using <a>ImportKeyPair</a>.</p> <p>You can have up to 5,000 key pairs per Amazon Web Services Region.</p> <p>For more information, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html">Amazon EC2 key pairs</a> in the <i>Amazon EC2 User Guide</i>.</p>

    Args:
        key_name: <p>A unique name for the key pair.</p> <p>Constraints: Up to 255 ASCII characters</p>
        key_type: <p>The type of key pair. Note that ED25519 keys are not supported for Windows instances.</p> <p>Default: <code>rsa</code> </p>
        tag_specifications: <p>The tags to apply to the new key pair.</p>
        key_format: <p>The format of the key pair.</p> <p>Default: <code>pem</code> </p>
        dry_run: <p>Checks whether you have the required permissions for the action, without actually making the request, and provides an error response. If you have the required permissions, the error response is <code>DryRunOperation</code>. Otherwise, it is <code>UnauthorizedOperation</code>.</p>

    Raises:
        capo_ec2.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        To create a key pair
        This example creates a key pair named my-key-pair.

        >>> client.create_key_pair(key_name='my-key-pair')
    """

    def _handler(
        req: "OperationRequest[capo_ec2.types.create_key_pair_request.CreateKeyPairRequest]",
    ) -> OperationResponse["capo_ec2.types.key_pair.KeyPair"]:
        import capo_ec2._operations.amazon_ec2.create_key_pair

        output, http_response = (
            capo_ec2._operations.amazon_ec2.create_key_pair.create_key_pair(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    def _redact_output(output: "capo_ec2.types.key_pair.KeyPair") -> dict[str, Any]:
        import capo_ec2.types.key_pair

        return capo_ec2.types.key_pair.redact_sensitive(output)

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging("CreateKeyPair", redact_output=_redact_output),
    )
    input_: capo_ec2.types.create_key_pair_request.CreateKeyPairRequest = {}
    if key_name is not None:
        input_["key_name"] = key_name
    if key_type is not None:
        input_["key_type"] = key_type
    if tag_specifications is not None:
        input_["tag_specifications"] = tag_specifications
    if key_format is not None:
        input_["key_format"] = key_format
    if dry_run is not None:
        input_["dry_run"] = dry_run

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