DeleteAlarms.php - AWS Code Sample

DeleteAlarms.php

DeleteAlarms.php demonstrates how to delete one or more specified Amazon CloudWatch alarms.

<?php /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ require 'vendor/autoload.php'; use Aws\CloudWatch\CloudWatchClient; use Aws\Exception\AwsException; /* //////////////////////////////////////////////////////////////////////////// * Purpose: Deletes an existing Amazon CloudWatch alarm. * * Prerequisites: At least one existing CloudWatch alarm. * * Inputs: * - $cloudWatchClient: An initialized CloudWatch client. * - $alarmNames: An array of names of the alarms to delete. * * Returns: Information about the deletion request; otherwise, * the error message. * ///////////////////////////////////////////////////////////////////////// */ function deleteAlarms($cloudWatchClient, $alarmNames) { try { $result = $cloudWatchClient->deleteAlarms([ 'AlarmNames' => $alarmNames ]); return 'The specified alarms at the following effective URI have ' . 'been deleted or do not currently exist: ' . $result['@metadata']['effectiveUri']; } catch (AwsException $e) { return 'Error: ' . $e->getAwsErrorMessage(); } } function deleteTheAlarms() { $alarmNames = array('my-alarm'); $cloudWatchClient = new CloudWatchClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-08-01' ]); echo deleteAlarms($cloudWatchClient, $alarmNames); } // Uncomment the following line to run this code in an AWS account. // deleteTheAlarms();

Sample Details

Service: cloudwatch

Last tested: 2020-05-05

Author: pccornel (AWS)

Type: full-example