使用 AWS SDK 啟用執行中 Amazon EC2 執行個體監控功能 - Amazon Elastic Compute Cloud

使用 AWS SDK 啟用執行中 Amazon EC2 執行個體監控功能

下列程式碼範例示範如何為執行中的 Amazon EC2 執行個體啟用監控功能。

C++
適用於 C++ 的開發套件
注意

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

Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::MonitorInstancesRequest request; request.AddInstanceIds(instanceId); request.SetDryRun(true); auto dry_run_outcome = ec2Client.MonitorInstances(request); if (dry_run_outcome.IsSuccess()) { std::cerr << "Failed dry run to enable monitoring on instance. A dry run should trigger an error." << std::endl; return false; } else if (dry_run_outcome.GetError().GetErrorType() != Aws::EC2::EC2Errors::DRY_RUN_OPERATION) { std::cerr << "Failed dry run to enable monitoring on instance " << instanceId << ": " << dry_run_outcome.GetError().GetMessage() << std::endl; return false; } request.SetDryRun(false); auto monitorInstancesOutcome = ec2Client.MonitorInstances(request); if (!monitorInstancesOutcome.IsSuccess()) { std::cerr << "Failed to enable monitoring on instance " << instanceId << ": " << monitorInstancesOutcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully enabled monitoring on instance " << instanceId << std::endl; }
  • 如需 API 詳細資訊,請參閱《AWS SDK for C++ API 參考》中的 MonitorInstances

JavaScript
SDK for JavaScript (v3)
注意

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

import { MonitorInstancesCommand } from "@aws-sdk/client-ec2"; import { client } from "../libs/client.js"; // Turn on detailed monitoring for the selected instance. // By default, metrics are sent to Amazon CloudWatch every 5 minutes. // For a cost you can enable detailed monitoring which sends metrics every minute. export const main = async () => { const command = new MonitorInstancesCommand({ InstanceIds: ["INSTANCE_ID"], }); try { const { InstanceMonitorings } = await client.send(command); const instancesBeingMonitored = InstanceMonitorings.map( (im) => ` • Detailed monitoring state for ${im.InstanceId} is ${im.Monitoring.State}.`, ); console.log("Monitoring status:"); console.log(instancesBeingMonitored.join("\n")); } catch (err) { console.error(err); } };
  • 如需 API 詳細資訊,請參閱《AWS SDK for JavaScript API 參考》中的 MonitorInstances

Rust
SDK for Rust
注意

這是適用於預覽版本 SDK 的文件。SDK 可能會變更,請勿用於生產環境。

注意

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

async fn enable_monitoring(client: &Client, id: &str) -> Result<(), Error> { client.monitor_instances().instance_ids(id).send().await?; println!("Enabled monitoring"); Ok(()) }
  • 如需 API 詳細資訊,請參閱《適用於 Rust 的 AWS SDK API 參考》中的 MonitorInstances

SAP ABAP
適用於 SAP ABAP 的開發套件
注意

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

DATA lt_instance_ids TYPE /aws1/cl_ec2instidstringlist_w=>tt_instanceidstringlist. APPEND NEW /aws1/cl_ec2instidstringlist_w( iv_value = iv_instance_id ) TO lt_instance_ids. "Perform dry run" TRY. " DryRun is set to true. This checks for the required permissions to monitor the instance without actually making the request. " lo_ec2->monitorinstances( it_instanceids = lt_instance_ids iv_dryrun = abap_true ). CATCH /aws1/cx_rt_service_generic INTO DATA(lo_exception). " If the error code returned is `DryRunOperation`, then you have the required permissions to monitor this instance. " IF lo_exception->av_err_code = 'DryRunOperation'. MESSAGE 'Dry run to enable detailed monitoring completed.' TYPE 'I'. " DryRun is set to false to enable detailed monitoring. " lo_ec2->monitorinstances( it_instanceids = lt_instance_ids iv_dryrun = abap_false ). MESSAGE 'Detailed monitoring enabled.' TYPE 'I'. " If the error code returned is `UnauthorizedOperation`, then you don't have the required permissions to monitor this instance. " ELSEIF lo_exception->av_err_code = 'UnauthorizedOperation'. MESSAGE 'Dry run to enable detailed monitoring failed. User does not have the permissions to monitor the instance.' TYPE 'E'. ELSE. DATA(lv_error) = |"{ lo_exception->av_err_code }" - { lo_exception->av_err_msg }|. MESSAGE lv_error TYPE 'E'. ENDIF. ENDTRY.
  • 如需 API 詳細資訊,請參閱《適用於 SAP ABAP 的 AWS SDK API 參考》中的 MonitorInstances

如需完整的 AWS SDK 開發人員指南和程式碼範例清單,請參閱使用 Amazon EC2 搭配 AWS SDK。此主題也包含入門相關資訊和舊版 SDK 的詳細資訊。