Skip to main content

Reusable component to create globally distributed applications with Azure Cosmos DB

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

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
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 infrastructure 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.

The CosmosApp component#

The CosmosApp 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

The application has three examples 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
  2. Configure Azure credentials
  3. Install Node.js

Deploying the example#

  1. Create a new stack:

    Terminal window
    pulumi stack init dev
  2. Log in to the Azure CLI (you will be prompted to do this during deployment if you forget this step):

    Terminal window
    az login
  3. Configure the target Azure environment:

    Terminal window
    pulumi config set azure:location <location>
    pulumi config set azure:subscriptionId <YOUR_SUBSCRIPTION_ID>
  4. Install dependencies:

    Terminal window
    npm install
  5. Deploy the stack:

    Terminal window
    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
    ...
  6. Check the deployed website endpoints. Three endpoints are now available. For example:

    Terminal window
    pulumi stack output functionsEndpoint
    curl "$(pulumi stack output functionsEndpoint)"
    http://functionscosmosfunc-rgcc15ea50.trafficmanager.net/api/cosmos
    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:

Terminal window
mocha -r ts-node/register unittests.ts

Running the policy pack#

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

Terminal window
pulumi up --policy-pack policy

Cleaning up#

Once you are done, you can destroy all of the resources, and the stack:

Terminal window
pulumi destroy
pulumi stack rm

Related

The infrastructure as code platform for any cloud.