Rajendra Gupta
three tier template

Use ARM templates to deploy Azure container instances with SQL Server Linux images

December 21, 2021 by

The Azure Resource Manager (ARM) template is a JavaScript Object Notation (JSON) file for deploying Azure resources automatically. You can use a declarative syntax to specify the resources, their configurations. Usually, if you need to deploy Azure resources, it might be a tiring experience of navigating through different services, their configurations. With the ARM templates, you no longer need to click and navigate around the portal. For example, you can use configure the template for Azure VM or Azure SQL Database deployment.

three tier template

Image Reference: Template design

Here are a few benefits of using the ARM templates.

  • Declarative syntax: You can define multiple Azure services such as VM, database, networks in a single deployment template
  • Repeatable results: The ARM templates can be used multiple times so that you can deploy Azure infrastructure repeatedly with consistency
  • Orchestration: Once we define multiple infrastructure pieces in the script, the resource manager orchestrates deploying resources correctly. It also starts their parallel deployment so that you can running infrastructure ASAP
  • Built-in validation: Once you submit the ARM templates, the resource manager goes through a validation check. If the validation is successful, it starts resource deployment. Therefore, it has lesser chances of stopping deployment in a half-finished state
  • Export templates: The Azure portal allows the export of the automation script and deploys it as an ARM template. Therefore, if you do not know JSON or ARM templates, you can still use them
  • Continuous integration and deployment: You can integrate the templates in the CI/CD tools such as Azure DevOps for fast, reliable infrastructure deployments

Template file section

The ARM template has the following sections:

  • Parameters: The parameters allow users to specify different values during the deployment of ARM templates
  • Variables: The variables enable users to specify different values during deployment to re-use the templates
  • User-defined functions: You can deploy custom functions for simplifying template deployment
  • Resources: These define Azure resources for deployment
  • Outputs: We can specify the output format from the deployed resources

Deploy Azure Container Instance(ACI) using ARM templates

Azure Container Instance is a managed service for running container instances with deployed images into Azure. You can use persisted or non-persisted(storage) for the container. The container deployment enables developers to deploy their code without waiting for infrastructure (VM, Servers) and installing databases. In the articles( TOC at the bottom), we explored deploying containers using the following ways with the SQL Server 2019 Linux image.

  • Azure Portal
  • Azure CLI
  • Use persisted storage for storing database files independently of the ACI lifecycle

This article explores deploying the Azure Container Instance for SQL Server On Linux using ARM templates. The quickest method to get the template script is to fill the ACI in the Azure portal initially. You can follow the article, Azure Container Instances for SQL Server 2019, to understand various container deployment options.

Create container instance

On the review page, you get an option – Download a template for automation.

Download a template for automation.

Click on the option, and it generates a template with the JSON script.

Template parameter and variables

In the template menu, you get multiple options, as outlined below.

  • Visualize template: The visualize templates show the resources graphically you will deploy using this ARM template. For example, it shows below the ACI graphically

    Resource visualizer

  • Download: You can download the JSON script for review and modification
  • Add to the library (preview): You can directly add this template into the Azure library to refer it for later deployments

Let’s download and extract the compressed files. It includes a parameter and template JSON script file.

Template and parameters JSON

Let’s open the files in the VS Code editor.

Parameters.JSON

This parameter file contains the input we provided during the Azure Container Instance ACI configuration. For example, Container name, image type, location, CPU cores, memory, restart policy, port number, environment variables.

Parameter.JSON file

The environment variable 1 belongs to the MSSQL SA password. It shows value as NULL because we used the secured string to hide the password in the script.

Environment variable for SA password

Template.json

The template JSON script file has two sections.

  • Parameter section with their types

    Each parameter is defined with a type. For example, the location is a string type

    Template.json file

  • Azure resource details

    In this section, the template defines the type of Azure resource and their details for configuration. For example, the ACIs has the following value –

    As you can see, the ACCEPT_EULA and MSSQL SA password values are from the specified environment variable parameter.

    Environment variables

Deploy ARM template for Azure Container Instance using default templates

You can also use the ACI template samples stored in the GitHub library.

Azure quickstart

We can use the Azure Container Instances – Linux container with public IP to deploy a single Linux container.

Linux container with public IP

Click on Deploy to Azure, and you get the following page.

Edit template and parameters

However, this template does not have options for SQL Server configurations such as environment variables for SA password and EULA. Therefore, you can click on the edit template to add the relevant details.

Here, I replace the content of the template and parameters with the script file downloaded earlier. Once modified, save both template and parameter files.

The updated Azure container instance deployment page looks as below. It displays all parameters and their values on this page.

Custom deployment

You can modify the values if required. If you specify any explicit values here, it overrides the parameters’ values specified in the parameter.json. For example, I modified the container name as azuresqlcontainer1. Click on Review + Create so that the Azure resource manager can validate the Azure template for container deployment.

As shown below, the validation is successful from a customized template.

Validation status

Click on Create. It starts deployment based on template inputs.

Deployment in progress

Once the deployment completes, you can view your resources. As shown below, we have the container name as [azuresqlcontainer1], the name defined earlier.

Container status

Now, you can connect to SQL Server using the public IP address or DNS name for writing your DB queries.

Verify SQL connection

Deploy ARM templates using Azure CLI

We learned to deploy the Azure Container Instance with SQL Server 2019 Linux image with the Azure Resource Manager (ARM) templates. However, we used the Azure portal for deploying resources with the template.

The benefit of templates is that you can deploy them using the Azure command-line interface(CLI). Therefore, once you have defined templates, you can store them in Azure library, Repositories such as GitHub, or local directory. You can modify the parameter’s value in the parameter file or specify values explicitly to override default values. In this article, we will use the template files downloaded earlier and stored in the local directory.

The Azure CLI script uses az deployment group create for ARM resource deployment. In the following script, we specify the path of both parameter and template JSON files using parameters $parameterfile and $templatefile.

Later, the script reference them using the parameters template-file and parameters as shown below.

Before running the script, run az login and authenticate to Azure using your credentials in the Windows PowerShell or terminal bash.

It starts deploying Azure resources after reading the values from both template and parameter files of ARM templates. Wait for few minutes while the script output is Running.

Deploy ARM template using Azure CLI

It returns container JSON metadata once resources are deployed successfully.

JSON metadata

You can log in to the Azure portal for verification of container resources.

Events

You can now quickly deploy resources without navigating the Azure portal, filling configuration values. For example, I modified parameters values and executed them using CLI for another Azure Container Instance.

Fill configuration values

As shown below, we have two Azure Container instance running with SQL Server 2019 Linux image.

SQL Server 2019 Linux image

Conclusion

This article explored the automation process to deploy Azure Container Instances with SQL Server Linux images using ARM templates. The ARM templates can make life easier with quick deployment and minimal manual intervention of the Azure portal for individual service deployment.

Rajendra Gupta
Latest posts by Rajendra Gupta (see all)
168 Views