

# Monitoring a channel or multiplex using Amazon CloudWatch Events
Monitor with CloudWatch events

MediaLive automatically turns the following information into events in CloudWatch Events:
+ Reporting on the [state of a channel or multiplex](monitor-activity-types-channel.md).
+ [Alerts ](monitor-activity-types-alerts-channels.md)generated when a channel is running.

You can use Amazon CloudWatch Events to manage these events. For example, you can create event rules and deliver the events in emails or SMS messages. You can deliver events to a number of destinations. This chapter describes how to deliver them through Amazon Simple Notification Service (SNS). 

For complete information about the options for managing events using Amazon CloudWatch Events, see the [CloudWatch Events User Guide](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/WhatIsCloudWatchEvents.html).

For complete information about using Amazon SNS, see the [SNS Developer Guide](https://docs.aws.amazon.com/sns/latest/dg/welcome.html).

Note that events are emitted on a best-effort basis. 

**Topics**
+ [

# JSON for a state change event
](monitoring-cloudwatch-json-state-change.md)
+ [

# JSON for an alert event
](monitoring-cloudwatch-json-alert.md)
+ [

# Option 1: Send all MediaLive events to an email address
](option-1.md)
+ [

# Option 2: Send events for specific channels to an email address
](option-2.md)

# JSON for a state change event


Events that are based on a change of state in a [channel or multiplex](monitor-activity-types-channel.md) are identified by their `detail-type` property:
+ `MediaLive Channel State Change` for a channel
+ `MediaLive Multiplex State Change` for a multiplex.

**Example**

Following is an example of the JSON payload for a state change event. Note the `detail-type` in line 3.

```
{
    "version": "0",
    "id": "fbcbbbe3-2541-d4a3-d819-x39f522a8ce",
    "detail-type": "MediaLive Channel State Change",
    "source": "aws.medialive",
    "account": "111122223333",
    "time": "2023-03-08T18:40:59Z",
    "region": "us-west-2",
    "resources": [
        "arn:aws:medialive:us-west-2:111122223333:channel:283886"
    ],
    "detail": {
        "channel_arn": "arn:aws:medialive:us-west-2:111122223333:channel:123456",
        "state": "DELETED",
        "message": "Deleted channel",
        "pipelines_running_count": 0
    }
}
```

# JSON for an alert event


Events that are based on [alerts](monitor-activity-types-alerts-channels.md) are identified by their `detail-type` property:
+ `MediaLive Channel Alert` for a channel
+ `MediaLive Multiplex Alert` for a multiplex.

**Example**

Following is an example of the JSON payload for an alert event. Note the `detail-type` in line 3.

```
{
    "version": "0",
    "id": "154769fb-9f7c-32a1-6822-26fppppe5a58",
    "detail-type": "MediaLive Channel Alert",
    "source": "aws.medialive",
    "account": "111122223333",
    "time": "2023-03-08T18:14:25Z",
    "region": "us-west-2",
    "resources": [
        "arn:aws:medialive:us-west-2:111122223333:channel:123456"
    ],
    "detail": {
        "alarm_state": "CLEARED",
        "alarm_id": "7ad616bd389832yue90aab1324bffab5b834a",
        "alert_type": "Failed to Create Output File or Socket",
        "pipeline": "0",
        "channel_arn": "arn:aws:medialive:us-west-2:111122223333:channel:123456",
        "message": "MPEGTS muxer for mediaID [1] unable to open output or stream [https://<path>]."
    }
}
```

# Option 1: Send all MediaLive events to an email address
Option 1: Events for all channel

This option shows how to set up to send all events to a single email address. The drawback of this setup is that the email account will receive a large volume of emails. Therefore, we recommend that you don't use this setup in a production environment. 

You must perform the following procedure in each Region where channels or multiplexes are running.

## Create a subscription
Step 1: Create a subscription

Create a subscription to set up a specific email address so that it automatically receives email notifications when any event occurs in MediaLive. You must identify an email recipient for the emails.

In the following procedure, we use the example of "MediaLive\$1alert" as the subject line and "MediaLive" as the sender of the email. We create the subscription using the Amazon Simple Notification Service (Amazon SNS) console.

**To create a subscription for email notifications (Amazon SNS console)**

1. Sign in to the AWS Management Console and open the Amazon SNS console at [https://console.aws.amazon.com/sns/v2/home](https://console.aws.amazon.com/sns/v2/home).

1. In the navigation pane, choose **Topics**, and then choose **Create new topic**.

1. In the **Create new topic** dialog box, for **Topic name**, type the name that you want for the subject line of the email, such as **MediaLive\$1alert**.

1. For **Display name**, type the name that you want for the sender of the email, such as **MediaLive**.

1. Choose **Create topic**.

1. Amazon SNS creates the topic and displays the ARN in the list of topics. For example, `arn:aws:sns:us-west-2:111122223333:MediaLive`, where `111122223333` is your AWS account.

1. Copy this ARN to your clipboard. 

1. In the navigation pane, choose **Subscriptions**, and then choose **Create subscription**.

1. On the **Subscriptions** page, choose **Create subscription**.

1. In the **Create subscriptions** dialog box, for **Topic ARN**, type or paste the ARN.

1. For **Protocol**, choose **Email**.

1. For **Endpoint**, type the email address of the recipient. You must be able to log on to this email account because Amazon SNS sends a confirmation email to this address.

1. Choose **Create subscription**.

   Amazon SNS sends a confirmation email to the address that you specified. 

1. Log on to that email account, and display the email. Choose the "Confirm subscription" link in the email to enable the subscription. A confirmation window appears in a web browser. You can close this window.

## Create a rule
Step 2: Create a rule

You now create a rule in Amazon CloudWatch that says, "When CloudWatch receives any event from `aws.medialive`, invoke the specified SNS topic." In other words, you create a rule that sends an email to the subscribed email address.

**To create a rule (Amazon CloudWatch console)**

1. Sign in to the AWS Management Console and open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

1. In the navigation pane, choose **Events**.

1. On the **Welcome to CloudWatch Events** page, choose **Create rule**.

1. On the **Step 1** page, in **Event Source**, choose **Event Pattern**.

1. Change **Build event pattern to match** to **Custom event pattern**.

1. In the box, type the following:

   ```
   {
     "source": [
       "aws.medialive"
     ]
   }
   ```

1. On the pane on the right, choose **Add target**.

1. Choose **SNS topic**.

1. For **Topic**, choose the topic that you created, for example, **MediaLive\$1alert**.

1. In **Configure input**, choose **Matched event**.

1. Choose **Configure details**.

1. Type a name and optional description, and then choose **Create rule**. 

Now, whenever an alert occurs in MediaLive, an event will be sent to Amazon CloudWatch. This event will trigger the rule that instructs CloudWatch to send an email to the email address that you specified in the SNS subscription.

# Option 2: Send events for specific channels to an email address
Option 2: Events for specific channels

You can set up a rule to send all events for one or several channels or multiplexes to one email address. You must perform this setup in each Region where channels or multiplexes are running.

Create as many subscriptions and rules combinations as you need. Follow the steps for [option 1](option-1.md), with these differences:
+ When creating the SNS subscription, you might want to add more detail to the topic, for example, **MediaLive\$1notifications\$1channel\$11234567**.
+ When creating the CloudWatch rule, you create an event pattern that identifies `aws.medialive` as the event source and the ARN for the specific channel or multiplex as the resource within that event source. For example, for a channel create this pattern:

  ```
  {
    "source": [
      "aws.medialive"
    ],
    "resources": [
      "arn:aws:medialive:us-west-2:111122223333:channel:1234567"
    ] 
  }
  ```

The resource is the ARN for the channel or multiplex. You can obtain this ARN from the channels list or multiplexes list on the MediaLive console. 

The rule for this example says, "When CloudWatch receives any event from `aws.medialive` for channel `1234567`, invoke the specified SNS topic." In other words, the rule triggers an email that is sent to the subscribed email address.

You can choose to include more than one channel or multiplex in the resources section, as shown in the following example:

```
  "resources": [
    "arn:aws:medialive:us-west-2:111122223333:channel:1234567",
    "arn:aws:medialive:us-west-2:111122223333:channel:2223334"
  ]
```