跳至內容

如何使用 EventBridge 和 Amazon SNS 監控 EC2 Linux 執行個體安全群組的變更?

2 分的閱讀內容
0

我為 Amazon Elastic Compute Cloud (Amazon EC2) Linux 執行個體設定了一個安全群組。我想使​​用 Amazon EventBridge 和 Amazon Simple Notification Service (Amazon SNS) 監控安全群組的變更。

簡短描述

若要監控安全群組的變更,請建立 EventBridge 規則,該規則會在應用程式進行 API 呼叫以修改安全群組時執行。然後,為符合規則的事件設定 Amazon SNS 通知。

解決方法

**先決條件:**建立 AWS CloudTrail 追蹤來記錄 API 呼叫。

建立並訂閱 SNS 主題

建立 SNS 主題。然後,訂閱主題,接著在 Protocol (協定) 中選取 Email (電子郵件)。Amazon SNS 會向您傳送訂閱確認電子郵件。

建立 EventBridge 規則

設定 EventBridge 規則,然後完成下列步驟來設定規則模式:

  1. 選取 Pre-defined pattern by service (依服務預先定義模式)。

  2. 在 Service provider (服務供應商) 中,選擇 AWS。

  3. 在 Service name (服務名稱) 中,選擇 EC2。

  4. 在 Event type (事件類型) 中,選擇 AWS API Call via CloudTrail(透過 CloudTrail 的 AWS API 呼叫)。

  5. 選擇 Specific operation (特定作業),然後逐一輸入以下 API 呼叫:

    AuthorizeSecurityGroupIngressAuthorizeSecurityGroupEgress
    RevokeSecurityGroupIngress
    RevokeSecurityGroupEgress

    **注意:**輸入每個 API 呼叫後,請選擇 Add (新增)。這些 API 呼叫會新增或刪除安全群組規則。
    上述設定會建立下列事件模式:

    {  "source": [
        "aws.ec2"
      ],
      "detail-type": [
        "AWS API Call via CloudTrail"
      ],
      "detail": {
        "eventSource": [
          "ec2.amazonaws.com"
        ],
        "eventName": [
          "AuthorizeSecurityGroupIngress",
          "AuthorizeSecurityGroupEgress",
          "RevokeSecurityGroupIngress",
          "RevokeSecurityGroupEgress"
        ]
      }
    }
  6. 在 Select targets (選取目標) 下,從 Target (目標) 清單中選取 SNS topic (SNS 主題)。

  7. 在 Topic (主題) 中,輸入您建立的主題。

  8. (選用) 預設情況下,在 Configure input (設定輸入) Matched event (相符事件) 下,選擇 Matched event (相符事件)。此組態會將事件的整個 JSON 輸出傳遞到 SNS 主題。若要篩選事件資訊,請選取輸入轉換器。使用輸入轉換器來自訂事件中的文字,以建立可讀取的訊息。例如,對 Input Path (輸入路徑) 使用下列鍵值組:

    {"name":"$.detail.requestParameters.groupId","source":"$.detail.eventName","time":"$.time","value":"$.detail"}

    在 Input Template (輸入範本) 中,輸入您想在訊息中顯示的文字和變數。
    輸入範本範例:

    "A source API call was made against the security group name on time with the below details"" value "
  9. 選擇 Create (建立)。

AWS 官方已更新 2 年前