In this hands-on lab, we use the Azure Portal to create an Azure Function that will receive JSON via HTTP and write the JSON to CosmosDB.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Log In to the Azure Portal
Log in to the Azure Portal using the username and password supplied by the lab.
- Open a browser.
- Navigate to the provided Azure Portal URL.
- Use the supplied username and password to authenticate.
- Configure the CosmosDB Instance
Starting in the Azure dashboard, perform the following tasks:
- Open the navigation menu in the upper-left of the Portal.
- Click on All resources. Wait for all the resources to appear.
- In the list, click on the CosmosDB instance that has a name starting with cdb-. This opens the overview page of the CosmosDB instance.
- Click the Data Explorer menu item.
- Click the New Container button on the Data Explorer page.
In the Add Container panel, enter the following and press the OK button.
- Database id: Create new / laazfuncs
- Container id: mydocs
- Partition key: /name
- Create the HTTP-Triggered Function
Navigate to the overview page of the provided Azure Function:
- Open the navigation menu in the upper-left of the Portal.
- Click All resources. Wait for all the resources to appear.
- In the list, click on the app service instance that has a name that starts with fa-. This open the overview page of the Azure Function app service.
- Click the Functions menu item in the navigation tree on the left of the page.
- Click New function near the top center of the page.
- From the list of trigger types, select HTTP trigger.
- In the New Function panel that appears on the right of the page, enter "MyHttpFunction" as the name and leave Function as the Authentication level.
- Click Create.
- Wait for the function to appear.
- Create the CosmosDB Output Integration
- Click Integrate under the MyHttpFunction.
- Under Outputs, click + New Output.
- In the Azure CosmosDB output panel, set the following options:
- Document parameter name: outputDocument
- Database name: laazfuncs
- Collection name: mydocs
- Click new near Azure Cosmos DB account connection.
- In the dialog that pops up, press the Select button.
- Press the Save button to create the integration.
- Modify the Function Code to Utilize the CosmosDB Integration
Replace the code for the function with the following which has an output parameter, sets that value to the request body, and makes the function not async due to the
out
parameter.#r "Newtonsoft.Json" using System.Net; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; using Newtonsoft.Json; public static IActionResult Run(HttpRequest req, out object outputDocument, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string name = req.Query["name"]; string requestBody = new StreamReader(req.Body).ReadToEnd(); outputDocument = requestBody; dynamic data = JsonConvert.DeserializeObject(requestBody); name = name ?? data?.name; return name != null ? (ActionResult)new OkObjectResult($"Hello, {name}") : new BadRequestObjectResult("Please pass a name on the query string or in the request body"); }
- Run the Function and Check the CosmosDB Collection for the Document
- On the functions code page, click Save and Run to run the function.
- Go back to the CosmosDB Data Explorer, and refresh the collection. Verify the presence of a new document.