简体   繁体   中英

create a file.pem containing the private key when creating a Key Pair using AWS SDK for Java

I am trying to create Key Pairs using the AWS SDK for Java. Usually if you are using the Management Console on AWS website and when you create a new Key Pair, you are provided with a YourKeypairName.pem file containing the private RSA key that you have to use to access an EC2 instance.

However when creating a Key Pair using AWS SDK for Java, I can manage to get a String containing the private key but I don't know exactly what to do with it. What I'm wondering is:

  • Is there a way to create.pem files and choose what to put in them in Java?

  • (in a more general way) How am I able to provide a.pem file containing this key to the user of my application in the Frontend.

     try { CreateKeyPairRequest request = CreateKeyPairRequest.builder().keyName("myKeyName").build(); CreateKeyPairResponse response=ec2.createKeyPair(request); System.out.println(response.keyMaterial()); // contains the private key } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); }

If you have the key as String in CDK program, you can simply save it (encrypted) in SSM Parameter store.

Later you can fetch it from any location that has access to your AWS SSM Parameter store. You only need to know the name of the variables.

For example you can read it in your deploy pipeline and save it to a file, or environment variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM