

# Manage data streams for AWS IoT SiteWise
<a name="manage-data-streams"></a>

 A data stream is the resource that contains historical time series data. Each data stream is identified by a unique alias, making it easier to keep track of the origin for each piece of data. Data streams are automatically created in AWS IoT SiteWise when the first time series data is received. If the first time series data is identified with an alias, AWS IoT SiteWise creates a new data stream with that alias, provided no asset properties are already assigned that alias. Alternatively, if the first time series data is identified with an asset ID and property ID, AWS IoT SiteWise creates a new data stream and associates that data stream with the asset property. 

 There are two ways to assign an alias to an asset property. The method used depends on if data is sent to AWS IoT SiteWise first, or an asset is created first.
+  If data is sent to AWS IoT SiteWise first, this automatically creates a data stream with the assigned alias. When the asset is created later, use the [ AssociateTimeSeriesToAssetProperty](https://docs.aws.amazon.com/iot-sitewise/latest/APIReference/API_AssociateTimeSeriesToAssetProperty.html) API to associate the data stream and its alias to the asset property. 
+  If an asset is created first, use the [ UpdateAssetProperty](https://docs.aws.amazon.com/iot-sitewise/latest/APIReference/API_UpdateAssetProperty.html) API to assign an alias to an asset property. When data is later sent to AWS IoT SiteWise, the data stream is automatically created and associated with the asset property. 

Currently, you can only associate data streams with measurements. *Measurements* are a type of asset property that represent devices' raw sensor data streams, such as timestamped temperature values or timestamped rotations per minute (RPM) values.

When these measurements define metrics or transformations, the incoming data triggers specific calculations. It’s important to note that an asset property can only be linked to one data stream at a time.

AWS IoT SiteWise uses `TimeSeries` for the Amazon Resource Name (ARN) resource to determine your storage charges. For more information, see [AWS IoT SiteWise Pricing](https://aws.amazon.com/iot-sitewise/pricing/).

The following sections show you how to use the AWS IoT SiteWise console or API to manage data streams.

**Topics**
+ [Configure permissions and settings](manage-data-streams-configuration.md)
+ [Associate a data stream to an asset property](manage-data-streams-method.md)
+ [Disassociate a data stream from an asset property](disassociate-data-streams-method.md)
+ [Delete a data stream](delete-data-streams-method.md)
+ [Update an asset property alias](update-data-streams-method.md)
+ [Common scenarios](data-ingestion-scenarios.md)

# Configure permissions and settings
<a name="manage-data-streams-configuration"></a>

 Data streams are automatically created in AWS IoT SiteWise when the first time series data is received. If the data ingested is not associated with an asset property, AWS IoT SiteWise creates a new disassociated data stream which is configurable to be associated with an asset property. Configure the access control of the gateway sending data to AWS IoT SiteWise, using IAM policies to specify the type of data to be ingested. 

 The following IAM policy disables disassociated data ingestion from the gateway, while still allowing data ingestion to data streams associated with an asset property: 

**Example IAM user policy that disables disassociated data ingestion from the gateway**    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
    {
      "Sid": "AllowPutAssetPropertyValuesUsingAssetIdAndPropertyId",
      "Effect": "Allow",
      "Action": "iotsitewise:BatchPutAssetPropertyValue",
      "Resource": "arn:aws:iotsitewise:*:*:asset/*"
    },
    {
      "Sid": "AllowPutAssetPropertyValuesUsingAliasWithAssociatedAssetProperty",
      "Effect": "Allow",
      "Action": "iotsitewise:BatchPutAssetPropertyValue",
      "Resource": "arn:aws:iotsitewise:*:*:time-series/*",
      "Condition": {
        "StringLikeIfExists": {
          "iotsitewise:isAssociatedWithAssetProperty": "true"
        }
      }
    },
    {
      "Sid": "DenyPutAssetPropertyValuesUsingAliasWithNoAssociatedAssetProperty",
      "Effect": "Deny",
      "Action": "iotsitewise:BatchPutAssetPropertyValue",
      "Resource": "arn:aws:iotsitewise:*:*:time-series/*",
      "Condition": {
        "StringLikeIfExists": {
          "iotsitewise:isAssociatedWithAssetProperty": "false"
        }
      }
    }
  ]
}
```

**Example IAM user policy that disables all data ingestion from the gateway**    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "DenyPutAssetPropertyValues",
            "Effect": "Deny",
            "Action": "iotsitewise:BatchPutAssetPropertyValue",
            "Resource": [
                "arn:aws:iotsitewise:*:*:asset/*",
                "arn:aws:iotsitewise:*:*:time-series/*"
            ]
        }
    ]
}
```

# Associate a data stream to an asset property
<a name="manage-data-streams-method"></a>

Manage your data streams using the AWS IoT SiteWise console or AWS CLI.

------
#### [ Console ]

Use the AWS IoT SiteWise console to manage your data streams.

**To manage data streams (console)**

1. <a name="sitewise-open-console"></a>Navigate to the [AWS IoT SiteWise console](https://console.aws.amazon.com/iotsitewise/).

1. In the navigation pane, choose **Data streams**.

1. Choose a data stream by either filtering on data stream alias, or selecting **Disassociated data streams** in the filter drop down menu.

1. Select the data stream to update. You may select multiple data streams. Click **Manage data streams** on the upper right. 

1. Select the data stream to be associated from **Update data stream associations**, and click the **Choose measurement** button.

1.  In the **Choose measurement** section, find the corresponding asset measurement property. Select the measurement then click **Choose**. 

1.  Perform steps 4 and 5 for other data streams selected in step 3. Assign asset properties to all the data streams. 

1.  Choose **Update** to commit the changes. A successful confirmation banner is displayed to confirm the update. 

------
#### [ AWS CLI ]

 To associate a data stream (identified by its alias) to an asset property (identified by its IDs), run the following command: 

```
aws iotsitewise associate-time-series-to-asset-property \ 
    --alias <data-stream-alias> \
    --assetId <asset-ID> \
    --propertyId <property-ID>
```

------

# Disassociate a data stream from an asset property
<a name="disassociate-data-streams-method"></a>

------
#### [ Console ]

Use the AWS IoT SiteWise console to disassociate your data stream from an asset property.

**To disassociate data streams from an asset property (console)**

1. <a name="sitewise-open-console"></a>Navigate to the [AWS IoT SiteWise console](https://console.aws.amazon.com/iotsitewise/).

1. In the navigation pane, choose **Data streams**.

1. Choose a data stream by either filtering on data stream alias, or selecting **Associated data streams** in the filter drop down menu.

1. Select the data stream to disassociate. The **Data stream alias** column must contain an alias. The **Asset name** and **Asset property name** columns must contain the values of the asset property the data stream is associated with. You can select multiple data streams.

1.  Click **Manage data streams** on the upper right. 

1.  In the **Update data stream associations** section, click **X** in the **Measurement name** column. A `submitted` status should appear in the **Status** column. 

1.  Choose **Update** to commit the changes. The data stream is now disassociated from the asset property, and the alias is now used to identify the data stream. 

------
#### [ AWS CLI ]

To disassociate a data stream from an asset property, (identified by its `ID`s and its alias), run the following command: 

```
    aws iotsitewise disassociate-time-series-from-asset-property \ 
        --alias <asset-property-alias> \
        --assetId <asset-ID> \
        --propertyId <property-ID>
```

 The data stream is now disassociated from the asset property, and the alias is used to identify the data stream. The alias is no longer associated with the asset property, as it is now associated with the data stream. 

------

# Delete a data stream
<a name="delete-data-streams-method"></a>

 When a property is removed from an asset model, AWS IoT SiteWise deletes the properties and their data streams from all assets that are managed by the asset model. It also deletes all properties and their data streams of an asset when the asset is deleted. If a data stream data must be preserved, it must be disassociated from the asset property before it is deleted. 

**Warning**  
 When a property is deleted from an asset, the associated data stream is also deleted. To preserve the data stream, disassociate it from the asset property first, before deleting the property from the asset model, or deleting the asset. 

------
#### [ Console ]

Use the AWS IoT SiteWise console to disassociate your data stream from an asset property.

**To delete a data stream (console)**

1. <a name="sitewise-open-console"></a>Navigate to the [AWS IoT SiteWise console](https://console.aws.amazon.com/iotsitewise/).

1.  In the navigation pane, choose **Data streams**. 

1.  Choose a data stream by filtering on data stream alias. 

1.  Select the data stream to delete. You may select multiple data streams. 

1.  Choose the **Delete** button to delete the data stream. 

------
#### [ AWS CLI ]

 Use the [ DeleteTimeSeries](https://docs.aws.amazon.com/iot-sitewise/latest/APIReference/API_DeleteTimeSeries.html) API to delete a specific data stream, by its alias. 

```
    aws iotsitewise delete-time-series \ 
        --alias <data-stream-alias>
```

------

# Update an asset property alias
<a name="update-data-streams-method"></a>

 Aliases must be unique within an AWS region. This includes aliases of both asset properties and data streams. Do not assign an alias to an asset property, if another property or data stream is using that alias. 

------
#### [ Console ]

Use the AWS IoT SiteWise console to update an asset property alias.

**To update an asset property alias (console)**

1. <a name="sitewise-open-console"></a>Navigate to the [AWS IoT SiteWise console](https://console.aws.amazon.com/iotsitewise/).

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

1.  Select the asset from the table. 

1.  Click the **Edit** button. 

1.  Select the **Property type** in the **Properties** table. 

1.  Find the property, and type the new alias in the property alias text field. 

1.  Click the **Save** button to save the changes. 

------
#### [ AWS CLI ]

 To update an alias on an asset property, run the following command: 

```
    aws iotsitewise update-asset-property \
        --asset-id <asset-ID> \
        --property-id <property-ID> \
        --property-alias <asset-property-alias> \
        --property-notification-state <ENABLED|DISABLED>
```

**Note**  
 If property notifications are currently enabled, it must be provided again to ensure it continues to be enabled. 

------

# Common scenarios
<a name="data-ingestion-scenarios"></a>

## Move a data stream
<a name="data-ingestion-scenario-move-data-stream"></a>

 To change a data stream’s association to another asset property, first disassociate the data stream from the current asset property. When disassociating a data stream from an asset property, there must be an alias assigned to that asset property. 

```
    aws iotsitewise disassociate-time-series-from-asset-property \ 
        --alias <asset-property-alias> \
        --assetId <asset-ID> \
        --propertyId <property-ID>
```

 Now re-assign the data stream to the new asset property. 

```
    aws iotsitewise associate-time-series-from-asset-property \ 
        --alias <data-stream-alias> \
        --assetId <new-asset-ID> \
        --propertyId <new-property-ID>
```

## Error when assigning an alias to an asset property
<a name="data-ingestion-scenario-assetid-contains-data"></a>

 When using the `UpdateAssetProperty` API to assign an alias to a property, you may see the following error message: 

```
Given alias <data-stream-alias> for property <property-name> with ID <property-ID> already in use by another property or data stream
```

 This error message indicates the alias is not assigned to the property, because it is currently used by another property or a data stream. 

 This happens if data is being ingested to AWS IoT SiteWise with an alias. When data is sent with an alias not being used by another data stream or asset property, a new data stream is created with that alias. The below two options resolve the issue. 
+  Use `AssociateTimeSeriesToAssetProperty` API to associate the data stream with its alias to the asset property. 
+  Temporarily stop the data ingestion and delete the data stream. Use `UpdateAssetProperty` API to assign the alias to the asset property, and then turn data ingestion back on. 

## Error when associating a data stream to an asset property
<a name="data-ingestion-scenario-move-data-stream"></a>

 When associating a data stream to an asset property, the following error message is seen. 

```
assetProperty <property-name> with assetId <asset-ID> propertyId <property-ID> contains data
```

 This error message indicates the asset property already is associated with a data stream containing data. That data stream must be disassociated or deleted, before associating an other data stream to that asset property. 

**Note**  
 When disassociating a data stream from an asset property, the alias assigned to the property is given to the data stream. For that alias to remain assigned to the property, assign a new alias to that property before disassociating the data stream. 

 To preserve the data stored in the asset property do the following: 
+  Ensure no data is being ingested to the asset property, to prevent creating a new data stream. 
+  Use `UpdateAssetProperty` API to set a new alias that is given to the currently assigned data stream. 
+  Use `DisassociateTimeSeriesFromAssetProperty` API to disassociate the current data stream from the asset property. 
+  Use `AssociateTimeSeriesToAssetProperty` API to associate the desired data stream to the asset property. 

 If the data stored in the asset property must be deleted, do the following: 
+  Ensure no data is being ingested to the asset property, to prevent creating a new data stream. 
+  Use `DeleteTimeSeries` API to delete the currently assigned data stream. 
+  Use `AssociateTimeSeriesToAssetProperty` API to associate the desired data stream to the asset property. 