Home > Cloud Computing, Software Development > Amazon Web Service Java SDK Tutorial: Create New Virtual Machine

Amazon Web Service Java SDK Tutorial: Create New Virtual Machine

In my previous post, I showed a sample on how to list virtual machine instances. While that is helpful, maybe even more so is to create a new virtual machine. Here comes another sample that creates new virtual machine instance using the Amazon Java SDK.

Instead of including the access key and secret key in the source, we change it to read them from a property file. Note that we use the getResourceAsStream() method defined in the Class class because it continues to work after you package everything into a jar file. Location wise, you can place the AwsCredentials.properties file into the src/main/resources directory from the project home if you use Maven project structure as we do here. We will list the content format in the properties file after the source code.

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure


The AMI we use is ami-2051294a, which is RHEL-7.2 64 bit provided by RedHat Inc. To create your own virtual machine, you can change it to whatever AMI. For the other variables you can probably keep them except the key name. For me, I have created a key pair named SteveKeyForAws using AWS console. You most likely have a different key name – just copy/paste it.

package org.doublecloud.awssample;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.Placement;
import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.ec2.model.RunInstancesResult;
public class AwsEc2NewInstanceDemo
  public static void main(String[] args) throws Exception
    AWSCredentials credentials = new PropertiesCredentials(AwsEc2NewInstanceDemo.class.getResourceAsStream("/AwsCredentials.properties"));
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);
    RunInstancesRequest request = new RunInstancesRequest();
    Placement p = new Placement();
    RunInstancesResult runInstancesRes = ec2.runInstances(request);
    String reservationId = runInstancesRes.getReservation().getReservationId();
    System.out.println("reservation ID of the executed transation: "+ reservationId);

Here is the pom.xml file:

<?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">

Here is the AwsCredentials.properties file with changed value. You must replace the values from your own account. For details how to generate access from AWS console, check out this post.


Please note that if you select “Download Key File” you want to change the key name to above sample from “AWSAccessKeyId” and “AWSSecretKey” in the downloaded file.

  1. No comments yet.
  1. No trackbacks yet.