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

Social Media Marketing: A Key to Business Success with Easy Digital Life

In today's digital-first world, businesses must adopt effective strategies to stay competitive. Social media marketing…

5 hours ago

Best 7 AI Tools Every UI/UX Designer Should Know About

62% of UX designers now use AI to enhance their workflows. Artificial intelligence (AI) rapidly…

2 days ago

How AI Enhances Photoshop Workflow: A Beginner’s Guide

The integration of artificial intelligence into graphic design through tools like Adobe Photoshop can save…

2 weeks ago

The Rise Of Crypto Trading Bots: A New Era In Digital Trading

The cryptocurrency trading world has grown significantly in recent years, with automation playing a key…

3 weeks ago

Real-World Insights on White-Label NFT Marketplace Development

The non-fungible token (NFT) market has witnessed explosive growth over the past few years, transforming…

3 weeks ago

Finding the Right Time to Build Your Software Instead of Buy

There are few things as valuable to a business as well-designed software. Organizations today rely…

1 month ago

This website uses cookies.