use of com.amazonaws.services.ec2.model.KeyPair in project deeplearning4j by deeplearning4j.
the class Ec2BoxCreator method create.
/**
* Create the instances
*/
public void create() {
RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId(amiId).withInstanceType(size).withKeyName(keyPair).withMinCount(1).withSecurityGroupIds(securityGroupId).withMaxCount(numBoxes);
AmazonEC2 ec2 = getEc2();
ec2.setRegion(com.amazonaws.regions.Region.getRegion(regions));
List<Instance> boxes = ec2.runInstances(runInstancesRequest).getReservation().getInstances();
if (boxesCreated == null) {
boxesCreated = new ArrayList<>();
for (Instance i : boxes) boxesCreated.add(i.getInstanceId());
log.info("Boxes created " + boxesCreated);
} else {
blowupBoxes();
boxesCreated.clear();
for (Instance i : boxes) boxesCreated.add(i.getInstanceId());
}
}
use of com.amazonaws.services.ec2.model.KeyPair 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.KeyPair in project camel by apache.
the class EC2ProducerTest method ec2CreateAndRunTestWithKeyPair.
@Test
public void ec2CreateAndRunTestWithKeyPair() throws Exception {
mock.expectedMessageCount(1);
Exchange exchange = template.request("direct:createAndRun", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(EC2Constants.OPERATION, EC2Operations.createAndRunInstances);
exchange.getIn().setHeader(EC2Constants.IMAGE_ID, "test-1");
exchange.getIn().setHeader(EC2Constants.INSTANCE_TYPE, InstanceType.T2Micro);
exchange.getIn().setHeader(EC2Constants.INSTANCE_MIN_COUNT, 1);
exchange.getIn().setHeader(EC2Constants.INSTANCE_MAX_COUNT, 1);
exchange.getIn().setHeader(EC2Constants.INSTANCES_KEY_PAIR, "keypair-1");
}
});
assertMockEndpointsSatisfied();
RunInstancesResult resultGet = (RunInstancesResult) exchange.getIn().getBody();
assertEquals(resultGet.getReservation().getInstances().get(0).getImageId(), "test-1");
assertEquals(resultGet.getReservation().getInstances().get(0).getInstanceType(), InstanceType.T2Micro.toString());
assertEquals(resultGet.getReservation().getInstances().get(0).getInstanceId(), "instance-1");
assertEquals(resultGet.getReservation().getInstances().get(0).getSecurityGroups().size(), 2);
assertEquals(resultGet.getReservation().getInstances().get(0).getSecurityGroups().get(0).getGroupId(), "id-3");
assertEquals(resultGet.getReservation().getInstances().get(0).getSecurityGroups().get(1).getGroupId(), "id-4");
}
use of com.amazonaws.services.ec2.model.KeyPair in project aws-cf-templates by widdix.
the class TestVPCSshBastion method test.
@Test
public void test() {
final String vpcStackName = "vpc-2azs-" + this.random8String();
final String bastionStackName = "vpc-ssh-bastion-" + this.random8String();
final String classB = "10";
final String keyName = "key-" + this.random8String();
try {
final KeyPair key = this.createKey(keyName);
try {
this.createStack(vpcStackName, "vpc/vpc-2azs.yaml", new Parameter().withParameterKey("ClassB").withParameterValue(classB));
try {
this.createStack(bastionStackName, "vpc/vpc-ssh-bastion.yaml", new Parameter().withParameterKey("ParentVPCStack").withParameterValue(vpcStackName), new Parameter().withParameterKey("KeyName").withParameterValue(keyName));
final String host = this.getStackOutputValue(bastionStackName, "IPAddress");
this.probeSSH(host, key);
} finally {
this.deleteStack(bastionStackName);
}
} finally {
this.deleteStack(vpcStackName);
}
} finally {
this.deleteKey(keyName);
}
}
use of com.amazonaws.services.ec2.model.KeyPair in project aws-cf-templates by widdix.
the class TestEC2AutoRecovery method test.
@Test
public void test() {
final String vpcStackName = "vpc-2azs-" + this.random8String();
final String stackName = "ec2-auto-recovery-" + this.random8String();
final String classB = "10";
final String keyName = "key-" + this.random8String();
try {
final KeyPair key = this.createKey(keyName);
try {
this.createStack(vpcStackName, "vpc/vpc-2azs.yaml", new Parameter().withParameterKey("ClassB").withParameterValue(classB));
try {
this.createStack(stackName, "ec2/ec2-auto-recovery.yaml", new Parameter().withParameterKey("ParentVPCStack").withParameterValue(vpcStackName), new Parameter().withParameterKey("KeyName").withParameterValue(keyName));
final String host = this.getStackOutputValue(stackName, "IPAddress");
this.probeSSH(host, key);
} finally {
this.deleteStack(stackName);
}
} finally {
this.deleteStack(vpcStackName);
}
} finally {
this.deleteKey(keyName);
}
}
Aggregations