use of com.amazonaws.services.ec2.model.CreateKeyPairRequest in project GNS by MobilityFirst.
the class AWSEC2 method createKeyPair.
/**
* Create a New Key Pair
*
* @param ec2
* @param name
* @return the keypair
*/
public static KeyPair createKeyPair(AmazonEC2 ec2, String name) {
CreateKeyPairRequest newKeyRequest = new CreateKeyPairRequest();
newKeyRequest.setKeyName(name);
CreateKeyPairResult keyresult = ec2.createKeyPair(newKeyRequest);
/**
* **********************print the properties of this key****************
*/
KeyPair keyPair = keyresult.getKeyPair();
System.out.println("The key we created is = " + keyPair.toString());
/**
* ***************store the key in a .pem file ***************
*/
try {
String fileName = KEYHOME + FILESEPARATOR + name + PRIVATEKEYFILEEXTENSION;
File distFile = new File(fileName);
BufferedReader bufferedReader = new BufferedReader(new StringReader(keyPair.getKeyMaterial()));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(distFile));
char[] buf = new char[1024];
int len;
while ((len = bufferedReader.read(buf)) != -1) {
bufferedWriter.write(buf, 0, len);
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
return keyPair;
}
use of com.amazonaws.services.ec2.model.CreateKeyPairRequest in project Synapse-Stack-Builder by Sage-Bionetworks.
the class EC2SecuritySetup method createOrGetKeyPair.
/**
* Create the key par
* @return
*/
public KeyPairInfo createOrGetKeyPair() {
String name = config.getStackKeyPairName();
KeyPairInfo info = describeKeyPair();
if (info == null) {
log.debug("Creating the Stack KeyPair: " + name + " for the first time");
CreateKeyPairResult kpResult = ec2Client.createKeyPair(new CreateKeyPairRequest(name));
File temp = null;
FileOutputStream fos = null;
try {
temp = File.createTempFile("Temp", ".tmp");
fos = new FileOutputStream(temp);
// Write the material to the file.
fos.write(kpResult.getKeyPair().getKeyMaterial().getBytes("UTF-8"));
fos.close();
// Now write the file to S3
s3Client.putObject(new PutObjectRequest(config.getStackConfigS3BucketName(), config.getStackKeyPairS3File(), temp));
} catch (IOException e) {
// convert to runtime
throw new RuntimeException(e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
if (temp != null) {
temp.delete();
}
}
return describeKeyPair();
} else {
log.debug("Stack KeyPair: " + name + " already exists");
return info;
}
}
use of com.amazonaws.services.ec2.model.CreateKeyPairRequest in project aws-doc-sdk-examples by awsdocs.
the class CreateKeyPair method main.
public static void main(String[] args) {
final String USAGE = "To run this example, supply a key pair name\n" + "Ex: CreateKeyPair <key-pair-name>\n";
if (args.length != 1) {
System.out.println(USAGE);
System.exit(1);
}
String key_name = args[0];
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
CreateKeyPairRequest request = new CreateKeyPairRequest().withKeyName(key_name);
CreateKeyPairResult response = ec2.createKeyPair(request);
System.out.printf("Successfully created key pair named %s", key_name);
}
use of com.amazonaws.services.ec2.model.CreateKeyPairRequest in project aws-doc-sdk-examples by awsdocs.
the class CreateKeyPair method createEC2KeyPair.
// snippet-start:[ec2.java2.create_key_pair.main]
public static void createEC2KeyPair(Ec2Client ec2, String keyName) {
try {
CreateKeyPairRequest request = CreateKeyPairRequest.builder().keyName(keyName).build();
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);
}
}
use of com.amazonaws.services.ec2.model.CreateKeyPairRequest in project incubator-gobblin by apache.
the class AWSSdkClient method createKeyValuePair.
/**
* Creates a 2048-bit RSA key pair with the specified name
*
* @param keyName Key name to use
* @return Unencrypted PEM encoded PKCS#8 private key
*/
public String createKeyValuePair(String keyName) {
final AmazonEC2 amazonEC2 = getEc2Client();
final CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest().withKeyName(keyName);
final CreateKeyPairResult createKeyPairResult = amazonEC2.createKeyPair(createKeyPairRequest);
final KeyPair keyPair = createKeyPairResult.getKeyPair();
final String material = keyPair.getKeyMaterial();
LOGGER.info("Created key: " + keyName);
LOGGER.debug("Created material: " + material);
return material;
}
Aggregations