| services | platforms | author |
|---|---|---|
azure-resource-manager |
nodejs |
haocs |
This sample explains how to use Azure Resource Manager templates to deploy your resources to Azure using the Azure SDK for Node.js.
When deploying an application definition with a template, you can provide parameter values to customize how the resources are created. You specify values for these parameters either inline or in a parameter file.
** On this page**
-
If you don't already have it, get node.js.
-
Clone the repository.
git clone git@github.com:Azure-Samples/resource-manager-node-template-deployment.git -
Install the dependencies.
cd resource-manager-node-template-deployment 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
setinstead ofexport. -
Run the sample.
// By default the script will use the ssh public key from your default ssh location node index.js [path/to/ssh_public_key] -
To clean up after index.js, run the cleanup script.
node cleanup.js <resourceGroupName> <deploymentName>
The sample 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 publicSSHKeyPath = process.argv[2] || "~/.ssh/id_rsa.pub";
var resourceClient;
//Sample Config
var randomIds = {};
var location = 'eastus';
var resourceGroupName = _generateRandomId('testrg', randomIds);
var deploymentName = _generateRandomId('testdeployment', randomIds);
var dnsLabelPrefix = _generateRandomId('testdnslable', randomIds);
///////////////////////////////////////
//Entrypoint for the sample script //
///////////////////////////////////////
msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain, function (err, credentials) {
if (err) return console.log(err);
resourceClient = new ResourceManagementClient(credentials, subscriptionId);
Then it creates a resource group into which the VM will be deployed.
var groupParameters = { location: location, tags: { sampletag: 'sampleValue' } };
resourceClient.resourceGroups.createOrUpdate(resourceGroupName, groupParameters, callback);
Now, the sample loads the template and deploys it into the resource group that it just created.
try {
var templateFilePath = path.join(__dirname, "templates/template.json");
var template = JSON.parse(fs.readFileSync(templateFilePath, 'utf8'));
var publicSSHKey = fs.readFileSync(expandTilde(publicSSHKeyPath), 'utf8');
} catch (ex) {
return callback(ex);
}
var parameters = {
"sshKeyData": {
"value": publicSSHKey
},
"vmName": {
"value": "azure-deployment-sample-vm"
},
"dnsLabelPrefix": {
"value": dnsLabelPrefix
}
};
var deploymentParameters = {
"properties": {
"parameters": parameters,
"template": template,
"mode": "Incremental"
}
};
resourceClient.deployments.createOrUpdate(resourceGroupName,
deploymentName,
deploymentParameters,
callback);
Please refer to Azure SDK for Node for more information.