Search in sources :

Example 1 with RebootInstancesRequest

use of software.amazon.awssdk.services.ec2.model.RebootInstancesRequest in project aws-doc-sdk-examples by awsdocs.

the class RebootInstance method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply an instance id\n" + "Ex: RebootInstance <instance_id>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String instance_id = args[0];
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    RebootInstancesRequest request = new RebootInstancesRequest().withInstanceIds(instance_id);
    RebootInstancesResult response = ec2.rebootInstances(request);
    System.out.printf("Successfully rebooted instance %s", instance_id);
}
Also used : RebootInstancesRequest(com.amazonaws.services.ec2.model.RebootInstancesRequest) RebootInstancesResult(com.amazonaws.services.ec2.model.RebootInstancesResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Example 2 with RebootInstancesRequest

use of software.amazon.awssdk.services.ec2.model.RebootInstancesRequest in project camel by apache.

the class EC2Producer method rebootInstances.

private void rebootInstances(AmazonEC2Client ec2Client, Exchange exchange) {
    Collection instanceIds;
    RebootInstancesRequest request = new RebootInstancesRequest();
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
        instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
        request.withInstanceIds(instanceIds);
    } else {
        throw new IllegalArgumentException("Instances Ids must be specified");
    }
    try {
        LOG.trace("Rebooting instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
        ec2Client.rebootInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Reboot Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
}
Also used : RebootInstancesRequest(com.amazonaws.services.ec2.model.RebootInstancesRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection)

Example 3 with RebootInstancesRequest

use of software.amazon.awssdk.services.ec2.model.RebootInstancesRequest in project photon-model by vmware.

the class AWSRebootService method reboot.

private void reboot(AmazonEC2AsyncClient client, ResourceOperationRequest pr, DefaultAdapterContext c) {
    if (!c.child.powerState.equals(ComputeService.PowerState.ON)) {
        logInfo("Cannot Reboot an EC2 instance in powered off state." + "The machine should be powered on first");
        c.taskManager.patchTaskToFailure(new IllegalStateException("Incorrect power state. Expected the machine " + "to be powered on "));
        return;
    }
    RebootInstancesRequest request = new RebootInstancesRequest();
    request.withInstanceIds(c.child.id);
    client.rebootInstancesAsync(request, new AsyncHandler<RebootInstancesRequest, RebootInstancesResult>() {

        @Override
        public void onSuccess(RebootInstancesRequest request, RebootInstancesResult result) {
            c.taskManager.finishTask();
        }

        @Override
        public void onError(Exception e) {
            c.taskManager.patchTaskToFailure(e);
        }
    });
}
Also used : RebootInstancesRequest(com.amazonaws.services.ec2.model.RebootInstancesRequest) RebootInstancesResult(com.amazonaws.services.ec2.model.RebootInstancesResult)

Example 4 with RebootInstancesRequest

use of software.amazon.awssdk.services.ec2.model.RebootInstancesRequest in project aws-doc-sdk-examples by awsdocs.

the class RebootInstance method rebootEC2Instance.

// snippet-start:[ec2.java2.reboot_instance.main]
public static void rebootEC2Instance(Ec2Client ec2, String instanceId) {
    try {
        RebootInstancesRequest request = RebootInstancesRequest.builder().instanceIds(instanceId).build();
        ec2.rebootInstances(request);
        System.out.printf("Successfully rebooted instance %s", instanceId);
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : RebootInstancesRequest(software.amazon.awssdk.services.ec2.model.RebootInstancesRequest) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception)

Example 5 with RebootInstancesRequest

use of software.amazon.awssdk.services.ec2.model.RebootInstancesRequest 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

RebootInstancesRequest (com.amazonaws.services.ec2.model.RebootInstancesRequest)4 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 RebootInstancesResult (com.amazonaws.services.ec2.model.RebootInstancesResult)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 AuthorizeSecurityGroupIngressRequest (com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest)1 CreateKeyPairRequest (com.amazonaws.services.ec2.model.CreateKeyPairRequest)1 CreateKeyPairResult (com.amazonaws.services.ec2.model.CreateKeyPairResult)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 MonitorInstancesRequest (com.amazonaws.services.ec2.model.MonitorInstancesRequest)1 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)1 StartInstancesRequest (com.amazonaws.services.ec2.model.StartInstancesRequest)1 StopInstancesRequest (com.amazonaws.services.ec2.model.StopInstancesRequest)1 UnmonitorInstancesRequest (com.amazonaws.services.ec2.model.UnmonitorInstancesRequest)1