DescribeInstanceStatus 搭配 a AWS SDK 或 CLI 使用 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DescribeInstanceStatus 搭配 a AWS SDK 或 CLI 使用

下列程式碼範例示範如何使用 DescribeInstanceStatus

CLI
AWS CLI

描述執行個體的狀態

下列 describe-instance-status 範例會描述指定執行個體的目前狀態。

aws ec2 describe-instance-status \ --instance-ids i-1234567890abcdef0

輸出:

{ "InstanceStatuses": [ { "InstanceId": "i-1234567890abcdef0", "InstanceState": { "Code": 16, "Name": "running" }, "AvailabilityZone": "us-east-1d", "SystemStatus": { "Status": "ok", "Details": [ { "Status": "passed", "Name": "reachability" } ] }, "InstanceStatus": { "Status": "ok", "Details": [ { "Status": "passed", "Name": "reachability" } ] } } ] }

如需詳細資訊,請參閱 Amazon EC2 使用者指南中的監控執行個體的狀態

PowerShell
for PowerShell 工具

範例 1:此範例說明指定執行個體的狀態。

Get-EC2InstanceStatus -InstanceId i-12345678

輸出:

AvailabilityZone : us-west-2a Events : {} InstanceId : i-12345678 InstanceState : Amazon.EC2.Model.InstanceState Status : Amazon.EC2.Model.InstanceStatusSummary SystemStatus : Amazon.EC2.Model.InstanceStatusSummary
$status = Get-EC2InstanceStatus -InstanceId i-12345678 $status.InstanceState

輸出:

Code Name ---- ---- 16 running
$status.Status

輸出:

Details Status ------- ------ {reachability} ok
$status.SystemStatus

輸出:

Details Status ------- ------ {reachability} ok
Rust
Rust 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

async fn show_all_events(client: &Client) -> Result<(), Error> { let resp = client.describe_regions().send().await.unwrap(); for region in resp.regions.unwrap_or_default() { let reg: &'static str = Box::leak(Box::from(region.region_name().unwrap())); let region_provider = RegionProviderChain::default_provider().or_else(reg); let config = aws_config::from_env().region(region_provider).load().await; let new_client = Client::new(&config); let resp = new_client.describe_instance_status().send().await; println!("Instances in region {}:", reg); println!(); for status in resp.unwrap().instance_statuses() { println!( " Events scheduled for instance ID: {}", status.instance_id().unwrap_or_default() ); for event in status.events() { println!(" Event ID: {}", event.instance_event_id().unwrap()); println!(" Description: {}", event.description().unwrap()); println!(" Event code: {}", event.code().unwrap().as_ref()); println!(); } } } Ok(()) }