

• 2026 年 4 月 30 日之後， AWS Systems Manager CloudWatch Dashboard 將不再可用。客戶可以繼續使用 Amazon CloudWatch 主控台來檢視、建立和管理其 Amazon CloudWatch 儀表板，就像現在一樣。如需詳細資訊，請參閱 [Amazon CloudWatch Dashboard 文件](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 設定現有的 CloudWatch 警示以建立 OpsItems (以程式設計方式)
<a name="OpsCenter-configuring-an-existing-alarm-programmatically"></a>

您可以使用 AWS Command Line Interface (AWS CLI)、 AWS CloudFormation 範本或Java程式碼片段，設定 Amazon CloudWatch 警示以OpsItems程式設計方式建立 。

**Topics**
+ [

## 開始之前
](#OpsCenter-create-OpsItems-from-CloudWatch-Alarms-before-you-begin)
+ [

## 設定 CloudWatch 警示以建立 OpsItems (AWS CLI)
](#OpsCenter-create-OpsItems-from-CloudWatch-Alarms-manually-configure-cli)
+ [

## 設定 CloudWatch 警示以建立或更新 OpsItems (CloudFormation)
](#OpsCenter-create-OpsItems-from-CloudWatch-Alarms-programmatically-configure-CloudFormation)
+ [

## 設定 CloudWatch 警示以建立或更新 OpsItems (Java)
](#OpsCenter-create-OpsItems-from-CloudWatch-Alarms-programmatically-configure-java)

## 開始之前
<a name="OpsCenter-create-OpsItems-from-CloudWatch-Alarms-before-you-begin"></a>

如果以程式設計方式編輯現有的警示，或建立警示 (該警示會建立 OpsItems)，則您必須指定 Amazon Resource Name (ARN)。此 ARN 可識別 Systems Manager OpsCenter 作為從警示建立之 OpsItems 的目標。您可以自訂 ARN，以便從警示建立之 OpsItems 包含特定資訊，例如嚴重性或類別。每個 ARN 包含下表中所述的資訊。


****  

| 參數 | 詳細資訊 | 
| --- | --- | 
|  `Region` (必要)  |   AWS 區域 警示所在的 。例如：`us-west-2`。如需您可在其中使用 OpsCenter 之 AWS 區域 相關資訊，請參閱 [AWS Systems Manager 端點和配額](https://docs.aws.amazon.com/general/latest/gr/ssm.html)。  | 
|  `account_ID` (必要)  |  用來建立警示的相同 AWS 帳戶 ID。例如：`123456789012`。帳戶 ID 後面必須加上冒號 (`:`) 和參數 `opsitem`，如下列範例所示。  | 
|  `severity` (必要)  |  從警示建立之 OpsItems 的使用者定義嚴重性層級。有效值：`1`、`2`、`3`、`4`  | 
|  `Category` (選用)  |  從警示建立之 OpsItems 的類別 有效值：`Availability`、`Cost`、`Performance`、`Recovery` 和 `Security`。  | 

使用下列語法建立 ARN。此 ARN 不包含選用的 `Category` 參數。

```
arn:aws:ssm:Region:account_ID:opsitem:severity
```

以下是範例。

```
arn:aws:ssm:us-west-2:123456789012:opsitem:3
```

若要建立使用選用 `Category` 參數的 ARN，請使用下列語法。

```
arn:aws:ssm:Region:account_ID:opsitem:severity#CATEGORY=category_name
```

以下是範例。

```
arn:aws:ssm:us-west-2:123456789012:opsitem:3#CATEGORY=Security
```

## 設定 CloudWatch 警示以建立 OpsItems (AWS CLI)
<a name="OpsCenter-create-OpsItems-from-CloudWatch-Alarms-manually-configure-cli"></a>

此命令要求您為 `alarm-actions` 參數指定 ARN。如需如何建立 ARN 的相關資訊，請參閱 [開始之前](#OpsCenter-create-OpsItems-from-CloudWatch-Alarms-before-you-begin)。

**設定 CloudWatch 警示以建立 OpsItems (AWS CLI)**

1. 如果您尚未安裝並設定 AWS Command Line Interface (AWS CLI)，請安裝並設定 。

   如需相關資訊，請參閱[安裝或更新最新版本的 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)。

1. 執行下列命令以收集您要設定之警示的相關資訊。

   ```
   aws cloudwatch describe-alarms --alarm-names "alarm name"
   ```

1. 執行下列命令以更新警示。將每個*範例資源預留位置*取代為您自己的資訊。

   ```
   aws cloudwatch put-metric-alarm --alarm-name name \
   --alarm-description "description" \
   --metric-name name --namespace namespace \
   --statistic statistic --period value --threshold value \
   --comparison-operator value \
   --dimensions "dimensions" --evaluation-periods value \
       --alarm-actions arn:aws:ssm:Region:account_ID:opsitem:severity#CATEGORY=category_name \
   --unit unit
   ```

   範例如下。

------
#### [ Linux & macOS ]

   ```
   aws cloudwatch put-metric-alarm --alarm-name cpu-mon \
   --alarm-description "Alarm when CPU exceeds 70 percent" \
   --metric-name CPUUtilization --namespace AWS/EC2 \
   --statistic Average --period 300 --threshold 70 \
   --comparison-operator GreaterThanThreshold \
   --dimensions "Name=InstanceId,Value=i-12345678" --evaluation-periods 2 \
   --alarm-actions arn:aws:ssm:us-east-1:123456789012:opsitem:3#CATEGORY=Security \
   --unit Percent
   ```

------
#### [ Windows ]

   ```
   aws cloudwatch put-metric-alarm --alarm-name cpu-mon ^
   --alarm-description "Alarm when CPU exceeds 70 percent" ^
   --metric-name CPUUtilization --namespace AWS/EC2 ^
   --statistic Average --period 300 --threshold 70 ^
   --comparison-operator GreaterThanThreshold ^
   --dimensions "Name=InstanceId,Value=i-12345678" --evaluation-periods 2 ^
   --alarm-actions arn:aws:ssm:us-east-1:123456789012:opsitem:3#CATEGORY=Security ^
   --unit Percent
   ```

------

## 設定 CloudWatch 警示以建立或更新 OpsItems (CloudFormation)
<a name="OpsCenter-create-OpsItems-from-CloudWatch-Alarms-programmatically-configure-CloudFormation"></a>

本節包含可用來設定 CloudWatch 警示以自動建立或更新 的 AWS CloudFormation 範本OpsItems。每個範本都要求您為 `AlarmActions` 參數指定 ARN。如需如何建立 ARN 的相關資訊，請參閱 [開始之前](#OpsCenter-create-OpsItems-from-CloudWatch-Alarms-before-you-begin)。

**指標警示** – 使用下列 CloudFormation 範本來建立或更新 CloudWatch 指標警示。此範本中指定的警示會監控 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體狀態檢查。如果警示進入 `ALARM` 狀態，它會在 OpsCenter 中建立 OpsItem。

```
    {
      "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:Region:account_ID:opsitem:severity#CATEGORY=category_name", { "Ref" : "AWS::Partition" }, ":ssm:", { "Ref" : "AWS::Region" }, { "Ref" : "AWS:: AccountId" }, ":opsitem:3" ]]} ],
            "Dimensions": [{"Name": "InstanceId","Value": {"Ref": "RecoveryInstance"}}]
          }
        }
      }
    }
```

**複合警示** – 使用下列 CloudFormation 範本來建立或更新複合警示。複合警示由多個指標警示組成。如果警示進入 `ALARM` 狀態，它會在 OpsCenter 中建立 OpsItem。

```
"Resources":{
       "HighResourceUsage":{
          "Type":"AWS::CloudWatch::CompositeAlarm",
          "Properties":{
             "AlarmName":"HighResourceUsage",
             "AlarmRule":"(ALARM(HighCPUUsage) OR ALARM(HighMemoryUsage)) AND NOT ALARM(DeploymentInProgress)",
             "AlarmActions":"arn:aws:ssm:Region:account_ID:opsitem:severity#CATEGORY=category_name",
             "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"
          }
       }
    }
```

## 設定 CloudWatch 警示以建立或更新 OpsItems (Java)
<a name="OpsCenter-create-OpsItems-from-CloudWatch-Alarms-programmatically-configure-java"></a>

本節包含 Java 程式碼片段，您可以用這些程式碼片段來設定 CloudWatch 警示以自動建立或更新 OpsItems。每個程式碼片段都要求您為 `validSsmActionStr` 參數指定 ARN。如需如何建立 ARN 的相關資訊，請參閱 [開始之前](#OpsCenter-create-OpsItems-from-CloudWatch-Alarms-before-you-begin)。

**特定警示** – 使用以下 Java 程式碼片段來建立或更新 CloudWatch 警示。此範本中指定的警示會監控 Amazon EC2 執行個體狀態檢查。如果警示進入 `ALARM` 狀態，它會在 OpsCenter 中建立 OpsItem。

```
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:Region:account_ID:opsitem:severity#CATEGORY=category_name";
     
        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);
    }
```

**更新所有警示** – 使用以下 Java 程式碼片段來更新 AWS 帳戶 中的所有 CloudWatch 警示，以在警示進入 `ALARM` 狀態時建立 OpsItems。

```
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:Region:account_ID:opsitem:severity#CATEGORY=category_name";
     
        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;
            }
        }
    }
```