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
255 views

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;
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 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.

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.

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
49 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 in AWS by Disha
52 views
+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:- https://docs.aws.amazon.com/cli/latest/ ...READ MORE

answered Feb 15 in AWS by Dev
• 360 points
264 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
• 56,140 points
680 views

© 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.