Cloud Computing

Serverless tutorial – On HTTP trigger store data in Azure Cosmos DB

In the last tutorial, we have created a new hello world function from the Azure portal. Azure Functions lets you execute your code in a serverless environment without having to first create a VM or publish a web application. In Azure Functions, input and output bindings provide a declarative way to connect to external service data from your function.

In this tutorial, we are going to check how to update the function we have created to have an output binding to store the data. Azure Functions allows writing code in response to events in Azure and other services, through triggers and bindings.

Triggers and Bindings

Triggers and bindings are a declarative way to define how a function is invoked and what data it works with. A Trigger defines how a function is invoked. Triggers have associated data, which is usually the payload that triggered the function.

Input and output bindings provide a declarative way to connect to data from within your code. We can specify connection strings and other properties in function configuration. Bindings are optional and a function can have multiple inputs and output bindings.

Using triggers and bindings, we can write code that is more generic and does not hardcode the details of the services with which it interacts. Data coming from services become input values for the function code.

To output data to another service (such as creating/updation of a new row in Azure Table Storage), use the return value of the method.

Some of the input and output bindings are given below :

  • Blob Storage
  • Cosmos DB
  • Event Hubs
  • External File
  • External Table
  • HTTP
  • Microsoft Graph
  • Excel tables
  • Microsoft Graph
  • OneDrive files
  • Microsoft Graph
  • Outlook email
  • Microsoft Graph
  • Events
  • Mobile Apps
  • Notification Hubs
  • Queue storage
  • SendGrid
  • Service Bus
  • Table storage
  • Twilio
  • Webhooks

Now that we have learned about triggers, input, and output bindings, our next step would be to use the same function that we created earlier to add an output binding that stores unstructured data in a Cosmos DB document storage.

Azure Cosmos DB is Microsoft’s globally distributed, multi-model database. With the click of a button, Azure Cosmos DB enables you to elastically and independently scale throughput and storage across any number of Azure’s geographic regions. It offers throughput, latency, availability, and consistency guarantees with comprehensive service level agreements (SLAs), something no other database service can offer.

You can Try Azure Cosmos DB for free without an Azure subscription, free of charge, and commitments.

Azure Cosmos DB

Image – Azure Cosmos DB

As a globally distributed database service, Azure Cosmos DB provides the following capabilities to help you build scalable, highly responsive applications:

Key Capabilities

  • Turnkey global distribution – You can distribute your data to any number of Azure regions, with the click of a button. This enables you to put your data where your users are, ensuring the lowest possible latency to your customers.
  • Multiple data models and popular APIs for accessing and querying data – APIs for the following data models are supported with SDKs available in multiple languages:
    • DocumentDB API: A schema-less JSON database engine with SQL querying capabilities.
    • MongoDB API: A MongoDB database service built on top of Cosmos DB.
    • Table API: A key-value database service built to provide premium capabilities for Azure Table storage applications.
    • Graph (Gremlin) API: A graph database service built following the Apache TinkerPop specification.
    • Cassandra API: A key/value store built on the Apache Cassandra implementation.
  • Elastically scale throughput and storage on demand, worldwide
  • Support for highly responsive and mission-critical applications
  • “always on” availability
  • No database schema/index management

The best use case for Azure Cosmos DB would be any web, mobile, gaming, and IoT applications that need to handle massive amounts of reads and writes on a global scale with low response times for a variety of data will benefit from Azure Cosmos DB’s guaranteed availability, high throughput, low latency, and tunable consistency.

The next step would be to use the same function that we created earlier to add an output binding that stores unstructured data in a Cosmos DB document storage.

Step #1 : Add Output binding to the existing function

  • Once you login to the Azure management portal, under the function app expand your function.

    Image – Azure portal

  • Select Integrate option (under Function app)

    Image – Azure Function App

  • Click on New Output under Outputs drop-down (top right corner)

    Image – Azure Function

  • Use Azure Cosmos DB as the output binding

    Image – Azure Function

Step #2: Create new Azure Cosmos DB

  • Select New next to the Azure Cosmos DB document connection label, and select + Create new.
  • Use the New account settings as specified in the table:
    Image – Create new Azure Cosmos DB

    Image – updated output binding

Image – Update the Function code

Step #3: Verify if data has been added to Cosmos DB

Image – Azure Cosmos DB

 

Image – Data in Azure Cosmos DB

Step #4: Clean up

We have created resources in a resource group. If these resources are not needed, we can delete them by deleting the resource group or respective resources.

In this tutorial, we have covered how to use the function to have an output binding and to store the data in Azure Cosmos DB.

Like this post? Don’t forget to share it!

Additional Resources :

Summary
Article Name
Serverless tutorial - On HTTP trigger store data in Azure Cosmos DB
Description
In this tutorial, we are going check how to update the function we have created to have an output binding to store the data.
Author
Publisher Name
Upnxtblog
Publisher Logo
Karthik

Allo! My name is Karthik,experienced IT professional.Upnxtblog covers key technology trends that impacts technology industry.This includes Cloud computing,Blockchain,Machine learning & AI,Best mobile apps, Best tools/open source libs etc.,I hope you would love it and you can be sure that each post is fantastic and will be worth your time.

Share
Published by
Karthik

Recent Posts

Innovators in Crypto: Prominent AI-Powered Coins

The cryptocurrency industry is being reshaped by the fusion of blockchain technology and artificial intelligence…

5 days ago

Top AI Design Tools Every Graphic Designer Should Use in 2024

Introduction Artificial Intelligence (AI) has also found its relevance in graphic design and is quickly…

1 month ago

Transforming Industries: The Integration of AI and Blockchain

Imagine a world where the brilliance of Artificial Intelligence (AI) meets the unbreakable security of…

2 months ago

How Can I Use Automation to Streamline My Digital Marketing Efforts?

In today’s fast-paced digital landscape, automation is not just a luxury but a necessity for…

2 months ago

Top 5 AI Technologies Transforming the Casino Gaming Landscape in 2025

The world of casino gaming has leveraged the emerging technology advancements to create immersive and…

2 months ago

Choosing the Best Web Hosting for Your Small Business Needs

Selecting the right web hosting for your small business is crucial for ensuring a smooth…

2 months ago

This website uses cookies.