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.

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;
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();
    request.setInstanceType("t2.micro");
    request.setMinCount(1);
    request.setMaxCount(1);
    Placement p = new Placement();
    p.setAvailabilityZone("us-east-1d");
    request.setPlacement(p);
    request.setImageId("ami-2051294a");
    request.setKeyName("SteveKeyForAws");
 
    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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.doublecloud</groupId>
    <artifactId>AwsEc2HelloWorld</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-ec2</artifactId>
            <version>1.10.50</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
</project>

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.

accessKey=AKIATBD6D32VUVKZAJX5
secretKey=zlDyupqRc1q1TzaMctc2MLBCLxGhChKzk0oB7qls

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.