1. Answers
  2. Integrating Azure Cosmos DB with Machine Learning

How do I integrate Azure Cosmos DB with Azure Machine Learning?

In this guide, we will create an Azure Cosmos DB account and integrate it with Azure Machine Learning using Pulumi. We will set up the necessary resources including a Cosmos DB account, a database, a container, and a Machine Learning workspace.

import * as pulumi from "@pulumi/pulumi";
import * as azure from "@pulumi/azure-native";

// Create an Azure Resource Group
const resourceGroup = new azure.resources.ResourceGroup("resourceGroup", {
    location: "WestUS",
});

// Create an Azure Cosmos DB account
const cosmosAccount = new azure.documentdb.DatabaseAccount("cosmosAccount", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    databaseAccountOfferType: "Standard",
    locations: [{
        locationName: resourceGroup.location,
        failoverPriority: 0,
    }],
    consistencyPolicy: {
        defaultConsistencyLevel: "Session",
    },
});

// Create a Cosmos DB SQL Database
const cosmosDatabase = new azure.documentdb.SqlResourceSqlDatabase("cosmosDatabase", {
    resourceGroupName: resourceGroup.name,
    accountName: cosmosAccount.name,
    resource: {
        id: "mydatabase",
    },
});

// Create a Cosmos DB SQL Container
const cosmosContainer = new azure.documentdb.SqlResourceSqlContainer("cosmosContainer", {
    resourceGroupName: resourceGroup.name,
    accountName: cosmosAccount.name,
    databaseName: cosmosDatabase.name,
    resource: {
        id: "mycontainer",
        partitionKey: {
            paths: ["/myPartitionKey"],
            kind: "Hash",
        },
    },
});

// Create an Azure Machine Learning Workspace
const mlWorkspace = new azure.machinelearningservices.Workspace("mlWorkspace", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    sku: {
        name: "Basic",
    },
    identity: {
        type: "SystemAssigned",
    },
});

// Export the Cosmos DB account endpoint and the Machine Learning workspace URL
export const cosmosEndpoint = cosmosAccount.documentEndpoint;
export const mlWorkspaceUrl = pulumi.interpolate`https://${mlWorkspace.name}.azurewebsites.net`;

Key Points

  • We created an Azure Resource Group to contain all our resources.
  • We set up an Azure Cosmos DB account with a SQL Database and a Container.
  • We created an Azure Machine Learning Workspace for running machine learning experiments.
  • We exported the Cosmos DB account endpoint and the Machine Learning workspace URL.

Summary

This guide demonstrated how to integrate Azure Cosmos DB with Azure Machine Learning using Pulumi. We created a Cosmos DB account, a database, a container, and a Machine Learning workspace, and exported the necessary endpoints for further use.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up