Azure Classroomnotes 01/Jul/2022

ARM Templates Contd..

  • Using variables in ARM Templates: Refer Here
  • Variables are values which are used often in the template but not expected to passed as input
  • Refer Here for the changes done
    Preview
  • Added primary and secondary databases in two different regions Refer Here for the changes done.
    Preview
  • Refer Here for the documentation of outputs section.
  • Refer Here for the outputs section added
  • The template so far
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "primaryregion": {
            "type": "string",
            "metadata": {
                "description": "This is the primary region"
            },
            "defaultValue": "southindia"
        },
        "secondaryregion": {
            "type": "string",
            "metadata": {
                "description": "This is the primary region"
            },
            "defaultValue": "centralindia"
        },
        "primary-cidr": {
            "type": "string",
            "metadata": {
                "description": "This is the primary vnet address space"
            },
            "defaultValue": "10.0.0.0/16"
        },
        "secondary-cidr": {
            "type": "string",
            "metadata": {
                "description": "This is the secondary vnet address space"
            },
            "defaultValue": "10.1.0.0/16"
        },
        "subnet-names": {
            "type": "array",
            "metadata": {
                "description": "subnet name"
            },
            "defaultValue": [ "web", "app", "db", "mgmt" ]
        },
        "primary-subnet-cidr-format": {
            "type": "string",
            "metadata": {
                "description": "subnet cidr format"
            },
            "defaultValue": "10.0.{0}.0/24"
        },
        "secondary-subnet-cidr-format": {
            "type": "string",
            "metadata": {
                "description": "subnet cidr format"
            },
            "defaultValue": "10.1.{0}.0/24"
        },
        "dbusername": {
            "type": "string",
            "metadata": {
                "description": "username of the database"
            },
            "defaultValue": "qtdevops"
        },
        "dbpassword": {
            "type": "securestring",
            "metadata": {
                "description": "Password for the database server"
            }
        }

    },
    "variables": {
        "primary-vnet-name": "ntier-primary-vnet",
        "secondary-vnet-name": "ntier-secondary-vnet",
        "randomValue": "[uniqueString(resourceGroup().id)]",
        "primary": "primary",
        "secondary": "secondary",
        "primary-db-server": "[format('qtdbsrv{0}{1}',variables('primary'), variables('randomValue'))]",
        "primary-db": "[format('qtdb{0}',variables('primary'))]",
        "secondary-db-server": "[format('qtdbsrv{0}{1}',variables('secondary'), variables('randomValue'))]",
        "secondary-db": "[format('qtdb{0}',variables('secondary'))]"



    },
    "resources": [
        {
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2020-11-01",
            "name": "[variables('primary-vnet-name')]",
            "location": "[parameters('primaryregion')]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [ "[parameters('primary-cidr')]" ]
                }
            }
        },
        {
            "type": "Microsoft.Network/virtualNetworks/subnets",
            "apiVersion": "2020-11-01",
            "name": "[format('{0}/{1}',variables('primary-vnet-name'),parameters('subnet-names')[copyIndex()])]",
            "properties": {
                "addressPrefix": "[format(parameters('primary-subnet-cidr-format'),copyIndex())]"
            },
            "copy": {
                "name": "primary-subnet-copy",
                "count": "[length(parameters('subnet-names'))]",
                "mode": "Serial"

            },
            "dependsOn": [
                "[resourceId('Microsoft.Network/virtualNetworks',variables('primary-vnet-name'))]"
            ]

        },
        {
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2020-11-01",
            "name": "[variables('secondary-vnet-name')]",
            "location": "[parameters('secondaryregion')]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [ "[parameters('secondary-cidr')]" ]
                }
            }
        },
        {
            "type": "Microsoft.Network/virtualNetworks/subnets",
            "apiVersion": "2020-11-01",
            "name": "[format('{0}/{1}',variables('secondary-vnet-name'),parameters('subnet-names')[copyIndex()])]",
            "properties": {
                "addressPrefix": "[format(parameters('secondary-subnet-cidr-format'),copyIndex())]"
            },
            "copy": {
                "name": "secondary-subnet-copy",
                "count": "[length(parameters('subnet-names'))]",
                "mode": "Serial"

            },
            "dependsOn": [
                "[resourceId('Microsoft.Network/virtualNetworks',variables('secondary-vnet-name'))]"
            ]

        },
        {
            "name": "[variables('primary-db-server')]",
            "type": "Microsoft.Sql/servers",
            "apiVersion": "2014-04-01",
            "location": "[parameters('primaryregion')]",
            "tags": {
                "displayName": "primary-sql-server"
            },
            "properties": {
                "administratorLogin": "[parameters('dbusername')]",
                "administratorLoginPassword": "[parameters('dbpassword')]"
            },
            "resources": [
                {
                    "type": "firewallRules",
                    "apiVersion": "2014-04-01",
                    "dependsOn": [
                        "[resourceId('Microsoft.Sql/servers', variables('primary-db-server'))]"
                    ],
                    "location": "[parameters('primaryregion')]",
                    "name": "PrimaryServerFirewall",
                    "properties": {
                        "startIpAddress": "0.0.0.0",
                        "endIpAddress": "255.255.255.255"
                    }
                }
            ]
        },
        {
            "name": "[format('{0}/{1}',variables('primary-db-server'),variables('primary-db'))]",
            "type": "Microsoft.Sql/servers/databases",
            "apiVersion": "2014-04-01",
            "location": "[parameters('primaryregion')]",
            "tags": {
                "displayName": "primarysqldb"
            },
            "dependsOn": [
                "[resourceId('Microsoft.Sql/servers', variables('primary-db-server'))]"
            ],
            "properties": {
                "collation": "SQL_Latin1_General_CP1_CI_AS",
                "edition": "Basic",
                "maxSizeBytes": "1073741824",
                "requestedServiceObjectiveName": "Basic"
            }
        },
        {
            "name": "[variables('secondary-db-server')]",
            "type": "Microsoft.Sql/servers",
            "apiVersion": "2014-04-01",
            "location": "[parameters('secondaryregion')]",
            "tags": {
                "displayName": "secondary-sql-server"
            },
            "properties": {
                "administratorLogin": "[parameters('dbusername')]",
                "administratorLoginPassword": "[parameters('dbpassword')]"
            },
            "resources": [
                {
                    "type": "firewallRules",
                    "apiVersion": "2014-04-01",
                    "dependsOn": [
                        "[resourceId('Microsoft.Sql/servers', variables('secondary-db-server'))]"
                    ],
                    "location": "[parameters('secondaryregion')]",
                    "name": "SecondaryServerFirewall",
                    "properties": {
                        "startIpAddress": "0.0.0.0",
                        "endIpAddress": "255.255.255.255"
                    }
                }
            ]
        },
        {
            "name": "[format('{0}/{1}',variables('secondary-db-server'),variables('secondary-db'))]",
            "type": "Microsoft.Sql/servers/databases",
            "apiVersion": "2014-04-01",
            "location": "[parameters('secondaryregion')]",
            "tags": {
                "displayName": "secondarysqldb"
            },
            "dependsOn": [
                "[resourceId('Microsoft.Sql/servers', variables('secondary-db-server'))]"
            ],
            "properties": {
                "collation": "SQL_Latin1_General_CP1_CI_AS",
                "edition": "Basic",
                "maxSizeBytes": "1073741824",
                "requestedServiceObjectiveName": "Basic"
            }
        }
    ],
    "outputs": {
        "primary-db-server": {
            "type": "string",
            "value": "[reference(resourceId('Microsoft.Sql/servers',variables('primary-db-server'))).fullyQualifiedDomainName]"
        },
        "secondary-db-server": {
            "type": "string",
            "value": "[reference(resourceId('Microsoft.Sql/servers',variables('secondary-db-server'))).fullyQualifiedDomainName]"
        }
    }
}
  • Exercise: Try adding a primary web server in websubnet with public ip and secondary webserver in web subnet of secondary vnet with public ip

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner