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

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

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;
public class AwsEc2NewInstanceDemo
  public static void main(String[] args) throws Exception
    AWSCredentials credentials = new PropertiesCredentials(AwsEc2NewInstanceDemo.class.getResourceAsStream("/"));
    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="" xmlns:xsi="" xsi:schemaLocation="">

Here is the 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.

This entry was posted in Cloud Computing, Software Development and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.