Working with |EC2| Key Pairs
To create a key pair, call the |ec2client|'s :methodname:`createKeyPair` method with a :aws-java-class:`CreateKeyPairRequest <services/ec2/model/CreateKeyPairRequest>` that contains the key's name.
Imports
.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/CreateKeyPair.java :lines: 16-19 :language: java
Code
.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/CreateKeyPair.java :lines: 39-44 :dedent: 8 :language: java
See the :sdk-examples-java-ec2:`complete example <CreateKeyPair.java>`.
To list your key pairs or to get information about them, call the |ec2client|'s :methodname:`describeKeyPairs` method. It returns a :aws-java-class:`DescribeKeyPairsResult <services/ec2/model/DescribeKeyPairsResult>` that you can use to access the list of key pairs by calling its :methodname:`getKeyPairs` method, which returns a list of :aws-java-class:`KeyPairInfo <services/ec2/model/KeyPairInfo>` objects.
Imports
.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/DescribeKeyPairs.java :lines: 16-19 :language: java
Code
.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/DescribeKeyPairs.java :lines: 28-38 :dedent: 8 :language: java
See the :sdk-examples-java-ec2:`complete example <DescribeKeyPairs.java>`.
To delete a key pair, call the |ec2client|'s :methodname:`deleteKeyPair` method, passing it a :aws-java-class:`DeleteKeyPairRequest <services/ec2/model/DeleteKeyPairRequest>` that contains the name of the key pair to delete.
Imports
.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/DeleteKeyPair.java :lines: 16-19 :language: java
Code
.. literalinclude:: example_code/ec2/src/main/java/aws/example/ec2/DeleteKeyPair.java :lines: 39-44 :dedent: 8 :language: java
See the :sdk-examples-java-ec2:`complete example <DeleteKeyPair.java>`.