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.
Quick Snapshot
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
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.
- Select Integrate option (under Function app)
- Click on New Output under Outputs drop-down (top right corner)
- Use Azure Cosmos DB as the output binding
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:
Step #3: Verify if data has been added to 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 :
- Azure Cosmos DB
- More examples using Cosmos DB
- Azure Functions Developer Guide
- Serverless tutorial – Invoke Azure functions using HTTP Trigger
- Serverless Comparison : AWS Lambda vs. Azure Functions vs. Google Functions
- Visualize IoT-scale time-series data using Azure Time Series Insights
Average Rating