简体   繁体   中英

How to launch a Virtual Machine in EC2 using Java

I am totally new to AWS. I have been given a official task to explore the AWS api for java So that we can launch a Virtual Machine ON EC2. I have gone through the official docs at sdk-for-java but having few doubts:

  1. Would I need to deploy the java code on EC2 ?
  2. Can Java code create and launch the Virtual Server or we can only start it through code and need to create the virtual machine through EC2 dashboard?

Thanks

First off, since you're very unfamiliar with AWS, have a read through their documentation. I know there's a lot there but the following links should directly apply to your situation:

Concepts
Getting started with AWS SDK for Java
Java examples

To answer your question, you want to start a virtual machine. Within AWS, these are referred to as "instances". To launch an instance using the API, you could execute the code that makes the API call locally and as long as you have valid credentials etc, the code should work. Your code does not need to be run inside of EC2.

The following code will run an EC2 instance for you as long as you have valid credentials and the AWS-SDK included in your java project.

RunInstancesRequest runInstancesRequest =
   new RunInstancesRequest();

runInstancesRequest.withImageId("ami-4b814f22") // This is a base image, for example a ubuntu or linux instance
                   .withInstanceType("m1.small") // The size of the instance, these cost more the more powerful they are
                   .withMinCount(1) // Minimum amount of instances you want to launch
                   .withMaxCount(1); // Maximum amount of instances you want to launch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM