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
6,290 views

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;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.CreateTagsRequest;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.InstanceNetworkInterfaceSpecification;
import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.ec2.model.RunInstancesResult;
import com.amazonaws.services.ec2.model.StartInstancesRequest;
import com.amazonaws.services.ec2.model.Tag;
public class EC2CreateInstance
{
    private static final AWSCredentials AWS_CREDENTIALS;
    
    static {
        // Your accesskey and secretkey
        AWS_CREDENTIALS = new BasicAWSCredentials(
                "Your-ID",
                "Your secret-key"
        );
    }
    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();
         
        // Launch an Amazon EC2 Instance
        RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId("ami-0080e4c5bc078760e")
                .withInstanceType("t2.micro") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html
                .withMinCount(1)
                .withMaxCount(1)
                .withKeyName("priyajdm")
                .withNetworkInterfaces(new InstanceNetworkInterfaceSpecification()
                        .withAssociatePublicIpAddress(true)
                        .withDeviceIndex(0)
                        .withSubnetId("subnet-id")
                        .withGroups("sg-id"));
 
        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()
                .withResources(instance.getInstanceId())
                .withTags(new Tag("Name", "Edurekademo"));
        ec2Client.createTags(createTagsRequest);
 
        // Starting the Instance
        StartInstancesRequest startInstancesRequest = new StartInstancesRequest().withInstanceIds(instanceId);
 
        ec2Client.startInstances(startInstancesRequest);

    }
}

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

answered Jan 22, 2019 by Priyaj
• 58,090 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.

Eg:-  https://www.edureka.co/community/32006/how-to-create-an-ec2-instance-using-python-boto3?show=32006#q32006

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,090 points
1,383 views
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
3,856 views
+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:- https://docs.aws.amazon.com/cli/latest/ ...READ MORE

answered Feb 15, 2019 in AWS by Dev
• 360 points
6,897 views
+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,090 points
15,005 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