cw_enablealarmactions.ts - AWS Code Sample


/* Copyright, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3), which is pending release. The preview version of the SDK is available at This example is in the 'AWS SDK for JavaScript v3 Developer Guide' at Purpose: cw_enablealarmactions.ts demonstrates how to enable actions for Amazon CloudWatch alarms. Inputs (replace in code): - REGION - ALARM_NAME - ACTION_ARN Running the code: ts-node cw_enablealarmactions.ts */ // Import required AWS SDK clients and commands for Node.js const { CloudWatchClient, PutMetricAlarmCommand, EnableAlarmActionsCommand } = require("@aws-sdk/client-cloudwatch"); // Set the AWS Region const REGION = "REGION"; //e.g. "us-east-1" // Set the parameters const params = { AlarmName: "ALARM_NAME", //ALARM_NAME ComparisonOperator: "GreaterThanThreshold", EvaluationPeriods: 1, MetricName: "CPUUtilization", Namespace: "AWS/EC2", Period: 60, Statistic: "Average", Threshold: 70.0, ActionsEnabled: true, AlarmActions: ["ACTION_ARN"], //e.g., "arn:aws:automate:us-east-1:ec2:stop" AlarmDescription: "Alarm when server CPU exceeds 70%", Dimensions: [ { Name: "InstanceId", Value: "INSTANCE_ID", }, ], Unit: "Percent", }; // Create CloudWatch service object const cw = new CloudWatchClient(REGION); const run = async () => { try { const data = await cw.send(new PutMetricAlarmCommand(params)); console.log("Alarm action added; RequestID:", data.$metadata.requestId); const paramsEnableAlarmAction = { AlarmNames: [params.AlarmName], }; try { const data = await cw.send( new EnableAlarmActionsCommand(paramsEnableAlarmAction) ); console.log("Alarm action enabled; RequestID:", data.$metadata.requestId); } catch (err) { console.log("Error", err); } } catch (err) { console.log("Error", err); } }; run();