Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Kelola Amazon EC2 instance
Buatlah sebuah instans
Buat Amazon EC2 instance baru dengan memanggil runInstances
Impor
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.InstanceType; import software.amazon.awssdk.services.ec2.model.RunInstancesRequest; import software.amazon.awssdk.services.ec2.model.RunInstancesResponse; import software.amazon.awssdk.services.ec2.model.Tag; import software.amazon.awssdk.services.ec2.model.CreateTagsRequest; import software.amazon.awssdk.services.ec2.model.Ec2Exception;
Kode
public static String createEC2Instance(Ec2Client ec2,String name, String amiId ) { RunInstancesRequest runRequest = RunInstancesRequest.builder() .imageId(amiId) .instanceType(InstanceType.T1_MICRO) .maxCount(1) .minCount(1) .build(); RunInstancesResponse response = ec2.runInstances(runRequest); String instanceId = response.instances().get(0).instanceId(); Tag tag = Tag.builder() .key("Name") .value(name) .build(); CreateTagsRequest tagRequest = CreateTagsRequest.builder() .resources(instanceId) .tags(tag) .build(); try { ec2.createTags(tagRequest); System.out.printf( "Successfully started EC2 Instance %s based on AMI %s", instanceId, amiId); return instanceId; } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
Lihat contoh lengkapnya
Memulai instans
Untuk memulai sebuah Amazon EC2 instance, panggil startInstances
Impor
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.StartInstancesRequest; import software.amazon.awssdk.services.ec2.model.StopInstancesRequest;
Kode
public static void startInstance(Ec2Client ec2, String instanceId) { StartInstancesRequest request = StartInstancesRequest.builder() .instanceIds(instanceId) .build(); ec2.startInstances(request); System.out.printf("Successfully started instance %s", instanceId); }
Lihat contoh lengkapnya
Menghentikan instans
Untuk menghentikan Amazon EC2 instance, panggil stopInstances
Impor
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.StartInstancesRequest; import software.amazon.awssdk.services.ec2.model.StopInstancesRequest;
Kode
public static void stopInstance(Ec2Client ec2, String instanceId) { StopInstancesRequest request = StopInstancesRequest.builder() .instanceIds(instanceId) .build(); ec2.stopInstances(request); System.out.printf("Successfully stopped instance %s", instanceId); }
Lihat contoh lengkapnya
Menyalakan ulang instans
Untuk me-reboot sebuah Amazon EC2 instance, panggil rebootInstances
Impor
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.Ec2Exception; import software.amazon.awssdk.services.ec2.model.RebootInstancesRequest;
Kode
public static void rebootEC2Instance(Ec2Client ec2, String instanceId) { try { RebootInstancesRequest request = RebootInstancesRequest.builder() .instanceIds(instanceId) .build(); ec2.rebootInstances(request); System.out.printf( "Successfully rebooted instance %s", instanceId); } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
Lihat contoh lengkapnya
Menjelaskan instans
Untuk membuat daftar instance Anda, buat DescribeInstancesRequestdescribeInstances
Instans dikelompokkan berdasarkan reservasi. Setiap reservasi sesuai dengan panggilan startInstances
yang meluncurkan instance. Untuk membuat daftar instance Anda, Anda harus terlebih dahulu memanggil reservations
metode DescribeInstancesResponse
kelas, dan kemudian memanggil instances
setiap objek Reservasi
Impor
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest; import software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse; import software.amazon.awssdk.services.ec2.model.Instance; import software.amazon.awssdk.services.ec2.model.Reservation; import software.amazon.awssdk.services.ec2.model.Ec2Exception;
Kode
public static void describeEC2Instances( Ec2Client ec2){ String nextToken = null; try { do { DescribeInstancesRequest request = DescribeInstancesRequest.builder().maxResults(6).nextToken(nextToken).build(); DescribeInstancesResponse response = ec2.describeInstances(request); for (Reservation reservation : response.reservations()) { for (Instance instance : reservation.instances()) { System.out.println("Instance Id is " + instance.instanceId()); System.out.println("Image id is "+ instance.imageId()); System.out.println("Instance type is "+ instance.instanceType()); System.out.println("Instance state name is "+ instance.state().name()); System.out.println("monitoring information is "+ instance.monitoring().state()); } } nextToken = response.nextToken(); } while (nextToken != null); } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
Hasil paged; Anda bisa mendapatkan hasil lebih lanjut dengan meneruskan nilai yang dikembalikan dari nextToken
metode objek hasil ke nextToken
metode objek permintaan baru, kemudian menggunakan objek permintaan baru dalam panggilan berikutnya. describeInstances
Lihat contoh lengkapnya
Memantau sebuah instance
Anda dapat memantau berbagai aspek Amazon EC2 instance Anda, seperti CPU dan pemanfaatan jaringan, memori yang tersedia, dan ruang disk yang tersisa. Untuk mempelajari lebih lanjut tentang pemantauan instans, lihat Pemantauan Amazon EC2 di Panduan Amazon EC2 Pengguna untuk Instans Linux.
Untuk mulai memantau instance, Anda harus membuat MonitorInstancesRequestmonitorInstances
Impor
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.MonitorInstancesRequest; import software.amazon.awssdk.services.ec2.model.UnmonitorInstancesRequest;
Kode
public static void monitorInstance( Ec2Client ec2, String instanceId) { MonitorInstancesRequest request = MonitorInstancesRequest.builder() .instanceIds(instanceId).build(); ec2.monitorInstances(request); System.out.printf( "Successfully enabled monitoring for instance %s", instanceId); }
Lihat contoh lengkapnya
Hentikan pemantauan instans
Untuk menghentikan pemantauan instance, buat UnmonitorInstancesRequestunmonitorInstances
Impor
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.MonitorInstancesRequest; import software.amazon.awssdk.services.ec2.model.UnmonitorInstancesRequest;
Kode
public static void unmonitorInstance(Ec2Client ec2, String instanceId) { UnmonitorInstancesRequest request = UnmonitorInstancesRequest.builder() .instanceIds(instanceId).build(); ec2.unmonitorInstances(request); System.out.printf( "Successfully disabled monitoring for instance %s", instanceId); }
Lihat contoh lengkapnya
Informasi lain
-
RunInstancesdi Referensi Amazon EC2 API
-
DescribeInstancesdi Referensi Amazon EC2 API
-
StartInstancesdi Referensi Amazon EC2 API
-
StopInstancesdi Referensi Amazon EC2 API
-
RebootInstancesdi Referensi Amazon EC2 API
-
MonitorInstancesdi Referensi Amazon EC2 API
-
UnmonitorInstancesdi Referensi Amazon EC2 API