Is there a way, using Azure Resource Manager (ARM) templates, to deploy a storage account only if that storage account does not exist?
The below template will create:
- App Insights resource (shared)
- Storage Account (shared)
- App Plan
- App instance with configuration containing the App Insights Instrumentation Key and the Storage Account Connection String.
I would like the first two steps to be optional meaning if they already exist, just use them.
Only thing I have found so far is the pattern of newOrExisting, but that just does not make any sense. The script should be able to tell if those resource already exist and simply skip creation.
The same App Insights and Storage Account will be used by other deployment scripts so it would be nice if the template could just figure it out.
Thanks for any help!
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"environmentName": {
"type": "string",
"defaultValue": "Dev",
"allowedValues": [
"Dev",
"Test",
"Prod"
]
}
},
"variables": {
"rgLocation": "[resourceGroup().location]",
"insightsName": "[concat('Insights-', parameters('environmentName'))]",
"appName": "[concat('MyAppName-', parameters('environmentName'))]",
"genStorageName": "[concat('blgenstorage', parameters('environmentName'))]"
},
{
"comments": "Creates a general storage account that is used to save various data, including configurations.",
"name": "[variables('genStorageName')]",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2017-06-01",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"location": "[variables('rgLocation')]",
"tags": {},
"properties": {}
},
{
"comments": "Creates the service plan under which the web app will live.",
"name": "[concat('ServicePlan-MyApp-', parameters('environment'))]",
"type": "Microsoft.Web/serverfarms",
"apiVersion": "2016-09-01",
"kind": "app",
"location": "[variables('rgLocation')]",
"tags": {},
"properties": {
"name": "[concat('ServicePlan-MyApp-', parameters('environmentName'))]",
"perSiteScaling": "false",
"reserved": "false"
},
"sku": {
"name": "S1",
"tier": "Standard",
"size": "S1",
"family": "S",
"capacity": 1
}
},
{
"comments": "Primary web app deployment.",
"name": "[variables('appName')]",
"type": "Microsoft.Web/sites",
"apiVersion": "2016-08-01",
"kind": "app",
"location": "[variables('rgLocation')]",
"tags": {},
"dependsOn": [
"[resourceId('Microsoft.Web/serverfarms', concat('ServicePlan-MyApp-', variables('envShortName')))]",
"[resourceId('Microsoft.Storage/storageAccounts', variables('genStorageName'))]",
"[resourceId('microsoft.insights/components', variables('insightsName'))]"
],
"properties": {
"enabled": true,
"hostNameSslStates": [
{
"name": "[concat(variables('appName'), '.azurewebsites.net')]",
"sslState": "Disabled"
},
{
"name": "[concat(variables('appName'), '.scm.azurewebsites.net')]",
"sslState": "Disabled"
}
],
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', concat('ServicePlan-MyApp-', parameters('environmentName')))]",
"siteConfig": {
"numberOfWorkers": 1,
"defaultDocuments": [
"Default.htm",
"Default.html",
"Default.asp",
"index.htm",
"index.html",
"iisstart.htm",
"default.aspx",
"index.php",
"hostingstart.html"
],
"netFrameworkVersion": "v4.6",
"appSettings": [
{
"name": "AppInsightsInstrumentationKey",
"value": "[reference(resourceId('Microsoft.Insights/components', variables('insightsName')), '2015-05-01').InstrumentationKey]"
}
],
"connectionStrings": [
{
"name": "AzureStorage",
"connectionString": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('genStorageName')), '2017-06-01').keys[0].value]",
"type": "Custom"
}
],
"alwaysOn": true,
"managedPipelineMode": "Integrated",
"virtualApplications": [
{
"virtualPath": "/",
"physicalPath": "site\\wwwroot",
"preloadEnabled": false
}
],
"autoHealEnabled": false,
"vnetName": ""
},
"microService": "WebSites",
"clientAffinityEnabled": false,
"clientCertEnabled": false,
"hostNamesDisabled": false
}
}