This article will show you how to deploy a basic website with a database to Azure. It will use Resource Manager templates. Check out the article on Resource Manager basics if you are unfamiliar.
We start with the familiar empty template and set the contentVersion
property. Add any of the following snippets to the resources
property:
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": []
}
Deploying the Web App
You run web apps (in Azure terminology, an “app service”) in “app service plans.” You need to create one if you don’t want to reuse an existing one.
The resource template for an app service plan is the following fragment. ServerFarm
is the old name for app service plans. The definition is available here:
{
"apiVersion": "2015-08-01",
"location": "[resourceGroup().location]",
"name": "demo-asp1",
"type": "Microsoft.Web/serverfarms",
"sku": {
"name": "F1",
"tier": "Free"
}
}
This will create the smallest (and free) app service plan available. To create a larger app service, change the sku
property object. For example, to create a basic service plan with medium size and 3 instances, specify:
"sku": {
"name": "B2",
"capacity": "3"
}
You can find the other values to specify in the portal or in Microsoft Docs.
Next, you can define the app service itself (the website). Add another resource to the array using this fragment:
{
"apiVersion": "2015-08-01",
"location": "[resourceGroup().location]",
"name": "demo-app1",
"type": "Microsoft.Web/sites",
"properties": {
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms','demo-asp1')]"
},
"dependsOn": ["[resourceId('Microsoft.Web/serverfarms','demo-asp1')]"]
}
The first four properties are analogous to other resources. An important property is the serverFarmId
, which identifies the app service plan created above. Because this resource depends on the other resource existing, it needs to be in the dependsOn
array.
Deploying the Database
Like above, you need a SQL server before you can create a SQL database:
{
"apiVersion": "2014-01-01",
"location": "[resourceGroup().location]",
"name": "demo-sql1",
"type": "Microsoft.Sql/servers",
"properties": {
"administratorLogin": "demo",
"administratorLoginPassword": "Password12!"
}
}
Be aware that the apiVersion
property is older! Providing the administrator login username and password like this is not recommended. See our next article on using KeyVault for a better approach.
Now you can create the database:
{
"apiVersion": "2015-01-01",
"location": "[resourceGroup().location]",
"name": "demo-sql1/demo-sql1-db1",
"type": "Microsoft.Sql/servers/databases",
"dependsOn": ["[resourceId('Microsoft.Sql/servers','demo-sql1')]"],
"properties": {
"edition": "Basic",
"requestedServiceObjectiveName": "Basic"
}
}
Note that the name specified must include the server you are deploying to. This is different from how you referenced the app service plan above.
Finally, you need to give the website access to the database by deploying a firewall rule:
{
"apiVersion": "2014-04-01",
"location": "[resourceGroup().location]",
"type": "Microsoft.Sql/servers/firewallrules",
"dependsOn": ["[resourceId('Microsoft.Sql/servers','demo-sql1')]"],
"name": "demo-sql1/AllowAllWindowsAzureIps",
"properties": {
"endIpAddress": "0.0.0.0",
"startIpAddress": "0.0.0.0"
}
}
To access the database from your computer, you must add another rule. Replace the IP address range with your public IP address:
{
"apiVersion": "2014-04-01",
"location": "[resourceGroup().location]",
"type": "Microsoft.Sql/servers/firewallrules",
"dependsOn": ["[resourceId('Microsoft.Sql/servers','demo-sql1')]"],
"name": "demo-sql1/MyIP",
"properties": {
"endIpAddress": "x.x.x.x",
"startIpAddress": "x.x.x.x"
}
}
Adding App Settings and Connection Strings
You deploy app settings as a separate resource definition:
{
"apiVersion": "2016-03-01",
"location": "[resourceGroup().location]",
"type": "Microsoft.Web/sites/config",
"name": "demo-app1/appsettings",
"dependsOn": ["[resourceId('Microsoft.Web/sites','demo-app1')]"],
"properties": {
"key1": "value1",
"key2": "value2"
}
}
You can specify connection strings very similarly:
{
"apiVersion": "2016-03-01",
"location": "[resourceGroup().location]",
"type": "Microsoft.Web/sites/config",
"name": "demo-app1/connectionstrings",
"dependsOn": ["[resourceId('Microsoft.Web/sites','demo-app1')]"],
"properties": {
"DefaultConnection": {
"value": "",
"type": "SQLAzure"
}
}
}
The connection string for SQL Server:
Data Source=tcp:demo-sql1.database.windows.net,1433;Initial Catalog=demo-sql1-db1;User [email protected];Password=Password12!;
Automatic Deployment from GitHub
To enable automatic deployment from GitHub:
{
"apiVersion": "2015-08-01",
"location": "[resourceGroup().location]",
"type": "Microsoft.Web/sites/sourcecontrols",
"name": "demo-app1/web",
"dependsOn": [
"[resourceId('Microsoft.Web/sites','demo-app1')],",
"[resourceId('Microsoft.Web/sites/config','demo-app1','appsettings')],",
"[resourceId('Microsoft.Web/sites/config','demo-app1','connectionstrings')]"
],
"properties": {
"RepoUrl": "https://github.com/aaronkai/html5-bootstrap",
"branch": "master",
"IsManualIntegration": true
}
}
For automatic deployment, set IsManualIntegration
to false
and ensure a GitHub connection is configured in the Azure portal.
Child resources
As you browse the other template files, you may come across child resources defined underneath a parent:
{
"apiVersion": "2015-08-01",
"location": "[resourceGroup().location]",
"name": "demo-app1",
"type": "Microsoft.Web/sites",
"properties": {
"serverFarmId": "[resourceId('Microsoft.Web/serverFarms','demo-asp1')]"
},
"dependsOn": [
"demo-asp1"
],
"resources": [
{
"apiVersion": "2016-03-01",
"location": "[resourceGroup().location]",
"type": "config",
"name": "appsettings",
"dependsOn": [
"demo-app1"
],
"properties": {
"key1": "value1",
"key2": "value2"
}
}
]
}
Using this approach, you can shorten the names and type values. The parent’s name and type are automatically appended. You can define these dependencies up to five levels deep. Unfortunately, you must still define the dependencies explicitly.
See Microsoft Docs for more information on defining dependencies in Azure Resource Templates.