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, 2019 in AWS by Anjali
• 2,950 points

2 answers to this question.

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, 2019 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, 2019 by Priyaj
• 58,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.
Can anyone please also share the code to access an app deployed on EC2 instance? I have a java app deployed on EC2 instance. I need a java code which accesses the app deployed on this EC2 instance. The app returns a string. I need to get that string in my java code residing on my local machine.

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, 2019 in AWS by Priyaj
• 58,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, 2019 in AWS by Disha
+1 vote
1 answer
0 votes
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, 2019 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
• 58,140 points