Open In App

How To Create EC2 Instances Using SDK For JAVA ?

The AWS SDK for Java provides various functionalities to use AWS services using APIs. It provides support for building Java applications easily with the help of the SDK. Using the SDK makes it easier to procure AWS services directly from Java code. Creating and provisioning EC2 instances from Java is possible through the SDK. Let’s see how to perform the creation of EC2 in Java.

Primary Terminologies

Prerequisites

Steps to create an EC2 instance using the SDK for Java

Step 1: Setup the environment



aws configure sso

aws sso login

Step 2: Create project



    <properties>
<aws.java.sdk.version>2.24.5</aws.java.sdk.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>${aws.java.sdk.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.gfg.ec2</groupId>
<artifactId>gfgec2</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<aws.java.sdk.version>2.24.5</aws.java.sdk.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>${aws.java.sdk.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ec2</artifactId>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sso</artifactId>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ssooidc</artifactId>
</dependency>

</dependencies>
</project>

Step 3: Add code for creating instance.

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.InstanceType;
import software.amazon.awssdk.services.ec2.model.RunInstancesRequest;
import software.amazon.awssdk.services.ec2.model.RunInstancesResponse;
import software.amazon.awssdk.services.ec2.model.Tag;
import software.amazon.awssdk.services.ec2.model.CreateTagsRequest;
import software.amazon.awssdk.services.ec2.model.Ec2Exception;

Region region = Region.AP_SOUTH_1;
Ec2Client ec2 = Ec2Client.builder()
.region(region)
.build();

RunInstancesRequest runRequest = RunInstancesRequest.builder()
.imageId(amiId)
.instanceType(InstanceType.T2_MICRO)
.maxCount(1)
.minCount(1)
.build();
RunInstancesResponse response = ec2.runInstances(runRequest);

Tag tag = Tag.builder()
.key("Name")
.value(name)
.build();
CreateTagsRequest tagRequest = CreateTagsRequest.builder()
.resources(instanceId)
.tags(tag)
.build();

package com.gfg.ec2;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.InstanceType;
import software.amazon.awssdk.services.ec2.model.RunInstancesRequest;
import software.amazon.awssdk.services.ec2.model.RunInstancesResponse;
import software.amazon.awssdk.services.ec2.model.Tag;
import software.amazon.awssdk.services.ec2.model.CreateTagsRequest;
import software.amazon.awssdk.services.ec2.model.Ec2Exception;

public class Main {
public static void main(String[] args) {
String name = "gfginstance";
String amiId = "ami-0449c34f967dbf18a";
Region region = Region.AP_SOUTH_1;
Ec2Client ec2 = Ec2Client.builder()
.region(region)
.build();

String instanceId = createEC2Instance(ec2, name, amiId);
System.out.println("The Instance ID is " + instanceId);
ec2.close();
}

public static String createEC2Instance(Ec2Client ec2, String name, String amiId) {
RunInstancesRequest runRequest = RunInstancesRequest.builder()
.imageId(amiId)
.instanceType(InstanceType.T2_MICRO)
.maxCount(1)
.minCount(1)
.build();

RunInstancesResponse response = ec2.runInstances(runRequest);
String instanceId = response.instances().get(0).instanceId();
Tag tag = Tag.builder()
.key("Name")
.value(name)
.build();

CreateTagsRequest tagRequest = CreateTagsRequest.builder()
.resources(instanceId)
.tags(tag)
.build();

try {
ec2.createTags(tagRequest);
System.out.println("Successfully started EC2 Instance based on AMI "+amiId);
return instanceId;

} catch (Ec2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}

return "";
}
}

Conclusion

Thus, we have created a EC2 instance using AWS SDK for JAVA. AWS SDK makes it easy to procure EC2 instances directly from java code. AWS SDK can be further explored for more featureful EC2 configurations and deployments.

How to Create EC2 Instances using SDK for JAVA – FAQ’s

What is the AWS SDK for Java?

The AWS SDK for Java is a set of tools and libraries for Java developers to interact with Amazon Web Services (AWS) programmatically, allowing seamless integration with AWS services, including EC2.

What are the prerequisites for creating EC2 instances using Java SDK?

You need an AWS account, AWS access key ID, and secret access key. Ensure your IAM roles have the necessary permissions for EC2 actions.

Can I create EC2 instances with different configurations using the Java SDK?

Yes, the Java SDK allows you to create EC2 instances with various configurations, including specifying instance types, AMIs, security groups, IAM roles, key pairs, and more.

What are some best practices for using the AWS SDK for Java when working with EC2 instances?

Follow best practices for secure and efficient code. Ensure proper error handling, use asynchronous operations where needed, and organize your code for readability and maintainability.


Article Tags :