Provisioning infrastructure in a timely and reliable manner is essential for agile development. One well-liked method that lets you use code to automate resource management and provisioning is infrastructure as code (IaC). Azure Resource Manager templates are one IaC solution for Azure (ARM templates).
A declarative framework for defining the resources and architecture for Azure deployment is provided by ARM templates. You outline the intended result rather than the specific processes involved in implementation. A JSON file with personalized template expressions is an ARM template. You specify logic and dynamic values in the template expressions that decide which Azure resources to install. The resources and their configurations are defined by these templates using a declarative paradigm, which facilitates deployment automation and guarantees consistency between environments. We will go over the fundamentals of Azure ARM templates in this article and walk you through the steps of utilizing an ARM template to create a single database.
Azure ARM Templates
Azure ARM templates consist of JSON files that contain the resources required for an Azure solution to be deployed. A variety of Azure services, including virtual machines, storage accounts, databases, and more, can be deployed using these templates. ARM templates have the following essential features:
- Statistical Declaration: Declarative syntax is used in ARM templates, so you provide the intended state of the infrastructure and Azure Resource Manager handles the technical details.
- Definition of Resource: Every Azure resource, such as a database or virtual machine, is described in the ARM template. Each resource's characteristics and configurations are listed in the template.
- Parameters and Variables: By supporting parameters and variables, ARM templates let you tailor deployments to suit various requirements or environments.
- Dependencies: To make sure resources are distributed in the right order, dependencies between them can be defined.
- Template Functions: ARM templates have functions that let you do out tasks during the deployment, such as manipulating strings or performing math operations.
Let's see the deployment of an ARM Template to create a single database.
Prerequisites: Before proceeding, ensure you have the following:
- Azure subscription.
- Basic understanding of ARM templates.
Step 1: Define the ARM Template Structure: Start by creating a new JSON file and defining the basic structure of your ARM template. The template consists of sections like parameters, variables, resources, and outputs. Here's a basic template structure:
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "resources": [], "outputs": {} }
Step 2: Add Parameters: Define parameters for your SQL database, server, and credentials. Parameters allow you to customize the deployment for different scenarios:
"parameters": { "serverName": { "type": "string", "defaultValue": "yourServerName", "metadata": { "description": "Name of the SQL server." } }, "databaseName": { "type": "string", "defaultValue": "yourDatabaseName", "metadata": { "description": "Name of the SQL database." } }, "adminLogin": { // Add other parameters for server admin login }, "adminPassword": { // Add other parameters for server admin password } }
Step 3: Define Variables: Variables help simplify your template by allowing you to reuse values. For example, constructing a server URL based on the provided parameters:
"variables": { "serverUrl": "[concat('https://', parameters('serverName'), '.database.windows.net')]" }
Step 4: Add a SQL Server Resource: Include a resource section for the SQL Server. Specify properties such as version , location etc.
in below code snippets Resource type SQL is server is added. Its version mentioned is 2017 and Location is mentioned as Default location of resource group location.
"resources": [ { "type": "Microsoft.Sql/servers", "apiVersion": "2017-10-01-preview", "name": "[parameters('serverName')]", "location": "[resourceGroup().location]", } ]
Step 5: Add SQL Database Resource: Include a resource section for the SQL database, specifying properties like name, edition, collation, and server name.
In this below code, Resource type is mentioned as SQL server Database. Location is mentioned as Default location of Resource Group.
{ "type": "Microsoft.Sql/servers/databases", "apiVersion": "2017-10-01-preview", "name": "[concat(parameters('serverName'), '/', parameters('databaseName'))]", "location": "[resourceGroup().location]", "dependsOn": [ "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]" ] }
Step 6: Deploy the ARM Template:
Go to the Azure Portal and navigate to the desired resource group. Click on the "Add" button to create a new resource.
Search for "Template deployment" in the marketplace. Select the "Template deployment" option and click on "Create." Select the "Build your own template in editor" option. Edit the Template, provide the necessary parameters. Then, click on "Review + create" to start the deployment.
Let's Create the Database named as "ARMTemplateDatabase" using this method in the Resource group "ARMTEST", and in SQL Server named as "testarmtemplate" following the above steps. Login to Azure Portal, select resource group and click on "Create a resource" option. Serach for "Deploy a custom template" option.
Click on "Build you own template in the editor" option to open the editor
In the template editor, I'll be using the below JSON script. The script is changed to meet the needs. the original script is derived from the azure QuickStart templates for a single database. The five parameters in the script are the location, login, password, server name, and sqldbname. SQL server and SQL database are the two different resource types. i have to create a Database, named as ARMTemplateDatabase, on the server, Testarmtemplate, These are mentioned as:
- SQL Server Name -- "testarmtemplate"
- Database Name -- "ARMTemplateDatabase"
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "metadata": { "_generator": { "name": "bicep", "version": "0.12.40.16777", "templateHash": "16856611863128783179" } }, "parameters": { "serverName": { "type": "string", "defaultValue": "testarmtemplate", "metadata": { "description": "The name of the SQL logical server." } }, "sqlDBName": { "type": "string", "defaultValue": "ARMTemplateDatabase", "metadata": { "description": "The name of the SQL Database." } }, "location": { "type": "string", "defaultValue": "[resourceGroup().location]", "metadata": { "description": "Location for all resources." } }, "administratorLogin": { "type": "string", "metadata": { "description": "The administrator username of the SQL logical server." } }, "administratorLoginPassword": { "type": "secureString", "metadata": { "description": "The administrator password of the SQL logical server." } } }, "resources": [ { "type": "Microsoft.Sql/servers", "apiVersion": "2022-05-01-preview", "name": "[parameters('serverName')]", "location": "[parameters('location')]", "properties": { "administratorLogin": "[parameters('administratorLogin')]", "administratorLoginPassword": "[parameters('administratorLoginPassword')]" } }, { "type": "Microsoft.Sql/servers/databases", "apiVersion": "2022-05-01-preview", "name": "[format('{0}/{1}', parameters('serverName'), parameters('sqlDBName'))]", "location": "[parameters('location')]", "sku": { "name": "Standard", "tier": "Standard" }, "dependsOn": [ "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]" ] } ] }
Once you edit the script you will see that template will be seen as Customized Template and it will show number of Resources used in the Script. in Our case it is showing as 2.
Review the subscription and resource group and select "Review + create " option. This will start the Deployment process. You will get notification as shown below stating the Deployment is in Progress.
Once Deployment is finished, we can see the database is created.
We can verify this by connecting this database using SSMS
In conclusion, ARM templates provide an effective means of defining and deploying Azure resources in a repeatable and consistent fashion. With these steps, you can lay the groundwork for Infrastructure as Code practices in your Azure environment by creating an ARM template for the deployment of an Azure SQL Database. Tailor the template to your own needs and take advantage of the extra capabilities that ARM templates have to offer for more complex situations.