amarzavery / resources-node-manage-resources-with-msi

sample that shows how to use msi with node sdk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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();

About

sample that shows how to use msi with node sdk

License:MIT License


Languages

Language:TypeScript 100.0%