---
title: "Reusable Component to Create Globally-distributed Applications with Azure Cosmos DB"
description: "Reusable Component to Create Globally-distributed Applications with Azure Cosmos DB"
url: "https://www.pulumi.com/dev/examples/classic-azure-ts-cosmosapp-component/"
image: "https://www.pulumi.com/assets/og/dev/examples/classic-azure-ts-cosmosapp-component.png"
---

# Reusable Component to Create Globally-distributed Applications with Azure Cosmos DB

Reusable Component to Create Globally-distributed Applications with Azure Cosmos DB

- Source on GitHub: https://github.com/pulumi/examples/tree/master/classic-azure-ts-cosmosapp-component
- Deploy with Pulumi: https://app.pulumi.com/new?template=https%3A%2F%2Fgithub.com%2Fpulumi%2Fexamples%2Ftree%2Fmaster%2Fclassic-azure-ts-cosmosapp-component

## Get started with this example

This example lives in the [pulumi/examples](https://github.com/pulumi/examples/tree/master/classic-azure-ts-cosmosapp-component) repo. Pull down just this directory to follow along:

```bash
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set classic-azure-ts-cosmosapp-component
cd pulumi-examples/classic-azure-ts-cosmosapp-component
```

This example demonstrates the usage of Pulumi to create globally-distributed applications with Azure Cosmos DB as the backend and pluggable infrastrustructure as the web tier.

The application shows several notable features:

1. Easy global deployments - a config setting provides a list of all the regions to deploy and a single execution deploys across them all.
2. Abstraction - the `CosmosApp` component - abstracts away all the common logic for a global app with Cosmos DB multi-region data distribution and Traffic Manager for routing the traffic.
3. Multi-model - examples are provided for serverless functions, containers, and virtual machines, all fitting to the above abstraction.

## `CosmosApp` component

The [`CosmosApp`](https://github.com/pulumi/examples/tree/master/classic-azure-ts-cosmosapp-component/cosmosApp.ts) defines a skeleton for the application. While not limiting the type of compute resources, it creates the multi-regional pieces of the infrastructure:

![Cosmos App](https://github.com/mikhailshilkov/pulumi-cosmos/raw/master/pictures/globalapp.png)

The application has three example of using this component with the following compute services:

- Azure Functions
- Azure Container Instances
- Azure VM Scale Sets + Azure Load Balancer

## Prerequisites

1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
1. [Configure Azure credentials](https://www.pulumi.com/docs/intro/cloud-providers/azure/setup/)

## Steps

### Step 1: Create a new stack

```bash
pulumi stack init dev
```

### Step 2: Log in to the Azure CLI

You will be prompted to do this during deployment if you forget this step.

```bash
az login
```

### Step 3: Install NPM dependencies

```bash
npm install
```

### Step 4: Configure target Azure environment

```bash
pulumi config set azure:location <location>
pulumi config set azure:subscriptionId <YOUR_SUBSCRIPTION_ID>
```

### Step 5: Deploy your changes

Run `pulumi up` to preview and deploy changes:

```console
$ pulumi up
Previewing changes:
+  azure-ts-cosmosapp-component-dev  create
+  examples:azure:CosmosApp vms create
+  azure:network:VirtualNetwork vnet-westeurope create
+  azure:network:PublicIp pip-westeurope create
+  azure:trafficmanager:Profile tmvms create
+  azure:trafficmanager:Endpoint tmvmswesteurope create
+  azure:cosmosdb:Account cosmos-vms
...
```

### Step 6: Check the deployed website endpoints

Three endpoints are now available. For example,

```console
$ pulumi stack output functionsEndpoint
http://functionscosmosfunc-rgcc15ea50.trafficmanager.net/api/cosmos

$ curl "$(pulumi stack output functionsEndpoint)"
Document 'cosmos' not found
```

Go to the Azure portal and add a document with the ID "cosmos" to receive a non-empty response.

## Running Unit Tests

The `unittests.ts` file contains two sample unit tests that can be run with Mocha:

```bash
mocha -r ts-node/register unittests.ts
```

## Running Policy Pack

The `policy` folder contains two sample policies that can be applied with the `policy-pack` argument:

```bash
pulumi up --policy-pack policy
```
