Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
85 lines (63 sloc) 2.42 KB
services platforms author
resources
nodejs
amarzavery

Manage resources using Managed Service Identity using node.js

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 https://github.com/Azure-Samples/compute-node-msi-vm.git
    
  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.

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.

// 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();
You can’t perform that action at this time.