

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

# 使用 Amazon EventBridge 對 Amazon Location Service 事件做出反應
<a name="location-events"></a>

Amazon EventBridge 是一種無伺服器事件匯流排，可使用 Amazon Location 等 AWS 服務的資料，有效率地將應用程式連接在一起。EventBridge 會從 Amazon Location 接收事件，並將該資料路由至類似的目標 AWS Lambda。您可以設定路由規則來判斷要將資料傳送到何處，以建置可即時反應的應用程式架構。

根據預設，只有地理圍欄事件 (`ENTER` 和 `EXIT`事件，當裝置進入或離開地理圍欄區域時） 會傳送至 EventBridge。您也可以啟用追蹤器資源的所有篩選位置更新事件。如需詳細資訊，請參閱[啟用追蹤器的更新事件](#enable-update-events)。

如需詳細資訊，請參閱[《Amazon EventBridge 使用者指南》中的事件和事件模式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-events.html)。 * EventBridge *

**Topics**
+ [啟用追蹤器的更新事件](#enable-update-events)
+ [建立 Amazon Location 的事件規則](#create-event-rule)
+ [Amazon Location Service 的 Amazon EventBridge 事件範例](#example-event)

## 啟用追蹤器的更新事件
<a name="enable-update-events"></a>

根據預設，Amazon Location `EXIT` 只會將 `ENTER`和地理圍欄事件傳送至 EventBridge。您可以啟用追蹤器的所有篩選位置`UPDATE`事件，以傳送至 EventBridge。您可以在[建立](https://docs.aws.amazon.com/location/latest/APIReference/API_WaypointTracking_CreateTracker.html)或[更新](https://docs.aws.amazon.com/location/latest/APIReference/API_WaypointTracking_UpdateTracker.html)追蹤器時執行此操作。

例如，若要使用 更新現有的追蹤器 AWS CLI，您可以使用下列命令 （使用追蹤器資源的名稱取代 *MyTracker*)。

```
aws location update-tracker --tracker-name MyTracker --event-bridge-enabled
```

若要關閉追蹤器的位置事件，您必須使用 API 或 Amazon Location Service 主控台。

## 建立 Amazon Location 的事件規則
<a name="create-event-rule"></a>

您可以在 EventBridge [中為每個事件匯流排建立最多 300 個規則](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-quota.html)，以設定為回應 Amazon Location 事件而採取的動作。

例如，您可以為地理圍欄事件建立規則，當在地理圍欄邊界內偵測到電話時，就會傳送推送通知。

**建立 Amazon Location 事件的規則**

使用下列值，根據 Amazon Location 事件[建立 EventBridge 規則](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-visual.html)：
+ 針對**規則類型**，選擇**具有事件模式的規則**。
+ 在**事件模式**方塊中，新增下列模式：

  ```
  {
    "source": ["aws.geo"],
    "detail-type": ["Location Geofence Event"]
  }
  ```

  若要建立追蹤器位置更新的規則，您可以改為使用下列模式：

  ```
  {
    "source": ["aws.geo"],
    "detail-type": ["Location Device Position Event"]
  }
  ```

  您可以選擇新增`detail`標籤來僅指定 `ENTER`或 `EXIT`事件 （如果您的規則用於追蹤器位置更新，則只有單一 `EventType`，因此不需要對其進行篩選）：

  ```
  {
    "source": ["aws.geo"],
    "detail-type": ["Location Geofence Event"],
    "detail": {
      "EventType": ["ENTER"]
    }
  }
  ```

  您也可以選擇性地篩選位置或地理圍欄的屬性：

  ```
  {
    "source": ["aws.geo"],
    "detail-type": ["Location Geofence Event"],
    "detail": {
      "EventType": ["ENTER"],
      "GeofenceProperties": {
        "Type": "LoadingDock"
      },
      "PositionProperties": {
        "VehicleType": "Truck"
      }
    }
  }
  ```
+ 對於**選取目標**，選擇從 Amazon Location Service 接收事件時要採取的目標動作。

  例如，使用 Amazon Simple Notification Service (SNS) 主題在事件發生時傳送電子郵件或文字訊息。您首先需要使用 Amazon SNS 主控台建立 Amazon SNS 主題。如需詳細資訊，請參閱[將 Amazon SNS 用於使用者通知](https://docs.aws.amazon.com/sns/latest/dg/sns-user-notifications.html)。

**警告**  
最佳實務是確認已成功套用事件規則，或您的自動化動作可能無法如預期啟動。若要驗證事件規則，請啟動事件規則的條件。例如，模擬進入地理圍欄區域的裝置。

您也可以從 Amazon Location 擷取所有事件，只要排除 `detail-type`區段即可。例如：

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

**注意**  
相同的事件可能會交付超過一次。您可以使用事件 ID 來取消複製您收到的事件。

## Amazon Location Service 的 Amazon EventBridge 事件範例
<a name="example-event"></a>

以下是透過呼叫 開始輸入地理圍欄的事件範例`BatchUpdateDevicePosition`。

```
{
  "version": "0",
  "id": "aa11aa22-33a-4a4a-aaa5-example",
  "detail-type": "Location Geofence Event",
  "source": "aws.geo",
  "account": "636103698109",
  "time": "2020-11-10T23:43:37Z",
  "region": "eu-west-1",
  "resources": [
    "arn:aws:geo:eu-west-1:0123456789101:geofence-collection/GeofenceEvents-GeofenceCollection_EXAMPLE",
    "arn:aws:geo:eu-west-1:0123456789101:tracker/Tracker_EXAMPLE"
  ],
  "detail": {
    "EventType": "ENTER",
    "GeofenceId": "polygon_14",
    "DeviceId": "Device1-EXAMPLE",
    "SampleTime": "2020-11-10T23:43:37.531Z",
    "Position": [
      -123.12390073297821,
      49.23433613216247
    ],
    "Accuracy": {
      "Horizontal": 15.3
    },
    "GeofenceProperties": {
      "ExampleKey1": "ExampleField1",
      "ExampleKey2": "ExampleField2"
    },
    "PositionProperties": {
      "ExampleKey1": "ExampleField1",
      "ExampleKey2": "ExampleField2"
    }
  }
}
```

以下是透過呼叫 來結束地理圍欄的事件範例`BatchUpdateDevicePosition`。

```
{
  "version": "0",
  "id": "aa11aa22-33a-4a4a-aaa5-example",
  "detail-type": "Location Geofence Event",
  "source": "aws.geo",
  "account": "123456789012",
  "time": "2020-11-10T23:41:44Z",
  "region": "eu-west-1",
  "resources": [
    "arn:aws:geo:eu-west-1:0123456789101:geofence-collection/GeofenceEvents-GeofenceCollection_EXAMPLE",
    "arn:aws:geo:eu-west-1:0123456789101:tracker/Tracker_EXAMPLE"
  ],
  "detail": {
    "EventType": "EXIT",
    "GeofenceId": "polygon_10",
    "DeviceId": "Device1-EXAMPLE",
    "SampleTime": "2020-11-10T23:41:43.826Z",
    "Position": [
      -123.08569321875426,
      49.23766166742559
    ],
    "Accuracy": {
      "Horizontal": 15.3
    },
    "GeofenceProperties": {
      "ExampleKey1": "ExampleField1",
      "ExampleKey2": "ExampleField2"
    },
    "PositionProperties": {
      "ExampleKey1": "ExampleField1",
      "ExampleKey2": "ExampleField2"
    }
  }
}
```

以下是透過呼叫 啟動的位置更新事件範例`BatchUpdateDevicePosition`。

```
{
  "version": "0",
  "id": "aa11aa22-33a-4a4a-aaa5-example",
  "detail-type": "Location Device Position Event",
  "source": "aws.geo",
  "account": "123456789012",
  "time": "2020-11-10T23:41:44Z",
  "region": "eu-west-1",
  "resources": [
    "arn:aws:geo:eu-west-1:0123456789101:tracker/Tracker_EXAMPLE"
  ],
  "detail": {
    "EventType": "UPDATE",
    "TrackerName": "tracker_2",
    "DeviceId": "Device1-EXAMPLE",
    "SampleTime": "2020-11-10T23:41:43.826Z",
    "ReceivedTime": "2020-11-10T23:41:39.235Z",
    "Position": [
      -123.08569321875426,
      49.23766166742559
    ],
    "Accuracy": {
      "Horizontal": 15.3
    },
    "PositionProperties": {
      "ExampleKey1": "ExampleField1",
      "ExampleKey2": "ExampleField2"
    }
  }
}
```