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

In the last tutorial we have created 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 check how to update the function we have created to have an output binding to store the data.Azure Functions allows to write 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 input 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 learnt about triggers,input and output bindings,our next step would be to use the same function what 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

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

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.

Next step would be to use the same function what 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 Azure management portal,under function app expand your function.
Azure portal
Image — Azure portal
  • Select Integrate option (under Function app)
Azure Function App
Image — Azure Function App
  • Click on New Output under Outputs drop down (top right corner)
Azure Function
Image — Azure Function
  • Use Azure Cosmos DB as the output binding
Azure Function
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:
Create new Azure Cosmos DB
Image — Create new Azure Cosmos DB
updated output binding
Image — updated output binding
Update the Function code
Image — Update the Function code

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

Azure Cosmos DB
Image — Azure Cosmos DB
Data in Azure Cosmos DB
Image — Data in Azure Cosmos DB

Step #5 : 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.

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

Additional Resources :

Originally published at @upnxtblog.