Stopping EC2 Instance using Java AWS SDK

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

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

1 answer to this question.

0 votes

You can follow the following steps:-

1. Create your credential files

2. Set up Amazon EC2 Client

3. Use the ID of the instance you want to stop

4. Stop 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.StopInstancesRequest;

public class EC2Stop
{
    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();
        
        //Stop EC2 Instance
        String instanecID = "i-0739c0ea4202ef40f";
        StopInstancesRequest stopInstancesRequest = new StopInstancesRequest()
                .withInstanceIds(instanecID);
         
        ec2Client.stopInstances(stopInstancesRequest)
                .getStoppingInstances()
                .get(0)
                .getPreviousState()
                .getName();
        System.out.println("Stopped the Instnace with ID: "+instanecID);
    }

}

Hope this helps.

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

Related Questions In AWS

0 votes
1 answer

Connecting AWS EC2 instance using Java

An Amazon EC2 instance is just like ...READ MORE

answered Mar 28, 2019 in AWS by Disha
4,282 views
0 votes
0 answers

AWS Java SDK - Get EC2 instance info

I have done some research (i.e. looking ...READ MORE

Apr 25, 2022 in AWS by Aditya
• 7,680 points
1,214 views
+1 vote
1 answer

AWS billing information using AWS java sdk

There are no APIs to get AWS ...READ MORE

answered Jul 26, 2018 in AWS by Priyaj
• 58,020 points
799 views
0 votes
0 answers

Create EC2 instance using Lambda function with Java

I have tried a lot of thing ...READ MORE

Jan 21, 2019 in AWS by Rishab
• 1,490 points
2,122 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,020 points
6,886 views
0 votes
1 answer
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