title | description | services | documentationcenter | author | ms.assetid | ms.service | ms.devlang | ms.topic | ms.tgt_pltfrm | ms.workload | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Deploy resources with REST API and template | Microsoft Docs |
Use Azure Resource Manager and Resource Manager REST API to deploy a resources to Azure. The resources are defined in a Resource Manager template. |
azure-resource-manager |
na |
tfitzmac |
1d8fbd4c-78b0-425b-ba76-f2b7fd260b45 |
azure-resource-manager |
na |
conceptual |
na |
na |
10/26/2018 |
tomfitz |
This article explains how to use the Resource Manager REST API with Resource Manager templates to deploy your resources to Azure.
Tip
For help with debugging an error during deployment, see:
- View deployment operations to learn about getting information that helps you troubleshoot your error
- Troubleshoot common errors when deploying resources to Azure with Azure Resource Manager to learn how to resolve common deployment errors
You can either include your template in the request body or link to a file. When using a file, it can be a local file or an external file that is available through a URI. When your template is in a storage account, you can restrict access to the template and provide a shared access signature (SAS) token during deployment.
-
Set common parameters and headers, including authentication tokens.
-
If you don't have an existing resource group, create a resource group. Provide your subscription ID, the name of the new resource group, and location that you need for your solution. For more information, see Create a resource group.
PUT https://management.azure.com/subscriptions/<YourSubscriptionId>/resourcegroups/<YourResourceGroupName>?api-version=2018-05-01
With a request body like:
{
"location": "West US",
"tags": {
"tagname1": "tagvalue1"
}
}
-
Validate your deployment before executing it by running the Validate a template deployment operation. When testing the deployment, provide parameters exactly as you would when executing the deployment (shown in the next step).
-
Create a deployment. Provide your subscription ID, the name of the resource group, the name of the deployment, and a link to your template. For information about the template file, see Parameter file. For more information about the REST API to create a resource group, see Create a template deployment. Notice the mode is set to Incremental. To run a complete deployment, set mode to Complete. Be careful when using the complete mode as you can inadvertently delete resources that aren't in your template.
PUT https://management.azure.com/subscriptions/<YourSubscriptionId>/resourcegroups/<YourResourceGroupName>/providers/Microsoft.Resources/deployments/<YourDeploymentName>?api-version=2018-05-01
With a request body like:
{
"properties": {
"templateLink": {
"uri": "http://mystorageaccount.blob.core.windows.net/templates/template.json",
"contentVersion": "1.0.0.0"
},
"mode": "Incremental",
"parametersLink": {
"uri": "http://mystorageaccount.blob.core.windows.net/templates/parameters.json",
"contentVersion": "1.0.0.0"
}
}
}
If you want to log response content, request content, or both, include **debugSetting** in the request.
"debugSetting": {
"detailLevel": "requestContent, responseContent"
}
You can set up your storage account to use a shared access signature (SAS) token. For more information, see [Delegating Access with a Shared Access Signature](https://docs.microsoft.com/rest/api/storageservices/delegating-access-with-a-shared-access-signature).
- Instead of linking to files for the template and parameters, you can include them in the request body.
{
"properties": {
"mode": "Incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountType": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_ZRS",
"Premium_LRS"
],
"metadata": {
"description": "Storage Account type"
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
}
},
"variables": {
"storageAccountName": "[concat(uniquestring(resourceGroup().id), 'standardsa')]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"name": "[variables('storageAccountName')]",
"apiVersion": "2018-02-01",
"location": "[parameters('location')]",
"sku": {
"name": "[parameters('storageAccountType')]"
},
"kind": "StorageV2",
"properties": {}
}
],
"outputs": {
"storageAccountName": {
"type": "string",
"value": "[variables('storageAccountName')]"
}
}
},
"parameters": {
"location": {
"value": "eastus2"
}
}
}
}
- Get the status of the template deployment. For more information, see Get information about a template deployment.
GET https://management.azure.com/subscriptions/<YourSubscriptionId>/resourcegroups/<YourResourceGroupName>/providers/Microsoft.Resources/deployments/<YourDeploymentName>?api-version=2018-05-01
For deployments that fail, you can specify that an earlier deployment from your deployment history is automatically redeployed. To use this option, your deployments must have unique names so they can be identified in the history. If you don't have unique names, the current failed deployment might overwrite the previously successful deployment in the history. You can only use this option with root level deployments. Deployments from a nested template aren't available for redeployment.
To redeploy the last successful deployment if the current deployment fails, use:
"onErrorDeployment": {
"type": "LastSuccessful",
},
To redeploy a specific deployment if the current deployment fails, use:
"onErrorDeployment": {
"type": "SpecificDeployment",
"deploymentName": "<deploymentname>"
}
The specified deployment must have succeeded.
If you use a parameter file to pass parameter values during deployment, you need to create a JSON file with a format similar to the following example:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"webSiteName": {
"value": "ExampleSite"
},
"webSiteHostingPlanName": {
"value": "DefaultPlan"
},
"webSiteLocation": {
"value": "West US"
},
"adminPassword": {
"reference": {
"keyVault": {
"id": "/subscriptions/{guid}/resourceGroups/{group-name}/providers/Microsoft.KeyVault/vaults/{vault-name}"
},
"secretName": "sqlAdminPassword"
}
}
}
}
The size of the parameter file can't be more than 64 KB.
If you need to provide a sensitive value for a parameter (such as a password), add that value to a key vault. Retrieve the key vault during deployment as shown in the previous example. For more information, see Pass secure values during deployment.
- To specify how to handle resources that exist in the resource group but aren't defined in the template, see Azure Resource Manager deployment modes.
- To learn about handling asynchronous REST operations, see Track asynchronous Azure operations.
- For an example of deploying resources through the .NET client library, see Deploy resources using .NET libraries and a template.
- To define parameters in template, see Authoring templates.
- For guidance on how enterprises can use Resource Manager to effectively manage subscriptions, see Azure enterprise scaffold - prescriptive subscription governance.