Manage Azure resources and resource groups with Node.js
This sample explains how to manage your resources and resource groups in Azure using the Azure SDK for Node.js.
On this page
Tasks done in this sample
- Create a resource group
- List a resource group
- Update a resource group
- Create a key vault resource in the resource group
- Get details for a given resource
- Export the resource group template
Running this sample
If you don't already have it, get node.js.
Clone the repository.
git clone git@github.com:Azure-Samples/resource-manager-node-resources-and-groups.git
Install the dependencies.
cd resource-manager-node-resources-and-groups npm install
Create an Azure service principal either through Azure CLI, PowerShell or the portal.
Set the following environment variables using the information from the service principle that you created.
export AZURE_SUBSCRIPTION_ID={your subscription id} export CLIENT_ID={your client id} export APPLICATION_SECRET={your client secret} export DOMAIN={your tenant id as a guid OR the domain name of your org <contosocorp.com>}
[AZURE.NOTE] On Windows, use
set
instead ofexport
.Run the sample.
node index.js
To clean up after index.js, run the cleanup script.
node cleanup.js <resourceGroupName> <resourceName>
What is index.js doing?
The sample creates, lists and updates a website. It starts by logging in using your service principal.
_validateEnvironmentVariables();
var clientId = process.env['CLIENT_ID'];
var domain = process.env['DOMAIN'];
var secret = process.env['APPLICATION_SECRET'];
var subscriptionId = process.env['AZURE_SUBSCRIPTION_ID'];
var resourceClient;
//Sample Config
var randomIds = {};
var location = 'westus';
var resourceGroupName = _generateRandomId('testrg', randomIds);
var resourceName = _generateRandomId('testresource', randomIds);
var resourceProviderNamespace = 'Microsoft.KeyVault';
var parentResourcePath = '';
var resourceType = 'vaults';
var apiVersion = '2015-06-01';
///////////////////////////////////////
//Entrypoint for the sample script //
///////////////////////////////////////
msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain, function (err, credentials) {
if (err) return console.log(err);
resourceClient = new ResourceManagementClient(credentials, subscriptionId);
With that set up, the sample performs these operations.
Create a resource group
var groupParameters = { location: location, tags: { sampletag: 'sampleValue' } };
resourceClient.resourceGroups.createOrUpdate(resourceGroupName, groupParameters, callback);
List resource groups
List the resource groups in your subscription.
resourceClient.resourceGroups.list(callback);
Update a resource group
The sample adds a tag to the resource group.
var groupParameters = { location: location, tags: { sampletag: 'helloworld' } };
resourceClient.resourceGroups.createOrUpdate(resourceGroupName, groupParameters, callback);
Create a key vault in the resource group
var keyvaultParameter = {
location : "West US",
properties : {
sku : {
family : 'A',
name : 'standard'
},
accessPolicies : [],
enabledForDeployment: true,
enabledForTemplateDeployment: true,
tenantId : domain
},
tags : {}
};
resourceClient.resources.createOrUpdate(resourceGroupName,
resourceProviderNamespace,
parentResourcePath,
resourceType,
resourceName,
apiVersion,
keyvaultParameter,
callback);
Get a resource
resourceClient.resources.get(resourceGroupName,
resourceProviderNamespace,
parentResourcePath,
resourceType,
resourceName,
apiVersion,
callback);
Export the resource group template
You can export the resource group as a template and then use that to deploy your resources to Azure.
var rgParameter = {
resources: ['*']
};
resourceClient.resourceGroups.exportTemplate(resourceGroupName, rgParameter, callback);
Delete a resource
resourceClient.resources.deleteMethod(resourceGroupName,
resourceProviderNamespace,
parentResourcePath,
resourceType,
resourceName,
apiVersion,
callback);
More information
Please refer to Azure SDK for Node for more information.