

# Setup for Devices and Fleets in SageMaker Edge Manager
<a name="edge-device-fleet"></a>

Fleets are collections of logically grouped devices you can use to collect and analyze data. You can use SageMaker Edge Manager to operate machine learning models on a fleet of smart cameras, smart speakers, robots, and other edge devices.

Create a fleet and register your devices either programmatically with the AWS SDK for Python (Boto3) or through the SageMaker AI console.

**Topics**
+ [Create a Fleet](edge-device-fleet-create.md)
+ [Register a Device](edge-device-fleet-register.md)
+ [Check Status](edge-device-fleet-check-status.md)

# Create a Fleet
<a name="edge-device-fleet-create"></a>

You can create a fleet programmatically with the AWS SDK for Python (Boto3) or through the SageMaker AI console [https://console.aws.amazon.com/sagemaker](https://console.aws.amazon.com/sagemaker/).

## Create a Fleet (Boto3)
<a name="edge-device-fleet-create-boto3"></a>

Use the `CreateDeviceFleet` API to create a fleet. Specify a name for the fleet, your AWS IoT Role ARN for the `RoleArn` field, as well as an Amazon S3 URI where you want the device to store sampled data.

You can optionally include a description of the fleet, tags, and an AWS KMS Key ID.

```
import boto3

# Create SageMaker client so you can interact and manage SageMaker resources
sagemaker_client = boto3.client("sagemaker", region_name="aws-region")

sagemaker_client.create_device_fleet(
    DeviceFleetName="sample-fleet-name",
    RoleArn="arn:aws:iam::999999999:role/rolename", # IoT Role ARN
    Description="fleet description",
    OutputConfig={
        S3OutputLocation="s3://bucket/",
        KMSKeyId: "1234abcd-12ab-34cd-56ef-1234567890ab",
    },
        Tags=[
        {
            "Key": "string", 
            "Value" : "string"
         }
     ],
)
```

An AWS IoT Role Alias is created for you when you create a device fleet. The AWS IoT role alias provides a mechanism for connected devices to authenticate to AWS IoT using X.509 certificates and then obtain short-lived AWS credentials from an IAM role that is associated with the AWS IoT role alias.

Use `DescribeDeviceFleet` to get the role alias name and ARN.

```
# Print Amazon Resource Name (ARN) and alias that has access 
# to AWS Internet of Things (IoT).
sagemaker_client.describe_device_fleet(DeviceFleetName=device_fleet_name)['IotRoleAlias']
```

Use `DescribeDeviceFleet` API to get a description of fleets you created.

```
sagemaker_client.describe_device_fleet(
    DeviceFleetName="sample-fleet-name"
)
```

By default, it returns the name of the fleet, the device fleet ARN, the Amazon S3 bucket URI, the IAM role, the role alias created in AWS IoT, a timestamp of when the fleet was created, and a timestamp of when the fleet was last modified.

```
{ "DeviceFleetName": "sample-fleet-name",
  "DeviceFleetArn": "arn:aws:sagemaker:us-west-2:9999999999:device-fleet/sample-fleet-name",
  "IAMRole": "arn:aws:iam::999999999:role/rolename",
  "Description": "this is a sample fleet",
  "IoTRoleAlias": "arn:aws:iot:us-west-2:9999999999:rolealias/SagemakerEdge-sample-fleet-name"
  "OutputConfig": {
              "S3OutputLocation": "s3://bucket/folder",
              "KMSKeyId": "1234abcd-12ab-34cd-56ef-1234567890ab"
   },
   "CreationTime": "1600977370",
   "LastModifiedTime": "1600977370"}
```

## Create a Fleet (Console)
<a name="edge-device-fleet-create-console"></a>

You can create a Edge Manager packaging job using the Amazon SageMaker AI console at [https://console.aws.amazon.com/sagemaker](https://console.aws.amazon.com/sagemaker/).

1. In the SageMaker AI console, choose **Edge Manager** and then choose **Edge device fleets**.

1. Choose **Create device fleet**.  
![\[The locaiton of the Create device fleet in the console.\]](http://docs.aws.amazon.com/sagemaker/latest/dg/images/smith/create-device-button-edited.png)

1. Enter a name for the device fleet in the **Device fleet name** field. Choose **Next**.  
![\[The location of the Next button in the Device fleet properties section in the console.\]](http://docs.aws.amazon.com/sagemaker/latest/dg/images/smith/create-device-fleet-filled.png)

1. On the **Output configuration** page, specify the Amazon S3 bucket URI where you want to store sample data from your device fleet. You can optionally add an encryption key as well by electing an existing AWS KMS key from the dropdown list or by entering a key’s ARN. Choose **Submit**.  
![\[Example Output configuration page in the console.\]](http://docs.aws.amazon.com/sagemaker/latest/dg/images/smith/create-device-fleet-output-filled.png)

1. Choose the name of your device fleet to be redirected to the device fleet details. This page displays the name of the device fleet, ARN, description (if you provided one), date the fleet was created, last time the fleet was modified, Amazon S3 bucket URI, AWS KMS key ID (if provided), AWS IoT alias (if provided), and IAM role. If you added tags, they appear in the **Device fleet tags** section.

# Register a Device
<a name="edge-device-fleet-register"></a>

**Important**  
Device registration is required to use any part of SageMaker Edge Manager.

You can create a fleet programmatically with the AWS SDK for Python (Boto3) or through the SageMaker AI console at [https://console.aws.amazon.com/sagemaker](https://console.aws.amazon.com/sagemaker/).

## Register a Device (Boto3)
<a name="edge-device-fleet-register-boto3"></a>

To register your device, first create and register an AWS IoT thing object and configure an IAM role. SageMaker Edge Manager takes advantage of the AWS IoT Core services to facilitate the connection between the edge devices and the cloud. You can take advantage of existing AWS IoT functionality after you set up your devices to work with Edge Manager.

To connect your device to AWS IoT you need to create AWS IoT thing objects, create and register a client certificate with AWS IoT, and create and configure IAM role for your devices.

See the [Getting Started Guide](https://docs.aws.amazon.com/sagemaker/latest/dg/edge-manager-getting-started.html) for an in-depth example or the [Explore AWS IoT Core services in hands-on tutorial](https://docs.aws.amazon.com/iot/latest/developerguide/iot-gs-first-thing.html).

Use the `RegisterDevices` API to register your device. Provide the name of the fleet of which you want the devices to be a part, as well as a name for the device. You can optionally add a description to the device, tags, and AWS IoT thing name associated with the device.

```
sagemaker_client.register_devices(
    DeviceFleetName="sample-fleet-name",
    Devices=[
        {          
            "DeviceName": "sample-device-1",
            "IotThingName": "sample-thing-name-1",
            "Description": "Device #1"
        }
     ],
     Tags=[
        {
            "Key": "string", 
            "Value" : "string"
         }
     ],
)
```

## Register a Device (Console)
<a name="edge-device-fleet-register-console"></a>

You can register your device using the SageMaker AI console at [https://console.aws.amazon.com/sagemaker](https://console.aws.amazon.com/sagemaker/).

1. In the SageMaker AI console, choose **Edge Inference** and then choose **Edge devices**.

1. Choose **Register devices**.  
![\[Location of Register devices in the Edge Devices section of the console.\]](http://docs.aws.amazon.com/sagemaker/latest/dg/images/smith/register-device-button.png)

1. In the **Device properties** section, enter the name of the fleet the device belongs to under the **Device fleet name** field. Choose **Next**.  
![\[The Device properties section in the console.\]](http://docs.aws.amazon.com/sagemaker/latest/dg/images/smith/register-devices-empty.png)

1. In the **Device source** section, add your devices one by one. You must include a **Device Name** for each device in your fleet. You can optionally provide a description (in the **Description** field) and an Internet of Things (IoT) object name (in the **IoT name** field). Choose **Submit** once you have added all your devices.  
![\[The Device source section in the console.\]](http://docs.aws.amazon.com/sagemaker/latest/dg/images/smith/register-devices-device-source.png)

   The **Devices** page displays the name of the device you have added, the fleet to which it belongs, when it was registered, the last heartbeat, and the description and AWS IoT name, if you provided one.

   Choose a device to view the device’s details, including the device name, fleet, ARN, description, IoT Thing name, when the device was registered, and the last heartbeat.

# Check Status
<a name="edge-device-fleet-check-status"></a>

Check that your device or fleet is connected and sampling data. Making periodic checks, manually or automatically, allows you to check that your device or fleet is working properly.

Use the Amazon S3 console at [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/) to interactively choose a fleet for a status check. You can also use the AWS SDK for Python (Boto3). The following describes different APIs from Boto3 you can use to check the status of your device or fleet. Use the API that best fits your use case.
+ **Check an individual device.**

  To check the status of an individual device, use `DescribeDevice` API. A list containing one or more models is provided if a models have been deployed to the device.

  ```
  sagemaker_client.describe_device(
      DeviceName="sample-device-1",
      DeviceFleetName="sample-fleet-name"
  )
  ```

  Running `DescribeDevice` returns:

  ```
  { "DeviceName": "sample-device".
    "Description": "this is a sample device",
    "DeviceFleetName": "sample-device-fleet",
    "IoTThingName": "SampleThing",
    "RegistrationTime": 1600977370,
    "LatestHeartbeat": 1600977370,
    "Models":[
          {
           "ModelName": "sample-model", 
           "ModelVersion": "1.1",
           "LatestSampleTime": 1600977370,
           "LatestInference": 1600977370 
          }
     ]
  }
  ```
+ **Check a fleet of devices.**

  To check the status of the fleet, use the `GetDeviceFleetReport` API. Provide the name of the device fleet to get a summary of the fleet.

  ```
  sagemaker_client.get_device_fleet_report(
      DeviceFleetName="sample-fleet-name"
  )
  ```
+ **Check for a heartbeat.**

  Each device within a fleet periodically generates a signal, or “heartbeat”. The heartbeat can be used to check that the device is communicating with Edge Manager. If the timestamp of the last heartbeat is not being updated, the device may be failing.

  Check the last heartbeat with made by a device with the `DescribeDevice` API. Specify the name of the device and the fleet to which the edge device belongs.

  ```
  sagemaker_client.describe_device(
      DeviceName="sample-device-1",
      DeviceFleetName="sample-fleet-name"
  )
  ```