Configuring an existing CloudWatch alarm to create OpsItems (programmatically)
You can configure Amazon CloudWatch alarms to create OpsItems programmatically by using the AWS Command Line Interface (AWS CLI), AWS CloudFormation templates, or Java code snippets.
Topics
Before you begin
If you edit an existing alarm programmatically or create an alarm that creates OpsItems, you must specify an Amazon Resource Name (ARN). This ARN identifies Systems Manager OpsCenter as the target for OpsItems created from the alarm. You can customize the ARN so that OpsItems created from the alarm include specific information such as severity or category. Each ARN includes the information described in the following table.
Parameter | Details |
---|---|
|
The AWS Region where the alarm exists. For example:
|
|
The same AWS account ID used to create the alarm.
For example: |
|
A user-defined severity level for OpsItems created from
the alarm. Valid values: |
|
A category for OpsItems created from the alarm. Valid
values: |
Create the ARN by using the following syntax. This ARN doesn't include the
optional Category
parameter.
arn:aws:ssm:
Region
:account_ID
:opsitem:severity
Following is an example.
arn:aws:ssm:us-west-2:123456789012:opsitem:3
To create an ARN that uses the optional Category
parameter,
use the following syntax.
arn:aws:ssm:
Region
:account_ID
:opsitem:severity
#CATEGORY=category_name
Following is an example.
arn:aws:ssm:us-west-2:123456789012:opsitem:3#CATEGORY=Security
Configuring CloudWatch alarms to create OpsItems (AWS CLI)
This command requires that you specify an ARN for the
alarm-actions
parameter. For information about how to
create the ARN, see Before you begin.
To configure a CloudWatch alarm to create OpsItems (AWS CLI)
Install and configure the AWS Command Line Interface (AWS CLI), if you haven't already.
For information, see Installing or updating the latest version of the AWS CLI.
-
Run the following command to collect information about the alarm that you want to configure.
aws cloudwatch describe-alarms --alarm-names "
alarm name
" -
Run the following command to update an alarm. Replace each
example resource placeholder
with your own information.aws cloudwatch put-metric-alarm --alarm-name
name
\ --alarm-description "description
" \ --metric-namename
--namespacenamespace
\ --statisticstatistic
--periodvalue
--thresholdvalue
\ --comparison-operatorvalue
\ --dimensions "dimensions
" --evaluation-periodsvalue
\ --alarm-actions arn:aws:ssm:Region
:account_ID
:opsitem:severity
#CATEGORY=category_name
\ --unitunit
Here's an example.
Configuring CloudWatch alarms to create or update OpsItems (CloudFormation)
This section includes AWS CloudFormation templates that you can use to configure
CloudWatch alarms to automatically create or update OpsItems. Each template requires
that you specify an ARN for the AlarmActions
parameter. For
information about how to create the ARN, see Before you begin.
Metric alarm – Use the following
CloudFormation template to create or update a CloudWatch metric alarm. The alarm
specified in this template monitors Amazon Elastic Compute Cloud (Amazon EC2) instance status
checks. If the alarm enters the ALARM
state, it creates an OpsItem
in OpsCenter.
{ "AWSTemplateFormatVersion": "2010-09-09", "Parameters" : { "RecoveryInstance" : { "Description" : "The EC2 instance ID to associate this alarm with.", "Type" : "AWS::EC2::Instance::Id" } }, "Resources": { "RecoveryTestAlarm": { "Type": "AWS::CloudWatch::Alarm", "Properties": { "AlarmDescription": "Run a recovery action when instance status check fails for 15 consecutive minutes.", "Namespace": "AWS/EC2" , "MetricName": "StatusCheckFailed_System", "Statistic": "Minimum", "Period": "60", "EvaluationPeriods": "15", "ComparisonOperator": "GreaterThanThreshold", "Threshold": "0", "AlarmActions": [ {"Fn::Join" : ["", ["arn:
arn:aws:ssm:
", { "Ref" : "AWS::Partition" }, ":ssm:", { "Ref" : "AWS::Region" }, { "Ref" : "AWS:: AccountId" }, ":opsitem:3" ]]} ], "Dimensions": [{"Name": "InstanceId","Value": {"Ref": "RecoveryInstance"}}] } } } }Region
:account_ID
:opsitem:severity
#CATEGORY=category_name
Composite alarm – Use the
following CloudFormation template to create or update a composite alarm. A
composite alarm consists of multiple metric alarms. If the alarm enters the
ALARM
state, it creates an OpsItem in OpsCenter.
"Resources":{ "HighResourceUsage":{ "Type":"AWS::CloudWatch::CompositeAlarm", "Properties":{ "AlarmName":"HighResourceUsage", "AlarmRule":"(ALARM(HighCPUUsage) OR ALARM(HighMemoryUsage)) AND NOT ALARM(DeploymentInProgress)", "AlarmActions":"
arn:aws:ssm:
", "AlarmDescription":"Indicates that the system resource usage is high while no known deployment is in progress" }, "DependsOn":[ "DeploymentInProgress", "HighCPUUsage", "HighMemoryUsage" ] }, "DeploymentInProgress":{ "Type":"AWS::CloudWatch::CompositeAlarm", "Properties":{ "AlarmName":"DeploymentInProgress", "AlarmRule":"FALSE", "AlarmDescription":"Manually updated to TRUE/FALSE to disable other alarms" } }, "HighCPUUsage":{ "Type":"AWS::CloudWatch::Alarm", "Properties":{ "AlarmDescription":"CPUusageishigh", "AlarmName":"HighCPUUsage", "ComparisonOperator":"GreaterThanThreshold", "EvaluationPeriods":1, "MetricName":"CPUUsage", "Namespace":"CustomNamespace", "Period":60, "Statistic":"Average", "Threshold":70, "TreatMissingData":"notBreaching" } }, "HighMemoryUsage":{ "Type":"AWS::CloudWatch::Alarm", "Properties":{ "AlarmDescription":"Memoryusageishigh", "AlarmName":"HighMemoryUsage", "ComparisonOperator":"GreaterThanThreshold", "EvaluationPeriods":1, "MetricName":"MemoryUsage", "Namespace":"CustomNamespace", "Period":60, "Statistic":"Average", "Threshold":65, "TreatMissingData":"breaching" } } }Region
:account_ID
:opsitem:severity
#CATEGORY=category_name
Configuring CloudWatch alarms to create or update OpsItems (Java)
This section includes Java code snippets that you can use
to configure CloudWatch alarms to automatically create or update OpsItems. Each
snippet requires that you specify an ARN for the
validSsmActionStr
parameter. For information about how to
create the ARN, see Before you begin.
A specific alarm – Use the
following Java code snippet to create or update a CloudWatch alarm.
The alarm specified in this template monitors Amazon EC2 instance status checks.
If the alarm enters the ALARM
state, it creates an OpsItem in
OpsCenter.
import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.ComparisonOperator; import com.amazonaws.services.cloudwatch.model.Dimension; import com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest; import com.amazonaws.services.cloudwatch.model.PutMetricAlarmResult; import com.amazonaws.services.cloudwatch.model.StandardUnit; import com.amazonaws.services.cloudwatch.model.Statistic; private void putMetricAlarmWithSsmAction() { final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); Dimension dimension = new Dimension() .withName("InstanceId") .withValue(instanceId); String validSsmActionStr = "
arn:aws:ssm:
"; PutMetricAlarmRequest request = new PutMetricAlarmRequest() .withAlarmName(alarmName) .withComparisonOperator( ComparisonOperator.GreaterThanThreshold) .withEvaluationPeriods(1) .withMetricName("CPUUtilization") .withNamespace("AWS/EC2") .withPeriod(60) .withStatistic(Statistic.Average) .withThreshold(70.0) .withActionsEnabled(false) .withAlarmDescription( "Alarm when server CPU utilization exceeds 70%") .withUnit(StandardUnit.Seconds) .withDimensions(dimension) .withAlarmActions(validSsmActionStr); PutMetricAlarmResult response = cw.putMetricAlarm(request); }Region
:account_ID
:opsitem:severity
#CATEGORY=category_name
Update all alarms – Use the
following Java code snippet to update all CloudWatch alarms in your
AWS account to create OpsItems when an alarm enters the ALARM
state.
import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest; import com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult; import com.amazonaws.services.cloudwatch.model.MetricAlarm; private void listMetricAlarmsAndAddSsmAction() { final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); boolean done = false; DescribeAlarmsRequest request = new DescribeAlarmsRequest(); String validSsmActionStr = "
arn:aws:ssm:
"; while(!done) { DescribeAlarmsResult response = cw.describeAlarms(request); for(MetricAlarm alarm : response.getMetricAlarms()) { // assuming there are no alarm actions added for the metric alarm alarm.setAlarmActions(ImmutableList.of(validSsmActionStr)); } request.setNextToken(response.getNextToken()); if(response.getNextToken() == null) { done = true; } } }Region
:account_ID
:opsitem:severity
#CATEGORY=category_name