Resources

Resources are mandatory in resource group deployment. The resource key contains an array of resource objects of different types. Different resource types will have different parameters that can be set. The following code sample, taken from https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-templates-resources, shows the entire structure of one resource entry:

"resources": [
{
"condition": "<boolean-value-whether-to-deploy>",
"apiVersion": "<api-version-of-resource>",
"type": "<resource-provider-namespace/resource-type-name>",
"name": "<name-of-the-resource>",
"location": "<location-of-resource>",
"tags": { ... },
"comments": "<your-reference-notes>",
"copy": { ... },
"dependsOn": [
"<array-of-related-resource-names>"
],
"properties": {
"<settings-for-the-resource>",
"copy": [
{
"name": ,
"count": ,
"input": {}
}
]
},
"sku": {
"name": "<sku-name>",
"tier": "<sku-tier>",
"size": "<sku-size>",
"family": "<sku-family>",
"capacity": <sku-capacity>
},
"kind": "<type-of-resource>",
"plan": {
"name": "<plan-name>",
"promotionCode": "<plan-promotion-code>",
"publisher": "<plan-publisher>",
"product": "<plan-product>",
"version": "<plan-version>"
},
"resources": [
"<array-of-child-resources>"
]
}
]

In order to deploy a resource group containing a storage account, we can cut down the amount of code by selecting only the necessary properties, such as location, storage account stock-keeping unit (SKU), and name. You can simply create a large hashtable containing your necessary keys, and convert it to JSON later on. This way, you can easily make use of the language capabilities of PowerShell:

# Crafting a template for a simple storage account
$template = @{
'$schema' = "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#"
contentVersion = '1.0.0.0'
resources = @(
@{
type = 'Microsoft.Storage/storageAccounts'
name = "contoso$((1..8 | ForEach-Object { [char[]](97..122) | Get-Random }) -join '')"
apiVersion = '2016-01-01'
location = 'westeurope'
sku = @{
name = 'Standard_LRS'
}
kind = 'Storage'
}
)
}

# You can script your template and when done, export to JSON
$template | ConvertTo-Json -Depth 100 | Out-File -FilePath .StorageAccountStatic.json -Encoding UTF8

This simple template can then be deployed into a new resource group. To do this, a resource group is needed first. Afterwards, the JSON template can be applied and deployed. Azure PowerShell first checks whether the template is valid, and then initiates the deployment. If the template deployment has not been started in a background job by using the parameter AsJob, the cmdlet will wait until the resource group deployment has finished before returning control to the script:

# Create a new resource group
$resourceGroupName = 'NastroAzzurro'
$location = 'westeurope'
New-AzureRmResourceGroup -Name $resourceGroupName -Location $location

# Deploy your JSON template into the new resource group
$deployment = @{
Name = 'FirstDeployment'
ResourceGroupName = $resourceGroupName
TemplateFile = '.StorageAccountStatic.json'
Verbose = $true
}

New-AzureRmResourceGroupDeployment @deployment

The default deployment mode is incremental. That means that your resource groups can be simply extended incrementally, later on. If you want to redeploy a resource group fully, the mode complete can be chosen instead.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.29.89