简体   繁体   English

如何使用Java在EC2中启动虚拟机

[英]How to launch a Virtual Machine in EC2 using Java

I am totally new to AWS. 我对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. 我已经获得了一项正式任务,以探索适用于Java的AWS api,以便我们可以在EC2上启动虚拟机。 I have gone through the official docs at sdk-for-java but having few doubts: 我已经阅读了sdk-for-java的官方文档,但几乎没有疑问:

  1. Would I need to deploy the java code on EC2 ? 我需要在EC2上部署Java代码吗?
  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? Java代码可以创建并启动虚拟服务器吗?还是我们只能通过代码启动虚拟服务器,而需要通过EC2仪表板创建虚拟机?

Thanks 谢谢

First off, since you're very unfamiliar with AWS, have a read through their documentation. 首先,由于您不熟悉AWS,因此请通读其文档。 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的AWS开发工具包入门
Java examples Java实例

To answer your question, you want to start a virtual machine. 要回答您的问题,您想启动一个虚拟机。 Within AWS, these are referred to as "instances". 在AWS中,这些被称为“实例”。 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. 要使用API​​启动实例,您可以执行在本地进行API调用的代码,只要您具有有效的凭据等,代码就可以正常工作。 Your code does not need to be run inside of EC2. 您的代码无需在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. 只要您具有有效的凭证并包含在Java项目中的AWS-SDK,以下代码将为您运行EC2实例。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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