Terminating an EC2 Intsance in Java

0 votes
Can someone help me with the code for terminating an EC2 instance using Java AWS SDK?

Thank you in advance.
Jan 22, 2019 in AWS by Anjali
• 2,950 points
1,799 views

1 answer to this question.

0 votes

You can terminate the EC2 Instance in few steps using Java, all you need to know is your instance Id.

1. Create your credentials

2. Setup an Amazon EC2 Client

3. Terminate the EC2 Instance.

Here is the code for doing that:-

package com.ec2application.ec2application;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.TerminateInstancesRequest;

public class EC2Terminate
{
    private static final AWSCredentials AWS_CREDENTIALS;
    
    static {
        // Your accesskey and secretkey
        AWS_CREDENTIALS = new BasicAWSCredentials(
                "id",
                "secretkey"
        );
    }
    public static void main(String[] args) {
        
        // Set up the amazon ec2 client
        AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(AWS_CREDENTIALS))
                .withRegion(Regions.US_EAST_1)
                .build();
        
        //Terminate EC2 Instance
        String instanecID = "i-0739c0ea4202ef40f";
        TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest()
                .withInstanceIds(instanecID);
        ec2Client.terminateInstances(terminateInstancesRequest)
                .getTerminatingInstances()
                .get(0)
                .getPreviousState()
                .getName();
        System.out.println("The Instance is terminated with id: "+instanecID);
                       
    }

}

Hope this will help. 

answered Jan 22, 2019 by Priyaj
• 58,090 points

Related Questions In AWS

0 votes
1 answer
0 votes
1 answer

Unable to send email from Amazon EC2 Server in Java

It mostly could be due to two ...READ MORE

answered Nov 30, 2018 in AWS by Archana
• 5,640 points
1,115 views
+1 vote
2 answers

How to create an EC2 instance in AWS Console?

Here is a step by step guide ...READ MORE

answered Feb 11, 2019 in AWS by Shubendu
1,695 views
0 votes
1 answer
0 votes
1 answer

Using Shapely on AWS Lambda with Python 3

For some reason, the pip install of ...READ MORE

answered Oct 8, 2018 in AWS by Priyaj
• 58,090 points
2,593 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

Launching an EC2 Instance using AWS SDK Java

package com.ec2application.ec2application; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.Regions; import com.amazonaws.services.ec2.AmazonEC2; import ...READ MORE

answered Jan 22, 2019 in AWS by Priyaj
• 58,090 points
6,329 views
0 votes
2 answers

How to create an EC2 Snapshot in AWS Console?

To create a snapshot of your EC2 ...READ MORE

answered Feb 11, 2019 in AWS by Priyaj
• 58,090 points
918 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP