You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can use Azure Functions to deploy code that implements your business logic directly to your Azure IoT Edge devices. This tutorial walks you through creating and deploying an Azure Function that filters sensor data on the simulated IoT Edge device. You use the simulated IoT Edge device that you created in the quickstarts. In this tutorial, you learn how to:
19
+
Use Azure Functions to deploy code that runs your business logic directly on your Azure IoT Edge devices. This tutorial shows you how to create and deploy an Azure Function that filters sensor data on a simulated IoT Edge device. Use the simulated IoT Edge device you created in the quickstarts. In this tutorial, you learn how to:
20
20
21
21
> [!div class="checklist"]
22
22
>
23
-
> * Use Visual Studio Code to create an Azure Function.
24
-
> * Use Visual Studio Code and Docker to create a Docker image and publish it to a container registry.
25
-
> * Deploy the module from the container registry to your IoT Edge device.
26
-
> * View filtered data.
23
+
> * Use Visual Studio Code to create an Azure Function
24
+
> * Use Visual Studio Code and Docker to create a Docker image and publish it to a container registry
25
+
> * Deploy the module from the container registry to your IoT Edge device
26
+
> * View filtered data
27
27
28
-
:::image type="content" source="./media/tutorial-deploy-function/functions-architecture.png" alt-text="Diagram of function architecture, showing how to stage and deploy a function module.":::
28
+
:::image type="content" source="./media/tutorial-deploy-function/functions-architecture.png" alt-text="Diagram that shows function architecture, including how to stage and deploy a function module.":::
29
29
30
-
The Azure Function that you create in this tutorial filters the temperature data that's generated by your device. The Function only sends messages upstream to Azure IoT Hub when the temperature is above a specified threshold.
30
+
The Azure Function you create in this tutorial filters temperature data generated by your device. The function only sends messages upstream to Azure IoT Hub when the temperature is above a specified threshold.
Before beginning this tutorial, do the tutorial to set up your development environment for Linux container development: [Develop Azure IoT Edge modules using Visual Studio Code](tutorial-develop-for-linux.md). After completing that tutorial, you should have the following prerequisites in place:
36
+
Before you start this tutorial, follow the tutorial to set up your development environment for Linux container development: [Develop Azure IoT Edge modules using Visual Studio Code](tutorial-develop-for-linux.md). When you finish, you have the following prerequisites in place:
37
37
38
-
* A free or standard-tier [IoT Hub](../iot-hub/iot-hub-create-through-portal.md) in Azure.
39
-
* An AMD64 device running Azure IoT Edge with Linux containers. You can use the quickstart to set up a [Linux device](quickstart-linux.md) or [Windows device](quickstart.md).
40
-
* A container registry, like [Azure Container Registry](/azure/container-registry/).
41
-
*[Visual Studio Code](https://code.visualstudio.com/) configured with the [Azure IoT Edge](https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-edge) and
42
-
[Azure IoT Hub](https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-toolkit) extensions. The *Azure IoT Edge tools for Visual Studio Code* extension is in [maintenance mode](https://github.com/microsoft/vscode-azure-iot-edge/issues/639).
43
-
* Download and install a [Docker compatible container management system](support.md#container-engines) on your development machine. Configure it to run Linux containers.
38
+
* A free or standard-tier [IoT Hub](../iot-hub/iot-hub-create-through-portal.md) in Azure
39
+
* An AMD64 device running Azure IoT Edge with Linux containers. Use the quickstart to set up a [Linux device](quickstart-linux.md) or [Windows device](quickstart.md).
40
+
* A container registry, like [Azure Container Registry](/azure/container-registry/)
41
+
*[Visual Studio Code](https://code.visualstudio.com/) set up with the [Azure IoT Edge](https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-edge) and [Azure IoT Hub](https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-toolkit) extensions. The *Azure IoT Edge tools for Visual Studio Code* extension is in [maintenance mode](https://github.com/microsoft/vscode-azure-iot-edge/issues/639).
42
+
* Download and install a [Docker compatible container management system](support.md#container-engines) on your development machine. Set it to run Linux containers.
44
43
45
-
To develop an IoT Edge module with Azure Functions, install additional prerequisites on your development machine:
44
+
To develop an IoT Edge module with Azure Functions, install these additional prerequisites on your development machine:
46
45
47
-
*[C# for Visual Studio Code (powered by OmniSharp) extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp).
The Azure IoT Edge for Visual Studio Code that you installed in the prerequisites provides management capabilities and some code templates. In this section, you use Visual Studio Code to create an IoT Edge solution that contains an Azure Function.
51
+
Azure IoT Edge for Visual Studio Code gives you management capabilities and code templates. In this section, you use Visual Studio Code to create an IoT Edge solution with an Azure Function.
53
52
54
53
### Create a new project
55
54
56
-
Follow these steps to create a C# Function solution template that's customizable.
55
+
Follow these steps to create a customizable C# Function solution template.
57
56
58
57
1. Open Visual Studio Code on your development machine.
59
58
@@ -71,32 +70,32 @@ Follow these steps to create a C# Function solution template that's customizable
71
70
72
71
### Add your registry credentials
73
72
74
-
The environment file of your solution stores the credentials for your container registry and shares them with the IoT Edge runtime. The runtime needs these credentials to pull your private images onto your IoT Edge device.
73
+
The environment file in your solution stores the credentials for your container registry and shares them with the IoT Edge runtime. The runtime needs these credentials to pull your private images onto your IoT Edge device.
75
74
76
-
The IoT Edge extension in Visual Studio Code tries to pull your container registry credentials from Azure and populate them in the environment file. Check to see if your credentials are already in the file. If not, add them now:
75
+
The IoT Edge extension in Visual Studio Code tries to pull your container registry credentials from Azure and populate them in the environment file. Check if your credentials are already in the file. If not, add them now:
77
76
78
77
1. In the Visual Studio Code explorer, open the `.env` file.
79
-
2. Update the fields with the **username** and **password** values that you copied from your Azure container registry. You can find them again by going to your container registry in Azure and looking on the **Settings** > **Access keys** page.
78
+
1. Update the fields with the **username** and **password** values you copied from your Azure container registry. To find them again, go to your container registry in Azure and look on the **Settings** > **Access keys** page.
80
79
3. Save this file.
81
80
82
81
>[!NOTE]
83
-
>This tutorial uses admin login credentials for Azure Container Registry, which are convenient for development and test scenarios. When you're ready for production scenarios, we recommend a least-privilege authentication option like service principals. For more information, see [Manage access to your container registry](production-checklist.md#manage-access-to-your-container-registry).
82
+
>This tutorial uses admin login credentials for Azure Container Registry, which are convenient for development and test scenarios. For production, use a least-privilege authentication option like service principals. For more information, see [Manage access to your container registry](production-checklist.md#manage-access-to-your-container-registry).
84
83
85
84
### Set target architecture to AMD64
86
85
87
-
Running Azure Functions modules on IoT Edge is supported only on Linux AMD64based containers. The default target architecture for Visual Studio Code is Linux AMD64, but we set it explicitly to Linux AMD64 here.
86
+
Azure Functions modules on IoT Edge are supported only on Linux AMD64-based containers. The default target architecture for Visual Studio Code is Linux AMD64, but you set it explicitly to Linux AMD64 here.
88
87
89
88
1. Open the command palette and search for **Azure IoT Edge: Set Default Target Platform for Edge Solution**.
90
89
91
90
2. In the command palette, select the AMD64 target architecture from the list of options.
92
91
93
92
### Update the module with custom code
94
93
95
-
Let's add some additional code so your **CSharpFunction** module processes the messages at the edge before forwarding them to IoT Hub.
94
+
Add some code so your **CSharpFunction** module processes messages at the edge before forwarding them to IoT Hub.
96
95
97
96
1. In the Visual Studio Code explorer, open **modules** > **CSharpFunction** > **CSharpFunction.cs**.
98
97
99
-
1. Replace the contents of the **CSharpFunction.cs** file with the following code. This code receives telemetry about ambient and machine temperature, and only forwards the message on to IoT Hub if the machine temperature is above a defined threshold.
98
+
1. Replace the contents of the **CSharpFunction.cs** file with the following code. This code receives telemetry about ambient and machine temperature, and forwards the message to IoT Hub only if the machine temperature is above a defined threshold.
100
99
101
100
```csharp
102
101
usingSystem;
@@ -173,60 +172,60 @@ Let's add some additional code so your **CSharpFunction** module processes the m
173
172
174
173
## Build and push your IoT Edge solution
175
174
176
-
In the previous section, you created an IoT Edge solution and modified the **CSharpFunction** to filter out messages with reported machine temperatures below the acceptable threshold. Now you need to build the solution as a container image and push it to your container registry.
175
+
In the previous section, you created an IoT Edge solution and changed the **CSharpFunction** to filter out messages with reported machine temperatures below the acceptable threshold. Now build the solution as a container image and push it to your container registry.
177
176
178
-
1. Open the Visual Studio Code integrated terminal by selecting**View** > **Terminal**.
177
+
1. Open the Visual Studio Code integrated terminal. Select**View** > **Terminal**.
179
178
180
-
2. Sign in to Docker by entering the following command in the terminal. Sign in with the username, password, and login server from your Azure container registry. You can retrieve these values from the **Access keys** section of your registry in the Azure portal.
179
+
1. Sign in to Docker in the terminal. Use the username, password, and login server from your Azure container registry. Get these values from the **Access keys** section of your registry in the Azure portal.
You may receive a security warning recommending the use of `--password-stdin`. While that best practice is recommended for production scenarios, it's outside the scope of this tutorial. For more information, see the [docker login](https://docs.docker.com/engine/reference/commandline/login/#provide-a-password-using-stdin) reference.
185
+
You can receive a security warning recommending the use of `--password-stdin`. While that best practice is recommended for production scenarios, it's outside the scope of this tutorial. For more information, see the [docker login](https://docs.docker.com/engine/reference/commandline/login/#provide-a-password-using-stdin) reference.
187
186
188
-
3. In the Visual Studio Code explorer, right-click the **deployment.template.json** file and select **Build and Push IoT Edge Solution**.
187
+
1. In the Visual Studio Code explorer, right-click the **deployment.template.json** file, then select **Build and Push IoT Edge Solution**.
189
188
190
-
The build and push command starts three operations. First, it creates a new folder in the solution called **config** that holds the full deployment manifest, which is built out of information in the deployment template and other solution files. Second, it runs `docker build` to build the container image based on the appropriate dockerfile for your target architecture. Then, it runs `docker push` to push the image repository to your container registry.
189
+
The build and push command starts three operations. First, it creates a new folder in the solution called **config** that has the full deployment manifest, which is built from the deployment template and other solution files. Second, it runs `docker build` to build the container image based on the appropriate Dockerfile for your target architecture. Then, it runs `docker push` to push the image repository to your container registry.
191
190
192
-
This process may take several minutes the first time, but is faster the next time that you run the commands.
191
+
This process can take several minutes the first time, but it's faster the next time you run the commands.
193
192
194
193
## View your container image
195
194
196
-
Visual Studio Code outputs a success message when your container image is pushed to your container registry. If you want to confirm the successful operation for yourself, you can view the image in the registry.
195
+
Visual Studio Code shows a success message when your container image is pushed to your container registry. To confirm the operation, view the image in the registry.
197
196
198
-
1. In the Azure portal, browse to your Azure container registry.
197
+
1. In the Azure portal, go to your Azure container registry.
199
198
2. Select **Services** > **Repositories**.
200
-
3. You should see the **csharpfunction** repository in the list. Select this repository to see more details.
201
-
4. In the **Tags** section, you should see the **0.0.1-amd64** tag. This tag indicates the version and platform of the image that you built. These values are set in the module.json file in the CSharpFunction folder.
199
+
3. You see the **csharpfunction** repository in the list. Select this repository to view more details.
200
+
4. In the **Tags** section, you see the **0.0.1-amd64** tag. This tag shows the version and platform of the image you built. These values are set in the *module.json* file in the *CSharpFunction* folder.
202
201
203
202
## Deploy and run the solution
204
203
205
-
You can use the Azure portal to deploy your Function module to an IoT Edge device like you did in the quickstart. You can also deploy and monitor modules from within Visual Studio Code. The following sections use the Azure IoT Edge and IoT Hub for Visual Studio Code that was listed in the prerequisites. Install the extensions now, if you haven't already.
204
+
Use the Azure portal to deploy your Function module to an IoT Edge device like in the quickstart. You can also deploy and monitor modules from Visual Studio Code. The following sections use the Azure IoT Edge and IoT Hub for Visual Studio Code extensions listed in the prerequisites. Install the extensions now if you haven't already.
206
205
207
-
1. In the Visual Studio Code explorer, under the **Azure IoT Hub** section, expand **Devices** to see your list of IoT devices.
206
+
1. In the Visual Studio Code explorer, under the **Azure IoT Hub** section, expand **Devices** to see the list of IoT devices.
208
207
209
208
2. Right-click the name of your IoT Edge device, and then select **Create Deployment for Single Device**.
210
209
211
-
3.Browse to the solution folder that contains the **CSharpFunction**. Open the config folder, select the **deployment.amd64.json** file, and then choose **Select Edge Deployment Manifest**.
210
+
3.Go to the solution folder that has the **CSharpFunction**. Open the config folder, select the **deployment.amd64.json** file, and then choose **Select Edge Deployment Manifest**.
212
211
213
-
4. Under your device, expand **Modules** to see a list of deployed and running modules. Select the refresh button. You should see the new **CSharpFunction** running along with the **SimulatedTemperatureSensor** module and the **$edgeAgent** and **$edgeHub**.
212
+
4. Under your device, expand **Modules** to see a list of deployed and running modules. Select the refresh button. You can see the new **CSharpFunction** running along with the **SimulatedTemperatureSensor** module, **$edgeAgent**, and **$edgeHub**.
214
213
215
-
It may take a few moments for the new modules to show up. Your IoT Edge device has to retrieve its new deployment information from IoT Hub, start the new containers, and then report the status back to IoT Hub.
214
+
It can take a few moments for the new modules to show up. The IoT Edge device retrieves its new deployment information from IoT Hub, starts the new containers, and then reports the status back to IoT Hub.
216
215
217
216
:::image type="content" source="./media/tutorial-deploy-function/view-modules.png" alt-text="Screenshot showing how to view deployed modules in Visual Studio Code.":::
218
217
219
218
## View the generated data
220
219
221
-
You can see all of the messages that arrive at your IoT hub from all your devices by running **Azure IoT Hub: Start Monitoring Built-in Event Endpoint** in the command palette. To stop monitoring messages, run the command**Azure IoT Hub: Stop Monitoring Built-in Event Endpoint** in the command palette.
220
+
See all messages that arrive at your IoT hub from your devices by running **Azure IoT Hub: Start Monitoring Built-in Event Endpoint** in the command palette. To stop monitoring messages, run **Azure IoT Hub: Stop Monitoring Built-in Event Endpoint** in the command palette.
222
221
223
-
You can also filter the view to see all of the messages that arrive at your IoT hub from a specific device. Right-click the device in the **Azure IoT Hub** > **Devices** section of the Visual Studio Code explorer and select **Start Monitoring Built-in Event Endpoint**.
222
+
To filter the view and see messages from a specific device, right-click the device in the **Azure IoT Hub** > **Devices** section of the Visual Studio Code explorer, and select **Start Monitoring Built-in Event Endpoint**.
224
223
225
224
## Clean up resources
226
225
227
-
If you plan to continue to the next recommended article, you can keep the resources and configurations that you created and reuse them. You can also keep using the same IoT Edge device as a test device.
226
+
If you plan to continue to the next recommended article, keep the resources and configurations you created and reuse them. You can also keep using the same IoT Edge device as a test device.
228
227
229
-
Otherwise, you can delete the local configurations and the Azure resources that you created in this article to avoid charges.
228
+
Otherwise, delete the local configurations and the Azure resources you created in this article to avoid charges.
0 commit comments