Create an Azure Function That Writes to CosmosDB

30 minutes
  • 6 Learning Objectives

About this Hands-on Lab

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.

  1. Open a browser.
  2. Navigate to the provided Azure Portal URL.
  3. Use the supplied username and password to authenticate.
Configure the CosmosDB Instance

Starting in the Azure dashboard, perform the following tasks:

  1. Open the navigation menu in the upper-left of the Portal.
  2. Click on All resources. Wait for all the resources to appear.
  3. In the list, click on the CosmosDB instance that has a name starting with cdb-. This opens the overview page of the CosmosDB instance.
  4. Click the Data Explorer menu item.
  5. Click the New Container button on the Data Explorer page.
  6. 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:

  1. Open the navigation menu in the upper-left of the Portal.
  2. Click All resources. Wait for all the resources to appear.
  3. 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.
  4. Click the Functions menu item in the navigation tree on the left of the page.
  5. Click New function near the top center of the page.
  6. From the list of trigger types, select HTTP trigger.
  7. 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.
  8. Click Create.
  9. Wait for the function to appear.
Create the CosmosDB Output Integration
  1. Click Integrate under the MyHttpFunction.
  2. Under Outputs, click + New Output.
  3. In the Azure CosmosDB output panel, set the following options:
    • Document parameter name: outputDocument
    • Database name: laazfuncs
    • Collection name: mydocs
  4. Click new near Azure Cosmos DB account connection.
  5. In the dialog that pops up, press the Select button.
  6. 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
  1. On the functions code page, click Save and Run to run the function.
  2. Go back to the CosmosDB Data Explorer, and refresh the collection. Verify the presence of a new document.

Additional Resources

Your employer would like to upload JSON documents from mobile devices and store them in CosmosDB. You are asked to prototype a solution using Azure Functions. You need to create an Azure Function that receives JSON via HTTP and uses output integration to write the JSON to CosmosDB.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!