Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Azure icon
Labs

Create an Azure Function That Writes to CosmosDB

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.

Azure icon
Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 30m
Published
Clock icon Feb 28, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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.
  2. Challenge

    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
  3. Challenge

    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.
  4. Challenge

    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
    1. Click new near Azure Cosmos DB account connection.
    2. In the dialog that pops up, press the Select button.
    3. Press the Save button to create the integration.
  5. Challenge

    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");
    }
    
    
  6. Challenge

    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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans