Launching an EC2 Instance using AWS SDK Java

0 votes
I was trying to create a new EC2 Instance in Eclipse Java using AWS SDK. Can someone provide the code for doing that?
Jan 22 in AWS by Anjali
• 2,900 points

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
Hey @Anjali, all you need to do is create a AWS Java Project.

1. Define your security credentials.

2. Setup an Amazon EC2 Client.

3. Launch an EC2 instance

4. Set tags for the EC2 Instance

5. Start the Instance.
answered Jan 22 by Shubham
0 votes
package com.ec2application.ec2application;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
public class EC2CreateInstance
    private static final AWSCredentials AWS_CREDENTIALS;
    static {
        // Your accesskey and secretkey
        AWS_CREDENTIALS = new BasicAWSCredentials(
                "Your secret-key"
    public static void main(String[] args) {
        // Set up the amazon ec2 client
        AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(AWS_CREDENTIALS))
        // Launch an Amazon EC2 Instance
        RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId("ami-0080e4c5bc078760e")
                .withInstanceType("t2.micro") //
                .withNetworkInterfaces(new InstanceNetworkInterfaceSpecification()
        RunInstancesResult runInstancesResult = ec2Client.runInstances(runInstancesRequest);
        Instance instance = runInstancesResult.getReservation().getInstances().get(0);
        String instanceId = instance.getInstanceId();
        System.out.println("EC2 Instance Id: " + instanceId);
        // Setting up the tags for the instance
        CreateTagsRequest createTagsRequest = new CreateTagsRequest()
                .withTags(new Tag("Name", "Edurekademo"));
        // Starting the Instance
        StartInstancesRequest startInstancesRequest = new StartInstancesRequest().withInstanceIds(instanceId);


This is the code you can use to Create an EC2 Instance using AWS SDK.

answered Jan 22 by Priyaj
• 56,140 points
Thank you. It worked fine.
@Shashank I am glad it did.
Is working on AWS services easy on python or java??
Hey @Shashank, working with Python is much easier as doing the same thing in Python will take less lines of code and also make things easier.


Try the one in Python and you can see the difference.

Related Questions In AWS

0 votes
1 answer

Stopping EC2 Instance using Java AWS SDK

You can follow the following steps:- 1. Create ...READ MORE

answered Jan 22 in AWS by Priyaj
• 56,140 points
0 votes
1 answer
0 votes
1 answer

Connecting AWS EC2 instance using Java

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

answered Mar 28 in AWS by Disha
+1 vote
1 answer
0 votes
1 answer
+1 vote
2 answers

Create an EC2 Instance using AWS CLI

You can get the command here:- ...READ MORE

answered Feb 15 in AWS by Dev
• 360 points
+1 vote
2 answers

AWS EC2 - Can't launch an instance - Account blocked

Try launching the Instance using CLI or ...READ MORE

answered Aug 7, 2018 in AWS by Priyaj
• 56,140 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.