Use UnmonitorInstances with an AWS SDK or CLI - Amazon Elastic Compute Cloud

Use UnmonitorInstances with an AWS SDK or CLI

The following code examples show how to use UnmonitorInstances.

Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:

C++
SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

//! Disable monitoring for an EC2 instance. /*! \param instanceId: An EC2 instance ID. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::EC2::disableMonitoring(const Aws::String &instanceId, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::UnmonitorInstancesRequest unrequest; unrequest.AddInstanceIds(instanceId); unrequest.SetDryRun(true); Aws::EC2::Model::UnmonitorInstancesOutcome dryRunOutcome = ec2Client.UnmonitorInstances(unrequest); if (dryRunOutcome.IsSuccess()) { std::cerr << "Failed dry run to disable monitoring on instance. A dry run should trigger an error." << std::endl; return false; } else if (dryRunOutcome.GetError().GetErrorType() != Aws::EC2::EC2Errors::DRY_RUN_OPERATION) { std::cout << "Failed dry run to disable monitoring on instance " << instanceId << ": " << dryRunOutcome.GetError().GetMessage() << std::endl; return false; } unrequest.SetDryRun(false); Aws::EC2::Model::UnmonitorInstancesOutcome unmonitorInstancesOutcome = ec2Client.UnmonitorInstances(unrequest); if (!unmonitorInstancesOutcome.IsSuccess()) { std::cout << "Failed to disable monitoring on instance " << instanceId << ": " << unmonitorInstancesOutcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully disable monitoring on instance " << instanceId << std::endl; } return unmonitorInstancesOutcome.IsSuccess(); }
CLI
AWS CLI

To disable detailed monitoring for an instance

This example command disables detailed monitoring for the specified instance.

Command:

aws ec2 unmonitor-instances --instance-ids i-1234567890abcdef0

Output:

{ "InstanceMonitorings": [ { "InstanceId": "i-1234567890abcdef0", "Monitoring": { "State": "disabling" } } ] }
JavaScript
SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import { UnmonitorInstancesCommand } from "@aws-sdk/client-ec2"; import { client } from "../libs/client.js"; export const main = async () => { const command = new UnmonitorInstancesCommand({ InstanceIds: ["i-09a3dfe7ae00e853f"], }); try { const { InstanceMonitorings } = await client.send(command); const instanceMonitoringsList = InstanceMonitorings.map( (im) => ` • Detailed monitoring state for ${im.InstanceId} is ${im.Monitoring.State}.`, ); console.log("Monitoring status:"); console.log(instanceMonitoringsList.join("\n")); } catch (err) { console.error(err); } };
PowerShell
Tools for PowerShell

Example 1: This example disables detailed monitoring for the specified instance.

Stop-EC2InstanceMonitoring -InstanceId i-12345678

Output:

InstanceId Monitoring ---------- ---------- i-12345678 Amazon.EC2.Model.Monitoring

For a complete list of AWS SDK developer guides and code examples, see Create Amazon EC2 resources using an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.