Skip to content

Commit

Permalink
Adding example of Cosmos DB logic app and API connection using native…
Browse files Browse the repository at this point in the history
… azure provider
  • Loading branch information
Vivek Lakshmanan committed Feb 23, 2021
1 parent 5f8fd83 commit 2ea8d1b
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 0 deletions.
2 changes: 2 additions & 0 deletions azure-nextgen-ts-cosmosdb-logicapp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bin/
/node_modules/
3 changes: 3 additions & 0 deletions azure-nextgen-ts-cosmosdb-logicapp/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: azure-nextgen-cosmosdb-logicapp
runtime: nodejs
description: An example of creating a CosmosDB container through a Azure's Cosmos SDK and deploying a Logic App and an API Connection
62 changes: 62 additions & 0 deletions azure-nextgen-ts-cosmosdb-logicapp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# Azure Cosmos DB, an API Connection, and a Logic App

With the native Azure provider we can directly use the Azure resource manager API to define API connections and linking it to a logic app. The resulting experience is much faster in comparison to performing the same operation through ARM templates.

## Prerequisites

- [Node.js](https://nodejs.org/en/download/)
- [Download and install the Pulumi CLI](https://www.pulumi.com/docs/get-started/install/)
- [Connect Pulumi with your Azure account](https://www.pulumi.com/docs/intro/cloud-providers/azure/setup/) (if your `az` CLI is configured, no further changes are required)

## Running the App

1. Create a new stack:

```sh
$ pulumi stack init dev
```

2. Set the required configuration variables for this program, and log into Azure:

```bash
$ pulumi config set azure:location westeurope
$ az login
```

3. Perform the deployment:

```sh
$ pulumi up

Type Name Status
+ pulumi:pulumi:Stack azure-cosmosdb-logicapp-dev created
+ ├─ azure-nextgen:resources/latest:ResourceGroup logicappdemo-rg created
+ ├─ azure-nextgen:storage/latest:StorageAccount logicappdemosa created
+ ├─ azure-nextgen:documentdb/latest:DatabaseAccount logicappdemo-cdb created
+ ├─ azure-nextgen:documentdb/latest:SqlResourceSqlDatabase db created
+ ├─ azure-nextgen:web/latest:Connection cosmosdbConnection created
+ ├─ azure-nextgen:documentdb/latest:SqlResourceSqlContainer container created
+ └─ azure-nextgen:logic/latest:Workflow workflow created

Resources:
+ 8 created

Duration: 3m16s
```

4. At this point, you have a Cosmos DB collection and a Logic App listening to HTTP requests. You can trigger the Logic App with a `curl` command:

```
$ curl -X POST '$(pulumi stack output endpoint)' -d '"Hello World"' -H 'Content-Type: application/json'
```

The POST body will be saved into a new document in the Cosmos DB collection.

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

```bash
$ pulumi destroy
$ pulumi stack rm
```
155 changes: 155 additions & 0 deletions azure-nextgen-ts-cosmosdb-logicapp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright 2016-2021, Pulumi Corporation. All rights reserved.

import * as authorization from "@pulumi/azure-nextgen/authorization/latest";
import * as documentdb from "@pulumi/azure-nextgen/documentdb/latest";
import * as logic from "@pulumi/azure-nextgen/logic/latest";
import * as resources from "@pulumi/azure-nextgen/resources/latest";
import * as storage from "@pulumi/azure-nextgen/storage/latest";
import * as web from "@pulumi/azure-nextgen/web/latest";
import * as pulumi from "@pulumi/pulumi";

// Create an Azure Resource Group
const resourceGroup = new resources.ResourceGroup("logicappdemo-rg", {
resourceGroupName: "logicappdemo-rg",
});

// Create an Azure resource (Storage Account)
const storageAccount = new storage.StorageAccount("logicappdemosa", {
accountName: "logicappdemosa21",
resourceGroupName: resourceGroup.name,
sku: {
name: storage.SkuName.Standard_LRS,
},
kind: storage.Kind.StorageV2,
});

// Cosmos DB Account
const cosmosdbAccount = new documentdb.DatabaseAccount("logicappdemo-cdb", {
accountName: "logicappdemo-cdb",
resourceGroupName: resourceGroup.name,
databaseAccountOfferType: documentdb.DatabaseAccountOfferType.Standard,
locations: [{ locationName: resourceGroup.location, failoverPriority: 0 }],
consistencyPolicy: {
defaultConsistencyLevel: documentdb.DefaultConsistencyLevel.Session,
},
});

// Cosmos DB Database
const db = new documentdb.SqlResourceSqlDatabase("db", {
databaseName: "sqldb",
resourceGroupName: resourceGroup.name,
accountName: cosmosdbAccount.name,
resource: {
id: "sqldb",
},
});

// Cosmos DB SQL Container
const dbContainer = new documentdb.SqlResourceSqlContainer("container", {
containerName: "container",
resourceGroupName: resourceGroup.name,
accountName: cosmosdbAccount.name,
databaseName: db.name,
resource: {
id: "container",
},
});

const accountKeys = pulumi.all([resourceGroup.name, cosmosdbAccount.name]).apply(async args => {
const resourceGroupName = args[0];
const accountName = args[1];
const accountKeys = documentdb.listDatabaseAccountKeys({
accountName: accountName,
resourceGroupName: resourceGroupName,
});
return await accountKeys;
});

const clientConfig = pulumi.output(authorization.getClientConfig());

const apiId = pulumi.interpolate`/subscriptions/${clientConfig.subscriptionId}/providers/Microsoft.Web/locations/${resourceGroup.location}/managedApis/documentdb`;

/*
* API Connection to be used in a Logic App
*/
const connection = new web.Connection("cosmosdbConnection", {
connectionName: "cosmosdbConnection",
resourceGroupName: resourceGroup.name,
properties: {
displayName: "cosmosdb_connection",
api: {
id: apiId,
},
parameterValues: {
databaseAccount: cosmosdbAccount.name,
accessKey: accountKeys.primaryMasterKey,
},
},
});

/*
* Logic App with an HTTP trigger and Cosmos DB action
*/
const workflow = new logic.Workflow("workflow", {
workflowName: "httpToCosmos",
resourceGroupName: resourceGroup.name,
definition: {
$schema: "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
contentVersion: "1.0.0.0",
parameters: {
$connections: {
defaultValue: {},
type: "Object",
},
},
triggers: {
Receive_post: {
type: "Request",
kind: "Http",
inputs: {
method: "POST",
schema: {
properties: {},
type: "object",
},
},
},
},
actions: {
write_body: {
type: "ApiConnection",
inputs: {
body: {
data: "@triggerBody()",
id: "@utcNow()",
},
host: {
connection: {
name: "@parameters('$connections')['documentdb']['connectionId']",
},
},
method: "post",
path: pulumi.interpolate`/dbs/${db.name}/colls/${dbContainer.name}/docs`,
},
},
},
},
parameters: {
$connections: {
value: {
documentdb: {
connectionId: connection.id,
connectionName: "logicapp-cosmosdb-connection",
id: apiId,
},
},
},
},
});

const callbackUrls = pulumi.all([resourceGroup.name, workflow.name]).apply(args => {
return logic.listWorkflowTriggerCallbackUrl({ resourceGroupName: args[0], workflowName: args[1], triggerName: "Receive_post" });
});

// Export the HTTP endpoint
export const endpoint = callbackUrls.value;
10 changes: 10 additions & 0 deletions azure-nextgen-ts-cosmosdb-logicapp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "azure-cosmosdb-logicapp",
"devDependencies": {
"@types/node": "latest"
},
"dependencies": {
"@pulumi/azure-nextgen": "^0.6.1",
"@pulumi/pulumi": "^2.0.0"
}
}
19 changes: 19 additions & 0 deletions azure-nextgen-ts-cosmosdb-logicapp/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true
},
"files": [
"index.ts"
]
}

0 comments on commit 2ea8d1b

Please sign in to comment.