{"id":10713,"date":"2025-07-11T10:30:15","date_gmt":"2025-07-11T17:30:15","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=10713"},"modified":"2025-07-11T10:30:15","modified_gmt":"2025-07-11T17:30:15","slug":"building-event-driven-go-applications-with-azure-cosmos-db-and-azure-functions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/building-event-driven-go-applications-with-azure-cosmos-db-and-azure-functions\/","title":{"rendered":"Building Event-Driven Go applications with Azure Cosmos DB and Azure Functions"},"content":{"rendered":"<p><a href=\"https:\/\/go.dev\/\" target=\"_blank\" rel=\"noopener noreferrer\">The Go programming language<\/a>\u00a0is a great fit for building serverless applications. Go applications can be easily compiled to a single, statically linked binary, making deployment simple and reducing external dependencies. They start up quickly, which is ideal for serverless environments where functions are frequently invoked from a cold start. Go applications also tend to use less memory compared to other languages, helping optimize resource usage and reduce costs in serverless scenarios.<\/p>\n<p>Azure Functions supports Go using\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/azure-functions\/functions-custom-handlers\" target=\"_blank\" rel=\"noopener noreferrer\">custom handlers<\/a>, and you can use triggers and input and output bindings via extension bundles. Azure Functions is\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/azure-functions\/functions-bindings-cosmosdb-v2?tabs=isolated-process%2Cextensionv4&amp;pivots=programming-language-csharp\" target=\"_blank\" rel=\"noopener noreferrer\">tightly integrated with Azure Cosmos DB<\/a>\u00a0using bindings (<a href=\"https:\/\/learn.microsoft.com\/azure\/azure-functions\/functions-bindings-cosmosdb-v2-input?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cextensionv4&amp;pivots=programming-language-csharp\" target=\"_blank\" rel=\"noopener noreferrer\">input<\/a>,\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/azure-functions\/functions-bindings-cosmosdb-v2-output?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cextensionv4&amp;pivots=programming-language-csharp\" target=\"_blank\" rel=\"noopener noreferrer\">output<\/a>), and\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/azure-functions\/functions-bindings-cosmosdb-v2-trigger?tabs=python-v2%2Cisolated-process%2Cextensionv4%2Cnodejs-v4&amp;pivots=programming-language-csharp\" target=\"_blank\" rel=\"noopener noreferrer\">triggers<\/a>.<\/p>\n<p>This blog post will walk you through how to build Azure Functions with Go that make use of these Azure Cosmos DB integrations. Bindings allow you to easily read and write data to Azure Cosmos DB, while triggers are useful for building event-driven applications that respond to changes in your data in Azure Cosmos DB.<\/p>\n<p><strong>Part 1<\/strong>\u00a0of this blog starts off with a function that gets triggered by changes in a Azure Cosmos DB container and simply logs the raw Azure Functions event payload and the Cosmos DB document. You will learn how to run the function and also test it with Cosmos DB locally, thanks to the\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/emulator\" target=\"_blank\" rel=\"noopener noreferrer\">Azure Cosmos DB emulator<\/a>\u00a0and\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/azure-functions\/functions-run-local?tabs=linux%2Cisolated-process%2Cnode-v4%2Cpython-v2%2Chttp-trigger%2Ccontainer-apps&amp;pivots=programming-language-csharp\" target=\"_blank\" rel=\"noopener noreferrer\">Azure Functions Core Tools<\/a>. If this is your first time working with Go and Azure Functions, you should find it helpful to get up and running quickly. Although you can deploy it to Azure, we will save that for the next part of this blog.<\/p>\n<p><strong>Part 2<\/strong>\u00a0dives into another function that generates embeddings for the documents in the Azure Cosmos DB container. This example will use an\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/ai-services\/openai\/overview\" target=\"_blank\" rel=\"noopener noreferrer\">Azure OpenAI<\/a>\u00a0embedding model to generate embeddings for the documents in the container and then store the embeddings back in the container. This is useful for building applications that require semantic search or other generative AI applications.<\/p>\n<p><div class=\"alert alert-primary\">Check out the\u00a0<a href=\"https:\/\/github.com\/abhirockzz\/golang_cosmosdb_azure_functions\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub repository<\/a>\u00a0for the complete code.<\/div><\/p>\n<h2>Part 1: Build a simple Azure Cosmos DB trigger-based function and run it locally<\/h2>\n<p>Just as the Azure Cosmos DB emulator lets you run Azure Cosmos DB locally, Azure Functions Core Tools lets you develop and test your functions locally.<\/p>\n<p>Start by installing the Azure Functions Core Tools \u2013\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/azure-functions\/functions-run-local?tabs=linux%2Cisolated-process%2Cnode-v4%2Cpython-v2%2Chttp-trigger%2Ccontainer-apps&amp;pivots=programming-language-csharp#install-the-azure-functions-core-tools\" target=\"_blank\" rel=\"noopener noreferrer\">refer to the documentation<\/a>\u00a0for instructions for your OS. For example, on Linux, you can:<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">sudo apt-get update\r\nsudo apt-get install azure-functions-core-tools-4<\/code><\/pre>\n<p>Next, start the Azure Cosmos DB emulator. The commands below are for Linux and use the Docker container-based approach &#8211;\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/how-to-develop-emulator?tabs=docker-linux%2Ccsharp&amp;pivots=api-nosql#start-the-emulator\" target=\"_blank\" rel=\"noopener noreferrer\">refer to the documentation<\/a>\u00a0for other options.<\/p>\n<p><div class=\"alert alert-info\">You need to have Docker installed and running on your machine. If you don&#8217;t have it installed, please refer to the <a href=\"https:\/\/docs.docker.com\/get-docker\/\">Docker installation guide<\/a>.<\/div><\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">docker pull mcr.microsoft.com\/cosmosdb\/linux\/azure-cosmos-emulator:latest\r\n\r\ndocker run \\\r\n --publish 8081:8081 \\\r\n --name linux-emulator \\\r\n -e AZURE_COSMOS_EMULATOR_PARTITION_COUNT=1 \\\r\n mcr.microsoft.com\/cosmosdb\/linux\/azure-cosmos-emulator:latest<\/code><\/pre>\n<p>Make sure to\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/how-to-develop-emulator?tabs=docker-linux%2Ccsharp&amp;pivots=api-nosql#import-the-emulators-tlsssl-certificate\" target=\"_blank\" rel=\"noopener noreferrer\">configure the emulator SSL certificate as well<\/a>. For example, for the Linux system I was using, I ran the following command to download the certificate and regenerate the certificate bundle:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">curl --insecure https:\/\/localhost:8081\/_explorer\/emulator.pem &gt; ~\/emulatorcert.crt\r\n\r\nsudo update-ca-certificates<\/code><\/pre>\n<p>Use the following URL to navigate to the Azure Cosmos DB Data Explorer using your browser:\u00a0<code>http:\/\/localhost:8081\/_explorer\/index.html<\/code>. Create the following resources:<\/p>\n<ul>\n<li>A database<\/li>\n<li>A container with partition key\u00a0<code>\/id<\/code>\u00a0\u2013 this is the source container<\/li>\n<li>A lease container with the name\u00a0<code>leases<\/code>\u00a0and partition key\u00a0<code>\/id<\/code>\u00a0\u2013 it is used by the trigger to keep track of the changes in the source container.<\/li>\n<\/ul>\n<p>Clone the GitHub repository with the code for the function:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">git clone https:\/\/github.com\/abhirockzz\/golang_cosmosdb_azure_functions.git\r\n\r\ncd golang_cosmosdb_azure_functions\/getting_started_guide<\/code><\/pre>\n<p>A <code>local.settings.json<\/code> file is used to store the configuration settings for your function app when running locally, Create a <code>local.settings.json<\/code> file and populate it with Azure Cosmos DB related information. Use the same database and container names as you created in the previous step.<\/p>\n<p>Here is the format:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-json\"><code class=\"language-json\">{\r\n  \"IsEncrypted\": false,\r\n  \"Values\": {\r\n    \"AzureWebJobsStorage\": \"\",\r\n    \"FUNCTIONS_WORKER_RUNTIME\": \"custom\",\r\n    \"COSMOS_CONNECTION\": \"AccountEndpoint=https:\/\/localhost:8081\/;AccountKey=C2y6yDjf5\/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw\/Jw==;\",\r\n    \"COSMOS_DATABASE_NAME\": \"test\",\r\n    \"COSMOS_CONTAINER_NAME\": \"tasks\"\r\n  }\r\n}\r\n<\/code><\/pre>\n<\/div>\n<p><div class=\"alert alert-primary\"><code>COSMOS_CONNECTION<\/code>\u00a0has a static value for the\u00a0connection string for the Azure Cosmos DB emulator\u00a0\u2013 do not change it.<\/div><\/p>\n<div class=\"highlight js-code-highlight\">\n<p>Build the Go function binary using the following command. This will create a binary file named\u00a0<code>main<\/code>\u00a0in the current directory:<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">go build -o main main.go<\/code><\/pre>\n<p>Start the function locally:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">func start<\/code><\/pre>\n<p>This will start the function app and listen for incoming requests. You should see an output similar to this:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">[2025-04-25T07:44:53.921Z] Worker process started and initialized.\r\n\r\nFunctions:\r\n\r\n        processor: cosmosDBTrigger\r\n\r\nFor detailed output, run func with --verbose flag.\r\n[2025-04-25T07:44:58.809Z] Host lock lease acquired by instance ID '0000000000000000000000006ADD8D3E'.\r\n\r\n\/\/...<\/code><\/pre>\n<\/div>\n<div>\n<p>Add data to the source container in Azure Cosmos DB. You can do this by navigating to Data Explorer in the emulator. For example, add a document with the following JSON:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-json\"><code class=\"language-json\">{\r\n  \"id\": \"42\",\r\n  \"description\": \"test\"\r\n}<\/code><\/pre>\n<p>The function should be triggered automatically when the document is added to the container. You can check the logs of the function app to see if it was triggered successfully:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">[2025-04-25T07:48:10.559Z] Executing 'Functions.processor' (Reason='New changes on container tasks at 2025-04-25T07:48:10.5593689Z', Id=7b62f8cf-683b-4a5b-9db0-83d049bc4c86)\r\n[2025-04-25T07:48:10.565Z] processor function invoked...\r\n[2025-04-25T07:48:10.565Z] Raw event payload: {{\"[{\\\"id\\\":\\\"42\\\",\\\"description\\\":\\\"test\\\",\\\"_rid\\\":\\\"AxI2AL1rrFoDAAAAAAAAAA==\\\",\\\"_self\\\":\\\"dbs\/AxI2AA==\/colls\/AxI2AL1rrFo=\/docs\/AxI2AL1rrFoDAAAAAAAAAA==\/\\\",\\\"_etag\\\":\\\"\\\\\\\"00000000-0000-0000-b5b6-6123f4d401db\\\\\\\"\\\",\\\"_attachments\\\":\\\"attachments\/\\\",\\\"_ts\\\":1745567285,\\\"_lsn\\\":4}]\"}} {{processor 2025-04-25T07:48:10.560243Z 4f29b3f3-ba95-4043-9b67-2856a43b4734}}}\r\n[2025-04-25T07:48:10.566Z] Cosmos DB document: {42  AxI2AL1rrFoDAAAAAAAAAA== dbs\/AxI2AA==\/colls\/AxI2AL1rrFo=\/docs\/AxI2AL1rrFoDAAAAAAAAAA==\/ \"00000000-0000-0000-b5b6-6123f4d401db\" attachments\/ 1745567285 4}\r\n[2025-04-25T07:48:10.566Z] Executed 'Functions.processor' (Succeeded, Id=7b62f8cf-683b-4a5b-9db0-83d049bc4c86, Duration=6ms)\r\n\r\n\/\/.....<\/code><\/pre>\n<h3>How it works<\/h3>\n<p>Here is a very high-level overview of the code:<\/p>\n<ul>\n<li><a href=\"https:\/\/getting_started_guide\/main.go\">main.go<\/a>\u00a0\u2013 Implements an HTTP server with a\u00a0<code>processor<\/code>\u00a0endpoint. When triggered, it reads a Azure Cosmos DB trigger payload from the request, parses the nested documents, logs information, and returns a structured JSON response. It uses types and helpers from the\u00a0<code>common<\/code>\u00a0package.<\/li>\n<li><a href=\"https:\/\/dev.togetting_started_guide\/common\">common<\/a>\u00a0package: Contains shared types and utilities for Azure Cosmos DB trigger processing:\n<ul>\n<li><a href=\"https:\/\/getting_started_guide\/common\/payload.go\">payload.go<\/a>: Defines data structures for the trigger payload, documents, and response.<\/li>\n<li><a href=\"https:\/\/getting_started_guide\/common\/parse.go\">parse.go<\/a>: Provides a\u00a0<code>Parse<\/code>\u00a0function to extract and unmarshal documents from the trigger payload\u2019s nested JSON structure.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<h2>Part 2: Use Azure OpenAI to generate embeddings for the documents in the Azure Cosmos DB container<\/h2>\n<p>In addition to its low-latency, high-performance, and scalability characteristics, its support for\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/nosql\/vector-search\" target=\"_blank\" rel=\"noopener noreferrer\">Vector<\/a>, <a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/gen-ai\/full-text-search?context=%2Fazure%2Fcosmos-db%2Fnosql%2Fcontext%2Fcontext\" target=\"_blank\" rel=\"noopener noreferrer\">Full-text<\/a>, and\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/cosmos-db\/gen-ai\/hybrid-search?context=%2Fazure%2Fcosmos-db%2Fnosql%2Fcontext%2Fcontext\" target=\"_blank\" rel=\"noopener noreferrer\">Hybrid search<\/a>\u00a0makes Azure Cosmos DB a\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/gen-ai\/why-cosmos-ai\" target=\"_blank\" rel=\"noopener noreferrer\">great fit for generative AI applications<\/a>. Consider a use case for managing a product catalog for an e-commerce platform. Each time a new product is added to the system (with a short description like \u201cBluetooth headphones with noise cancellation\u201d), we want to immediately make that item searchable semantically.<\/p>\n<p>As soon as the product document is written to Azure Cosmos DB, an Azure Function is\u00a0<strong>triggered<\/strong>. It extracts the product description, generates a vector embedding using Azure OpenAI, and writes the embedding back to the same document using an\u00a0<strong>output binding<\/strong>. With the embedding in place, the product is now indexed and ready for search queries.<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/07\/solution_overview.png\"><img decoding=\"async\" class=\"aligncenter wp-image-10720 size-medium\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/07\/solution_overview-300x274.png\" alt=\"Solution overview image\" width=\"300\" height=\"274\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/07\/solution_overview-300x274.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/07\/solution_overview-1024x935.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/07\/solution_overview-768x701.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/07\/solution_overview.png 1205w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<\/div>\n<h3>Prerequisites<\/h3>\n<p>You will run this example in Azure, so you need to have an Azure account. If you don&#8217;t have one, you can\u00a0<a href=\"https:\/\/azure.microsoft.com\/free\/\" target=\"_blank\" rel=\"noopener noreferrer\">create a free account<\/a>. <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/cosmos-db\/nosql\/how-to-create-account?tabs=azure-cli\" target=\"_blank\" rel=\"noopener noreferrer\">Create an Azure Cosmos DB for NoSQL account<\/a> and <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/cosmos-db\/nosql\/vector-search\" target=\"_blank\" rel=\"noopener noreferrer\">Enable the vector indexing and search feature<\/a>\u00a0\u2013 this is a one-time operation.<\/p>\n<p>Just like before, you will need to create the following resources:<\/p>\n<ul>\n<li>A database<\/li>\n<li>A container with partition key\u00a0<code>\/id<\/code>\u00a0\u2013 this is the source container<\/li>\n<li>A lease container with the name\u00a0<code>leases<\/code>\u00a0and partition key\u00a0<code>\/id<\/code>\u00a0\u2013 it is used by the trigger to keep track of the changes in the source container.<\/li>\n<\/ul>\n<p><div class=\"alert alert-info\">The lease container needs to be created in advance since we have configured Azure Functions to <a href=\"https:\/\/learn.microsoft.com\/azure\/azure-functions\/functions-bindings-cosmosdb-v2-trigger?tabs=python-v2%2Cin-process%2Cfunctionsv2%2Cnodejs-v4&amp;pivots=programming-language-csharp#grant-permission-to-the-identity\">use managed identity to access the Azure Cosmos DB account<\/a> \u2013 you don&#8217;t need to use keys or connection strings.<\/div><\/p>\n<\/div>\n<p><a href=\"https:\/\/learn.microsoft.com\/azure\/ai-services\/openai\/how-to\/create-resource?pivots=web-portal#create-a-resource\" target=\"_blank\" rel=\"noopener noreferrer\">Create an Azure OpenAI Service<\/a>\u00a0resource. Azure OpenAI Service provides access to OpenAI&#8217;s models including GPT-4o, GPT-4o mini (and more), as well as embedding models.\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/ai-services\/openai\/how-to\/create-resource?pivots=web-portal#deploy-a-model\" target=\"_blank\" rel=\"noopener noreferrer\">Deploy an embedding model<\/a>\u00a0of your choice using the Azure AI Foundry portal (for example, I used the\u00a0<code>text-embedding-3-small<\/code>\u00a0model). Just like the Azure Cosmos DB account, the Azure Function app uses a managed identity to access the Azure OpenAI Service resource.<\/p>\n<h3><a href=\"https:\/\/dev.to\/abhirockzz\/building-event-driven-go-applications-with-azure-cosmos-db-and-azure-functions-47a9#deploy-resources\" name=\"deploy-resources\"><\/a>Deploy resources<\/h3>\n<p>Move into the right directory:<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">cd ..\/embeddings_generator\r\n<\/code><\/pre>\n<p>To simplify the deployment of the function app along with the required resources and configuration, you can use the\u00a0<code>deploy.sh<\/code>\u00a0script. At a high level, it:<\/p>\n<ul>\n<li>Sets up environment variables for Azure resources.<\/li>\n<li>Creates an Azure resource group, storage account, and function app plan.<\/li>\n<li>Deploys a custom Go-based Azure Function App.<\/li>\n<li>Builds the Go binary for Windows.<\/li>\n<li>Publishes the function app to Azure.<\/li>\n<li>Enables the function app system identity and provides it the required roles for Azure Cosmos DB and Azure OpenAI resource access.<\/li>\n<\/ul>\n<p>Before you deploy the solution, update the\u00a0<code>local.settings.json<\/code>. Use the same database and container names as you created in the previous step:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-json\"><code class=\"language-json\">{\r\n  \"IsEncrypted\": false,\r\n  \"Values\": {\r\n    \"AzureWebJobsStorage\": \"\",\r\n    \"FUNCTIONS_WORKER_RUNTIME\": \"custom\",\r\n    \"COSMOS_CONNECTION__accountEndpoint\": \"https:\/\/ENTER_COSMOSDB_ACCOUNT_NAME.documents.azure.com:443\/\",\r\n    \"COSMOS_DATABASE_NAME\": \"name of the database\",\r\n    \"COSMOS_CONTAINER_NAME\": \"name of the container\",\r\n    \"COSMOS_HASH_PROPERTY\": \"hash\",\r\n    \"COSMOS_VECTOR_PROPERTY\": \"embedding\",\r\n    \"COSMOS_PROPERTY_TO_EMBED\": \"description\",\r\n    \"OPENAI_DEPLOYMENT_NAME\": \"enter the embedding model deployment name e.g. text-embedding-3-small\",\r\n    \"OPENAI_DIMENSIONS\": \"enter the dimensions e.g. 1536\",\r\n    \"OPENAI_ENDPOINT\": \"https:\/\/ENTER_OPENAI_RESOURCE_NAME.openai.azure.com\/\"\r\n  }\r\n}<\/code><\/pre>\n<\/div>\n<div>\n<ul>\n<li><strong>COSMOS_CONNECTION_accountEndpoint<\/strong>: Endpoint URL for the Azure Cosmos DB account.<\/li>\n<li><strong>COSMOS_DATABASE_NAME<\/strong>: Name of the Azure Cosmos DB database to use.<\/li>\n<li><strong>COSMOS_CONTAINER_NAME<\/strong>: Name of the Azure Cosmos DB container to use.<\/li>\n<li><strong>COSMOS_HASH_PROPERTY<\/strong>: Name of the property used as a hash in Azure Cosmos DB documents (no need to modify this).<\/li>\n<li><strong>COSMOS_VECTOR_PROPERTY<\/strong>: Name of the property storing vector embeddings in Azure Cosmos DB.<\/li>\n<li><strong>COSMOS_PROPERTY_TO_EMBED<\/strong>: Name of the property whose value will be embedded. Change this based on your document structure.<\/li>\n<li><strong>OPENAI_DEPLOYMENT_NAME<\/strong>: Name of the Azure OpenAI model deployment to use for embeddings.<\/li>\n<li><strong>OPENAI_DIMENSIONS<\/strong>: Number of dimensions for the embedding vectors.<\/li>\n<li><strong>OPENAI_ENDPOINT<\/strong>: Endpoint URL for the Azure OpenAI resource.<\/li>\n<\/ul>\n<\/div>\n<p>Run the\u00a0<code>deploy.sh<\/code>\u00a0script:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">chmod +x deploy.sh\r\n.\/deploy.sh\r\n<\/code><\/pre>\n<h3>Run the end-to-end example<\/h3>\n<p>Add data to the source container in Azure Cosmos DB. For example, add a document with the following JSON:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-json\"><code class=\"language-json\">{\r\n  \"id\": \"de001c6d-4efe-4a65-a59a-39a0580bfa2a\",\r\n  \"description\": \"Research new technology\"\r\n}<\/code><\/pre>\n<p>The function should be triggered automatically when the document is added to the container. You can check the logs of the function app to see if it was triggered successfully:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">func azure functionapp logstream &lt;FUNCTION_APP_NAME&gt;\r\n<\/code><\/pre>\n<p>You should see logs similar to this (the payload will be different depending on the data you add):<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">2025-04-23T05:34:41Z [Information] function invoked\r\n2025-04-23T05:34:41Z [Information] cosmosVectorPropertyName: embedding\r\n2025-04-23T05:34:41Z [Information] cosmosVectorPropertyToEmbedName: description\r\n2025-04-23T05:34:41Z [Information] cosmosHashPropertyName: hash\r\n2025-04-23T05:34:41Z [Information] Processing 1 documents\r\n2025-04-23T05:34:41Z [Information] Processing document ID: de001c6d-4efe-4a65-a59a-39a0580bfa2a\r\n2025-04-23T05:34:41Z [Information] Document data: Research new technology\r\n2025-04-23T05:34:41Z [Information] New document detected, generated hash: 5bb57053273563e2fbd4202c666373ccd48f86eaf9198d7927a93a555aa200aa\r\n2025-04-23T05:34:41Z [Information] Document modification status: true, hash: 5bb57053273563e2fbd4202c666373ccd48f86eaf9198d7927a93a555aa200aa\r\n2025-04-23T05:34:41Z [Information] Created embedding for document: map[description:Research new technology id:de001c6d-4efe-4a65-a59a-39a0580bfa2a]\r\n2025-04-23T05:34:41Z [Information] Adding 1 document with embeddings\r\n2025-04-23T05:34:41Z [Information] Added enriched documents to binding output\r\n2025-04-23T05:34:41Z [Information] Executed 'Functions.cosmosdbprocessor' (Succeeded, Id=91f4760f-047a-4867-9030-46a6602ab179, Duration=128ms)\r\n\r\n\/\/....<\/code><\/pre>\n<p>Verify the data in Azure Cosmos DB. You should see an embedding for the\u00a0<code>description<\/code>\u00a0property of the document stored in the\u00a0<code>embedding<\/code>\u00a0property. It should look something like this:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-json\"><code class=\"language-json\">{\r\n  \"id\": \"de001c6d-4efe-4a65-a59a-39a0580bfa2a\",\r\n  \"description\": \"Research new technology\",\r\n  \"embedding\": [\r\n    0.028226057, -0.00958694\r\n    \/\/....\r\n  ],\r\n  \"hash\": \"5bb57053273563e2fbd4202c666373ccd48f86eaf9198d7927a93a555aa200aa\"\r\n}\r\n<\/code><\/pre>\n<p>Once the embeddings are generated, you can integrate this with generative AI applications. For example, you can use the\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/nosql\/vector-search\" target=\"_blank\" rel=\"noopener noreferrer\">Vector Search<\/a>\u00a0feature of Azure Cosmos DB to perform similarity searches based on the embeddings.<\/p>\n<\/div>\n<h3>How it works<\/h3>\n<p>Here is a very high-level overview of the code:<\/p>\n<ul>\n<li><a href=\"https:\/\/embeddings_generator\/main.go\">main.go<\/a>: Implements an HTTP server with a\u00a0<code>cosmosdbprocessor<\/code>\u00a0endpoint. When triggered, it reads a Azure Cosmos DB trigger payload from the request, parses the nested documents, generates embeddings using Azure OpenAI, and writes the enriched documents back to the Cosmos DB container.\n<ul>\n<li>Exposes the\u00a0<code>cosmosdbprocessor<\/code>\u00a0endpoint, which processes incoming Azure Cosmos DB documents.<\/li>\n<li>For each document, checks if it is new or modified (using a hash), generates an embedding (vector) using Azure OpenAI, and prepares enriched documents for output.<\/li>\n<li>Handles logging and error reporting for the function execution.<\/li>\n<\/ul>\n<\/li>\n<li><a href=\"https:\/\/dev.toembeddings_generator\/common\/\">common<\/a>\u00a0package: Contains shared utilities and types for processing Azure Cosmos DB documents\n<ul>\n<li><a href=\"https:\/\/embeddings_generator\/common\/embedding.go\">embedding.go<\/a>: Handles creation of embeddings using Azure OpenAI.<\/li>\n<li><a href=\"https:\/\/embeddings_generator\/common\/parse.go\">parse.go<\/a>: Parses and extracts documents from the Azure Cosmos DB trigger payload.<\/li>\n<li><a href=\"https:\/\/embeddings_generator\/common\/payload.go\">payload.go<\/a>: Defines data structures for payloads and responses used across the project.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<p>The function uses a\u00a0<code>hash<\/code>\u00a0property to check if the document has already been processed. If the\u00a0<code>hash<\/code>\u00a0value is different from the one stored in Azure Cosmos DB, it means that the document has been modified and needs to be re-processed. In this case, the function will generate a new embedding and update the document with the new hash value. This ensures that the function does not get stuck in an infinite loop. If the\u00a0<code>hash<\/code>\u00a0value is the same, it means that the document has not been modified and does not need to be re-processed. In this case, the function will log that the document is unchanged and will not generate a new embedding.<\/p>\n<p>You should see logs similar to this:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">2025-04-23T05:34:42Z   [Information]   function invoked\r\n2025-04-23T05:34:42Z   [Information]   cosmosVectorPropertyName: embedding\r\n2025-04-23T05:34:42Z   [Information]   cosmosVectorPropertyToEmbedName: description\r\n2025-04-23T05:34:42Z   [Information]   cosmosHashPropertyName: hash\r\n2025-04-23T05:34:42Z   [Information]   Processing 1 document\r\n2025-04-23T05:34:42Z   [Information]   Processing document ID: de001c6d-4efe-4a65-a59a-39a0580bfa2a\r\n2025-04-23T05:34:42Z   [Information]   Document data: Research new technology\r\n2025-04-23T05:34:42Z   [Information]   Document unchanged, hash: 5bb57053273563e2fbd4202c666373ccd48f86eaf9198d7927a93a555aa200aa\r\n2025-04-23T05:34:42Z   [Information]   Document modification status: false, hash:\r\n2025-04-23T05:34:42Z   [Information]   Executed 'Functions.cosmosdbprocessor' (Succeeded, Id=f0cf039a-5de5-4cc1-b29d-928ce32b294e, Duration=6ms)\r\n\r\n\/\/....<\/code><\/pre>\n<h3>Delete resources<\/h3>\n<p>Be sure to clean up the resources you created in Azure. You can do this using the Azure portal or the Azure CLI. For example, to delete the resource group and all its resources, run:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">az group delete --name &lt;resource-group-name&gt;\r\n<\/code><\/pre>\n<div class=\"crayons-article__main \">\n<div id=\"article-body\" class=\"crayons-article__body text-styles spec__body\" data-article-id=\"2433149\">\n<h2>Conclusion<\/h2>\n<p>In this blog post, you learned how to build Azure Functions with Go that use Azure Cosmos DB triggers and bindings. It started with a simple function that logs the raw event payload and the Azure Cosmos DB document, and then moved on to a function that generates embeddings for the documents in the Azure Cosmos DB container using Azure OpenAI. You also learned how to run the functions locally using the Azure Cosmos DB emulator and Azure Functions Core Tools, and how to deploy them to Azure.<\/p>\n<p>You can use these examples as a starting point for building your own serverless applications with Go and Azure Functions. The combination of Go&#8217;s performance and simplicity, along with Azure Functions&#8217; scalability and integration with Azure Cosmos DB, makes it a powerful platform for building modern applications.<\/p>\n<\/div>\n<h2>About Azure Cosmos DB<\/h2>\n<article id=\"post-10622\" class=\"middle-column pe-xl-198\" data-clarity-region=\"article\">\n<div class=\"entry-content sharepostcontent \" data-bi-area=\"body_article\" data-bi-id=\"post_page_body_article\">\n<p>Azure Cosmos DB is a fully managed and serverless distributed database for modern app development, with SLA-backed speed and availability, automatic and instant scalability, and support for open-source PostgreSQL, MongoDB, and Apache Cassandra. To stay in the loop on Azure Cosmos DB updates, follow us on\u00a0<a href=\"https:\/\/twitter.com\/AzureCosmosDB\" target=\"_blank\" rel=\"noopener\">X<\/a>,\u00a0<a href=\"https:\/\/aka.ms\/AzureCosmosDBYouTube\" target=\"_blank\" rel=\"noopener\">YouTube<\/a>, and\u00a0<a href=\"https:\/\/www.linkedin.com\/company\/azure-cosmos-db\/\" target=\"_blank\" rel=\"noopener\">LinkedIn<\/a>.<\/p>\n<p>To easily build your first database, watch our\u00a0<a href=\"https:\/\/youtube.com\/playlist?list=PLmamF3YkHLoLLGUtSoxmUkORcWaTyHlXp\" target=\"_blank\" rel=\"noopener\">Get Started videos<\/a>\u00a0on YouTube and explore ways to\u00a0<a href=\"https:\/\/docs.microsoft.com\/azure\/cosmos-db\/optimize-dev-test\" target=\"_blank\" rel=\"noopener\">dev\/test free.<\/a><\/p>\n<\/div>\n<\/article>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Go programming language\u00a0is a great fit for building serverless applications. Go applications can be easily compiled to a single, statically linked binary, making deployment simple and reducing external dependencies. They start up quickly, which is ideal for serverless environments where functions are frequently invoked from a cold start. Go applications also tend to use [&hellip;]<\/p>\n","protected":false},"author":181737,"featured_media":10726,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[14,1935,1940],"tags":[499,496,1738],"class_list":["post-10713","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-sql-api","category-go-sdk","category-serverless","tag-azure-cosmos-db","tag-azure-functions","tag-go"],"acf":[],"blog_post_summary":"<p>The Go programming language\u00a0is a great fit for building serverless applications. Go applications can be easily compiled to a single, statically linked binary, making deployment simple and reducing external dependencies. They start up quickly, which is ideal for serverless environments where functions are frequently invoked from a cold start. Go applications also tend to use [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/10713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/181737"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=10713"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/10713\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/10726"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=10713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=10713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=10713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}