截至 2024 年 7 月 31 日, AWS SDK for Java 1.x 已進入維護模式,並將end-of-support
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
管理 Amazon EC2 執行個體
建立執行個體
建立新Amazon EC2實例通過調用卓越亞馬遜客户端的runInstances
方法,為其提供RunInstancesRequest包含Amazon Machine Image (AMI)使用,並使用執行個體類型。
匯入
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.InstanceType; import com.amazonaws.services.ec2.model.RunInstancesRequest; import com.amazonaws.services.ec2.model.RunInstancesResult; import com.amazonaws.services.ec2.model.Tag;
Code
RunInstancesRequest run_request = new RunInstancesRequest() .withImageId(ami_id) .withInstanceType(InstanceType.T1Micro) .withMaxCount(1) .withMinCount(1); RunInstancesResult run_response = ec2.runInstances(run_request); String reservation_id = run_response.getReservation().getInstances().get(0).getInstanceId();
請參完整範例
啟動執行個體
啟動Amazon EC2實例中,請調用卓越亞馬遜客户端的startInstances
方法,為其提供StartInstancesRequest,其中包含要啟動的執行個體之 ID。
匯入
import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.StartInstancesRequest;
Code
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); StartInstancesRequest request = new StartInstancesRequest() .withInstanceIds(instance_id); ec2.startInstances(request);
請參完整範例
停止執行個體
若要停止Amazon EC2實例中,請調用卓越亞馬遜客户端的stopInstances
方法,為其提供StopInstancesRequest,其中包含要停止的執行個體之 ID。
匯入
import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.StopInstancesRequest;
Code
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); StopInstancesRequest request = new StopInstancesRequest() .withInstanceIds(instance_id); ec2.stopInstances(request);
請參完整範例
重新啟動 執行個體
若要重新啟動Amazon EC2實例中,請調用卓越亞馬遜客户端的rebootInstances
方法,為其提供RebootInstancesRequest,其中包含要重新啟動的實例的 ID。
匯入
import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.RebootInstancesRequest; import com.amazonaws.services.ec2.model.RebootInstancesResult;
Code
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); RebootInstancesRequest request = new RebootInstancesRequest() .withInstanceIds(instance_id); RebootInstancesResult response = ec2.rebootInstances(request);
請參完整範例
描述 執行個體
若要列出您的執行個體,請建立DescribeInstancesRequest並致電卓越亞馬遜客户端的describeInstances
方法。它將返回DescribeInstancesResult物件,您可以使用此物件列出Amazon EC2執行個體。
執行個體依照保留分組。每個保留對應到呼叫 startInstances
,用以啟動執行個體。若要列出您的執行個體,您必須先呼叫DescribeInstancesResult
類別getReservations' method, and then call `getInstances
在每個返回保留物件。
匯入
import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.DescribeInstancesRequest; import com.amazonaws.services.ec2.model.DescribeInstancesResult; import com.amazonaws.services.ec2.model.Instance; import com.amazonaws.services.ec2.model.Reservation;
Code
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); boolean done = false; DescribeInstancesRequest request = new DescribeInstancesRequest(); while(!done) { DescribeInstancesResult response = ec2.describeInstances(request); for(Reservation reservation : response.getReservations()) { for(Instance instance : reservation.getInstances()) { System.out.printf( "Found instance with id %s, " + "AMI %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.getInstanceId(), instance.getImageId(), instance.getInstanceType(), instance.getState().getName(), instance.getMonitoring().getState()); } } request.setNextToken(response.getNextToken()); if(response.getNextToken() == null) { done = true; } }
結果會分頁;您可以將結果物件的getNextToken
方法添加到原始請求對象的setNextToken
方法,然後在下次調用describeInstances
。
請參完整範例
監控執行個體
您可以監控 Amazon EC2 執行個體的各個面向,例如 CPU 和網路使用率、可用記憶體和剩餘磁碟空間。若要進一步了解執行個體監控,請參監控Amazon EC2中的Amazon EC2Linux 執行個體使用者指南。
要開始監控實例,您必須創建MonitorInstancesRequest,並將要監控之執行個體的 ID 建立,然後將其傳遞給卓越亞馬遜客户端的monitorInstances
方法。
匯入
import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.MonitorInstancesRequest;
Code
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); MonitorInstancesRequest request = new MonitorInstancesRequest() .withInstanceIds(instance_id); ec2.monitorInstances(request);
請參完整範例
停止執行個體監控
要停止監控實例,請創建UnmonitorInstancesRequest,以停止監控之執行個體之 ID,然後將其傳遞給卓越亞馬遜客户端的unmonitorInstances
方法。
匯入
import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.UnmonitorInstancesRequest;
Code
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); UnmonitorInstancesRequest request = new UnmonitorInstancesRequest() .withInstanceIds(instance_id); ec2.unmonitorInstances(request);
請參完整範例
詳細資訊
-
RunInstances中的Amazon EC2API 參考
-
DescribeInstances中的Amazon EC2API 參考
-
StartInstances中的Amazon EC2API 參考
-
StopInstances中的Amazon EC2API 參考
-
RebootInstances中的Amazon EC2API 參考
-
MonitorInstances中的Amazon EC2API 參考
-
UnmonitorInstances中的Amazon EC2API 參考