Search in sources :

Example 1 with CreateKeyPairResult

use of com.amazonaws.services.ec2.model.CreateKeyPairResult 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 2 with CreateKeyPairResult

use of com.amazonaws.services.ec2.model.CreateKeyPairResult 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;
    }
}
Also used : KeyPairInfo(com.amazonaws.services.ec2.model.KeyPairInfo) CreateKeyPairResult(com.amazonaws.services.ec2.model.CreateKeyPairResult) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) CreateKeyPairRequest(com.amazonaws.services.ec2.model.CreateKeyPairRequest) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 3 with CreateKeyPairResult

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

Example 4 with CreateKeyPairResult

use of com.amazonaws.services.ec2.model.CreateKeyPairResult 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;
}
Also used : KeyPair(com.amazonaws.services.ec2.model.KeyPair) CreateKeyPairResult(com.amazonaws.services.ec2.model.CreateKeyPairResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) CreateKeyPairRequest(com.amazonaws.services.ec2.model.CreateKeyPairRequest)

Example 5 with CreateKeyPairResult

use of com.amazonaws.services.ec2.model.CreateKeyPairResult in project tutorials by eugenp.

the class EC2Application method main.

public static void main(String[] args) {
    // Set up the client
    AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion(Regions.US_EAST_1).build();
    // Create a security group
    CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest().withGroupName("BaeldungSecurityGroup").withDescription("Baeldung Security Group");
    ec2Client.createSecurityGroup(createSecurityGroupRequest);
    // Allow HTTP and SSH traffic
    IpRange ipRange1 = new IpRange().withCidrIp("0.0.0.0/0");
    IpPermission ipPermission1 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 })).withIpProtocol("tcp").withFromPort(80).withToPort(80);
    IpPermission ipPermission2 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 })).withIpProtocol("tcp").withFromPort(22).withToPort(22);
    AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest().withGroupName("BaeldungSecurityGroup").withIpPermissions(ipPermission1, ipPermission2);
    ec2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);
    // Create KeyPair
    CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest().withKeyName("baeldung-key-pair");
    CreateKeyPairResult createKeyPairResult = ec2Client.createKeyPair(createKeyPairRequest);
    String privateKey = createKeyPairResult.getKeyPair().getKeyMaterial();
    // See what key-pairs you've got
    DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
    DescribeKeyPairsResult describeKeyPairsResult = ec2Client.describeKeyPairs(describeKeyPairsRequest);
    // Launch an Amazon Instance
    RunInstancesRequest runInstancesRequest = // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html | https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/usingsharedamis-finding.html
    new RunInstancesRequest().withImageId("ami-97785bed").withInstanceType(// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html
    "t2.micro").withMinCount(1).withMaxCount(1).withKeyName(// optional - if not present, can't connect to instance
    "baeldung-key-pair").withSecurityGroups("BaeldungSecurityGroup");
    String yourInstanceId = ec2Client.runInstances(runInstancesRequest).getReservation().getInstances().get(0).getInstanceId();
    // Start an Instance
    StartInstancesRequest startInstancesRequest = new StartInstancesRequest().withInstanceIds(yourInstanceId);
    ec2Client.startInstances(startInstancesRequest);
    // Monitor Instances
    MonitorInstancesRequest monitorInstancesRequest = new MonitorInstancesRequest().withInstanceIds(yourInstanceId);
    ec2Client.monitorInstances(monitorInstancesRequest);
    UnmonitorInstancesRequest unmonitorInstancesRequest = new UnmonitorInstancesRequest().withInstanceIds(yourInstanceId);
    ec2Client.unmonitorInstances(unmonitorInstancesRequest);
    // Reboot an Instance
    RebootInstancesRequest rebootInstancesRequest = new RebootInstancesRequest().withInstanceIds(yourInstanceId);
    ec2Client.rebootInstances(rebootInstancesRequest);
    // Stop an Instance
    StopInstancesRequest stopInstancesRequest = new StopInstancesRequest().withInstanceIds(yourInstanceId);
    ec2Client.stopInstances(stopInstancesRequest).getStoppingInstances().get(0).getPreviousState().getName();
    // Describe an Instance
    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
    DescribeInstancesResult response = ec2Client.describeInstances(describeInstancesRequest);
    System.out.println(response.getReservations().get(0).getInstances().get(0).getKernelId());
}
Also used : IpRange(com.amazonaws.services.ec2.model.IpRange) DescribeKeyPairsRequest(com.amazonaws.services.ec2.model.DescribeKeyPairsRequest) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) DescribeKeyPairsResult(com.amazonaws.services.ec2.model.DescribeKeyPairsResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) CreateSecurityGroupRequest(com.amazonaws.services.ec2.model.CreateSecurityGroupRequest) CreateKeyPairRequest(com.amazonaws.services.ec2.model.CreateKeyPairRequest) MonitorInstancesRequest(com.amazonaws.services.ec2.model.MonitorInstancesRequest) RebootInstancesRequest(com.amazonaws.services.ec2.model.RebootInstancesRequest) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) CreateKeyPairResult(com.amazonaws.services.ec2.model.CreateKeyPairResult) AuthorizeSecurityGroupIngressRequest(com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) IpPermission(com.amazonaws.services.ec2.model.IpPermission) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) UnmonitorInstancesRequest(com.amazonaws.services.ec2.model.UnmonitorInstancesRequest)

Aggregations

CreateKeyPairRequest (com.amazonaws.services.ec2.model.CreateKeyPairRequest)5 CreateKeyPairResult (com.amazonaws.services.ec2.model.CreateKeyPairResult)5 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)3 KeyPair (com.amazonaws.services.ec2.model.KeyPair)2 File (java.io.File)2 IOException (java.io.IOException)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 DescribeKeyPairsResult (com.amazonaws.services.ec2.model.DescribeKeyPairsResult)1 IpPermission (com.amazonaws.services.ec2.model.IpPermission)1 IpRange (com.amazonaws.services.ec2.model.IpRange)1 KeyPairInfo (com.amazonaws.services.ec2.model.KeyPairInfo)1 MonitorInstancesRequest (com.amazonaws.services.ec2.model.MonitorInstancesRequest)1 RebootInstancesRequest (com.amazonaws.services.ec2.model.RebootInstancesRequest)1 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)1 StartInstancesRequest (com.amazonaws.services.ec2.model.StartInstancesRequest)1