Utilisation de UnmonitorInstances avec un kit AWS SDK ou une interface de ligne de commande - Exemples de code de kit AWS SDK

D’autres exemples de kits AWS SDK sont disponibles dans le référentiel GitHub AWS Doc SDK Examples.

Utilisation de UnmonitorInstances avec un kit AWS SDK ou une interface de ligne de commande

Les exemples de code suivants illustrent comment utiliser UnmonitorInstances.

Les exemples d’actions sont des extraits de code de programmes de plus grande envergure et doivent être exécutés en contexte. Vous pouvez voir cette action en contexte dans l’exemple de code suivant :

C++
kit SDK pour C++
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

//! 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(); }
  • Pour plus d’informations sur l’API, consultez la section UnmonitorInstances (français non garanti) dans la Référence d’API AWS SDK pour C++.

CLI
AWS CLI

Désactivation de la surveillance détaillée d’une instance

Cet exemple de commande désactive la surveillance détaillée de l’instance spécifiée.

Commande :

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

Sortie :

{ "InstanceMonitorings": [ { "InstanceId": "i-1234567890abcdef0", "Monitoring": { "State": "disabling" } } ] }
  • Pour plus d’informations sur l’API, consultez la rubrique UnmonitorInstances dans l’AWS CLI Command Reference.

JavaScript
Kit SDK pour JavaScript (v3)
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import { EC2Client, UnmonitorInstancesCommand } from "@aws-sdk/client-ec2"; import { fileURLToPath } from "node:url"; import { parseArgs } from "node:util"; /** * Turn off detailed monitoring for the selected instance. * @param {{ instanceIds: string[] }} options */ export const main = async ({ instanceIds }) => { const client = new EC2Client({}); const command = new UnmonitorInstancesCommand({ InstanceIds: instanceIds, }); 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 (caught) { if ( caught instanceof Error && caught.name === "InvalidInstanceID.NotFound" ) { console.warn(`${caught.message}`); } else { throw caught; } } };
  • Pour plus d’informations sur l’API, consultez la section UnmonitorInstances (français non garanti) dans la Référence d’API AWS SDK pour JavaScript.

PowerShell
Outils pour PowerShell V4

Exemple 1 : cet exemple désactive la surveillance détaillée de l’instance spécifiée.

Stop-EC2InstanceMonitoring -InstanceId i-12345678

Sortie :

InstanceId Monitoring ---------- ---------- i-12345678 Amazon.EC2.Model.Monitoring
  • Pour plus de détails sur l’API, consultez UnmonitorInstances dans la Référence des applets de commande pour les Outils AWS pour PowerShell (V4).

Outils pour PowerShell V5

Exemple 1 : cet exemple désactive la surveillance détaillée de l’instance spécifiée.

Stop-EC2InstanceMonitoring -InstanceId i-12345678

Sortie :

InstanceId Monitoring ---------- ---------- i-12345678 Amazon.EC2.Model.Monitoring
  • Pour plus de détails sur l’API, consultez UnmonitorInstances dans la Référence des applets de commande pour les Outils AWS pour PowerShell (V5).