com.amazonaws.services.ec2.model.CreateKeyPairResult Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.CreateKeyPairResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CreateKeyPair.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #2
Source File: AWSSdkClient.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/***
 * 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;
}
 
Example #3
Source File: EC2Impl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public KeyPair createKeyPair(CreateKeyPairRequest request,
        ResultCapture<CreateKeyPairResult> extractor) {

    ActionResult result = service.performAction("CreateKeyPair", request,
            extractor);

    if (result == null) return null;
    return new KeyPairImpl(result.getResource());
}
 
Example #4
Source File: EC2Impl.java    From aws-sdk-java-resources with Apache License 2.0 5 votes vote down vote up
@Override
public KeyPair createKeyPair(String keyName,
        ResultCapture<CreateKeyPairResult> extractor) {

    CreateKeyPairRequest request = new CreateKeyPairRequest()
        .withKeyName(keyName);
    return createKeyPair(request, extractor);
}
 
Example #5
Source File: SimpleKeyPairService.java    From sequenceiq-samples with Apache License 2.0 4 votes vote down vote up
public CreateKeyPairResult createKeyPair(AmazonEC2Client client, String keyName) {
	CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest().withKeyName(keyName);
	CreateKeyPairResult createKeyPairResult = client.createKeyPair(createKeyPairRequest);
	return createKeyPairResult;
}
 
Example #6
Source File: SimpleKeyPairService.java    From sequenceiq-samples with Apache License 2.0 4 votes vote down vote up
@Override
public CreateKeyPairResult createKeyPair(AWSCredentials credentials, String keyName) {
	return createKeyPair(amazonEC2ClientFactory.createAmazonEC2Client(credentials), keyName);
}
 
Example #7
Source File: KeyPairsConroller.java    From sequenceiq-samples with Apache License 2.0 4 votes vote down vote up
@RequestMapping(method = RequestMethod.GET, value = {"/keypairs/create"})
@ResponseBody
public CreateKeyPairResult createKeyPairs(@RequestParam("name") String name, @RequestParam("accessKey") String accessKey,
        @RequestParam("secretKey") String secretKey) {
	return awsec2Service.createKeyPair(awsCredentialsFactory.createSimpleAWSCredentials(accessKey, secretKey), name);
}
 
Example #8
Source File: EC2Impl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public KeyPair createKeyPair(String keyName) {
    return createKeyPair(keyName, (ResultCapture<CreateKeyPairResult>)null);
}
 
Example #9
Source File: EC2Application.java    From tutorials with MIT License 4 votes vote down vote up
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(); // make sure you keep it, the private key, Amazon doesn't store the private key

    // See what key-pairs you've got
    DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
    DescribeKeyPairsResult describeKeyPairsResult = ec2Client.describeKeyPairs(describeKeyPairsRequest);

    // Launch an Amazon Instance
    RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId("ami-97785bed") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html | https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/usingsharedamis-finding.html
        .withInstanceType("t2.micro") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html
        .withMinCount(1)
        .withMaxCount(1)
        .withKeyName("baeldung-key-pair") // optional - if not present, can't connect to instance
        .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());
}
 
Example #10
Source File: EC2.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Performs the <code>CreateKeyPair</code> action and use a ResultCapture to
 * retrieve the low-level client response.
 *
 * <p>
 *
 * @return The <code>KeyPair</code> resource object associated with the
 *         result of this action.
 * @see CreateKeyPairRequest
 */
com.amazonaws.resources.ec2.KeyPair createKeyPair(CreateKeyPairRequest
        request, ResultCapture<CreateKeyPairResult> extractor);
 
Example #11
Source File: EC2.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * The convenient method form for the <code>CreateKeyPair</code> action.
 *
 * @see #createKeyPair(CreateKeyPairRequest, ResultCapture)
 */
com.amazonaws.resources.ec2.KeyPair createKeyPair(String keyName,
        ResultCapture<CreateKeyPairResult> extractor);
 
Example #12
Source File: KeyPairService.java    From sequenceiq-samples with Apache License 2.0 votes vote down vote up
CreateKeyPairResult createKeyPair(AWSCredentials credentials, String keyName);