Search in sources :

Example 1 with KeyPair

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());
    }
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Example 2 with KeyPair

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;
}
Also used : KeyPair(com.amazonaws.services.ec2.model.KeyPair) CreateKeyPairResult(com.amazonaws.services.ec2.model.CreateKeyPairResult) FileWriter(java.io.FileWriter) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IOException(java.io.IOException) File(java.io.File) CreateKeyPairRequest(com.amazonaws.services.ec2.model.CreateKeyPairRequest) BufferedWriter(java.io.BufferedWriter)

Example 3 with 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");
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) Test(org.junit.Test)

Example 4 with KeyPair

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);
    }
}
Also used : KeyPair(com.amazonaws.services.ec2.model.KeyPair) Parameter(com.amazonaws.services.cloudformation.model.Parameter) ACloudFormationTest(de.widdix.awscftemplates.ACloudFormationTest) Test(org.junit.Test)

Example 5 with KeyPair

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);
    }
}
Also used : KeyPair(com.amazonaws.services.ec2.model.KeyPair) Parameter(com.amazonaws.services.cloudformation.model.Parameter) ACloudFormationTest(de.widdix.awscftemplates.ACloudFormationTest) Test(org.junit.Test)

Aggregations

CreateKeyPairRequest (com.amazonaws.services.ec2.model.CreateKeyPairRequest)4 CreateKeyPairResult (com.amazonaws.services.ec2.model.CreateKeyPairResult)4 KeyPair (com.amazonaws.services.ec2.model.KeyPair)4 Test (org.junit.Test)4 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)3 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)3 File (java.io.File)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 Parameter (com.amazonaws.services.cloudformation.model.Parameter)2 Instance (com.amazonaws.services.ec2.model.Instance)2 ACloudFormationTest (de.widdix.awscftemplates.ACloudFormationTest)2 IOException (java.io.IOException)2 Exchange (org.apache.camel.Exchange)2 Processor (org.apache.camel.Processor)2 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 AuthorizeSecurityGroupIngressRequest (com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest)1 CreateSecurityGroupRequest (com.amazonaws.services.ec2.model.CreateSecurityGroupRequest)1 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)1 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)1 DescribeKeyPairsRequest (com.amazonaws.services.ec2.model.DescribeKeyPairsRequest)1