Manage resources using Managed Service Identity using node.js

Senast uppdaterad: 2017-09-22
Redigera på GitHub

This sample demonstrates how to manage Azure resources via Managed Service Identity using the node.js SDK with typescript.

On this page

Create an Azure VM with MSI extension

Azure Compute VM with MSI

Run this sample

  1. If you don't already have it, get the latest LTS version of node.js.

  2. Clone the repository.

    git clone
  3. Install the dependencies.

    cd compute-node-msi-vm
    npm install
  4. Set the following environment variables.

    export AZURE_SUBSCRIPTION_ID={your subscription id}
    • optionally you can also set the port. If not set then it will use the default port 50342. This should be the same value that you specified while creating the vm with SMI extension. export MSI_PORT={port numer}

    [AZURE.NOTE] On Windows, use set instead of export.

  5. Run the sample.

    node dist/lib/index.js

What is index.js doing?

Create an MSI Token Provider

Initialize subscription_id, tenant_id and port from environment variables. typescript public domain: string = process.env['DOMAIN']; public subscriptionId: string = process.env['AZURE_SUBSCRIPTION_ID']; public port: number = process.env['MSI_PORT'] ? parseInt(process.env['MSI_PORT']) : 50342; //If not provided then we assume the default port

Now, we will create token credential using the msi login. javascript // Create Managed Service Identity as the token provider credentials = await msRestAzure.loginWithMSI({ port: this.state.port });

Create a resource client and list resource groups

Now, we will create a resource management client using Managed Service Identity token provider.

this.resourceClient = new ResourceManagementClient(credentials, this.state.subscriptionId);
let finalResult = await this.resourceClient.resourceGroups.list();