Work with Amazon EC2 key pairs - AWS SDK for Java
AWS services or capabilities described in AWS documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with AWS services in China.

At Amazon Web Services (AWS), we’re focused on finding ways to improve our products and provide a better customer experience. To do that, we need your feedback. Please take 5 minutes of your time to share insights regarding your experience with Java Spring and your need for Spring integration with AWS.

Click here to take a quick survey

This survey is hosted by an external company (Qualtrics), so the link above does not lead to our website. Please note that AWS will own the data gathered via this survey, and will not share the information/results collected with survey respondents. AWS handles your information as described in the AWS Privacy Notice.

Work with Amazon EC2 key pairs

Create a key pair

To create a key pair, call the Ec2Client’s createKeyPair method with a CreateKeyPairRequest that contains the key’s name.

Imports

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.CreateKeyPairRequest; import software.amazon.awssdk.services.ec2.model.CreateKeyPairResponse; import software.amazon.awssdk.services.ec2.model.Ec2Exception;

Code

public static void createEC2KeyPair(Ec2Client ec2,String keyName ) { try { CreateKeyPairRequest request = CreateKeyPairRequest.builder() .keyName(keyName).build(); CreateKeyPairResponse response = ec2.createKeyPair(request); System.out.printf( "Successfully created key pair named %s", keyName); } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); }

See the complete example on GitHub.

Describe key pairs

To list your key pairs or to get information about them, call the Ec2Client’s describeKeyPairs method. It returns a DescribeKeyPairsResponse that you can use to access the list of key pairs by calling its keyPairs method, which returns a list of KeyPairInfo objects.

Imports

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.DescribeKeyPairsResponse; import software.amazon.awssdk.services.ec2.model.KeyPairInfo; import software.amazon.awssdk.services.ec2.model.Ec2Exception;

Code

public static void describeEC2Keys( Ec2Client ec2){ try { DescribeKeyPairsResponse response = ec2.describeKeyPairs(); for(KeyPairInfo keyPair : response.keyPairs()) { System.out.printf( "Found key pair with name %s " + "and fingerprint %s", keyPair.keyName(), keyPair.keyFingerprint()); } } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); }

See the complete example on GitHub.

Delete a key pair

To delete a key pair, call the Ec2Client’s deleteKeyPair method, passing it a DeleteKeyPairRequest that contains the name of the key pair to delete.

Imports

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.DeleteKeyPairRequest; import software.amazon.awssdk.services.ec2.model.DeleteKeyPairResponse; import software.amazon.awssdk.services.ec2.model.Ec2Exception;

Code

public static void deleteKeys(Ec2Client ec2, String keyPair) { try { DeleteKeyPairRequest request = DeleteKeyPairRequest.builder() .keyName(keyPair) .build(); DeleteKeyPairResponse response = ec2.deleteKeyPair(request);

See the complete example on GitHub.

More information