Quickstart: Create instance with Terraform - Azure SQL Managed Instance

Article tested with the following Terraform and Terraform provider versions:

This article shows how to deploy an Azure SQL Managed Instance in a virtual network (vNet) and a subnet associated with a route table and a network security group by using Terraform.

Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.

In this article, you learn how to:

  • Create all supporting services for SQL Managed Instance to run on
  • Deploy SQL Managed Instance

Note

The example code in this article is located in the Azure Terraform GitHub repo.

Prerequisites

Implement the Terraform code

  1. Create a directory in which to test and run the sample Terraform code and make it the current directory.

  2. Create a file named providers.tf and insert the following code:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">=3.0.0"
    }
  }
}

provider "azurerm" {
  features {}

  environment = "china"
}
  1. Create a file named main.tf and insert the following code:
# TODO set the variables below either enter them in plain text after = sign, or change them in variables.tf
#  (var.xyz will take the default value from variables.tf if you don't change it)

# Create resource group
resource "azurerm_resource_group" "example" {
  name     = var.azurerm_resource_group_name
  location = var.location
}

# Create security group
resource "azurerm_network_security_group" "example" {
  name                = var.azurerm_network_security_group_name
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

# Create a virtual network
resource "azurerm_virtual_network" "example" {
  name                = var.azurerm_virtual_network_name
  resource_group_name = azurerm_resource_group.example.name
  address_space       = ["10.0.0.0/24"]
  location            = azurerm_resource_group.example.location
}

# Create a subnet
resource "azurerm_subnet" "example" {
  name                 = var.azurerm_subnet_name
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.0.0/27"]

  delegation {
    name = "managedinstancedelegation"

    service_delegation {
      name = "Microsoft.Sql/managedInstances"
      actions = [
        "Microsoft.Network/virtualNetworks/subnets/join/action",
        "Microsoft.Network/virtualNetworks/subnets/prepareNetworkPolicies/action",
        "Microsoft.Network/virtualNetworks/subnets/unprepareNetworkPolicies/action"
      ]
    }
  }
}

# Associate subnet and the security group
resource "azurerm_subnet_network_security_group_association" "example" {
  subnet_id                 = azurerm_subnet.example.id
  network_security_group_id = azurerm_network_security_group.example.id
}

# Create a route table
resource "azurerm_route_table" "example" {
  name                          = "routetable-mi-terraform"
  location                      = azurerm_resource_group.example.location
  resource_group_name           = azurerm_resource_group.example.name
  disable_bgp_route_propagation = false
}

# Associate subnet and the route table
resource "azurerm_subnet_route_table_association" "example" {
  subnet_id      = azurerm_subnet.example.id
  route_table_id = azurerm_route_table.example.id
}

# Create managed instance
resource "azurerm_mssql_managed_instance" "example" {
  name                         = var.database_name
  resource_group_name          = azurerm_resource_group.example.name
  location                     = azurerm_resource_group.example.location
  subnet_id                    = azurerm_subnet.example.id
  administrator_login          = var.administrator_login
  administrator_login_password = var.administrator_login_password
  license_type                 = var.license_type
  sku_name                     = var.sku_name
  vcores                       = var.vcores
  storage_size_in_gb           = var.storage_size_in_gb
}
  1. Create a file named variables.tf and insert the following code:
variable "azurerm_resource_group_name" {
  type        = string
  description = "Enter the resource group name"
  default     = "terraform-database-resource-group"
}
variable "azurerm_network_security_group_name" {
  type        = string
  description = "Enter the security group name"
  default     = "mi-security-group-terraform"
}
variable "azurerm_virtual_network_name" {
  type        = string
  description = "Enter the virtual network name"
  default     = "vnet-mi-terraform"
}
variable "azurerm_subnet_name" {
  type        = string
  description = "Enter subnet name"
  default     = "subnet-mi-terraform"
}
variable "location" {
  type        = string
  description = "Enter the location where you want to deploy the resources"
  default     = "chinanorth3"
}

variable "administrator_login" {
  type        = string
  description = "Enter Administrator name for the database"
  default     = "VeryStrongAdministrator"
}

variable "administrator_login_password" {
  type        = string
  description = "Enter administrator password for the database"
  default     = "IamAVeryStrongP@ssw0rd123"
  sensitive   = true
}

variable "database_name" {
  type        = string
  description = "Enter database name"
  default     = "sql-mi-terraform"
}

variable "sku_name" {
  type        = string
  description = "Enter SKU"
  default     = "GP_Gen5"
}
variable "license_type" {
  type        = string
  description = "Enter license type"
  default     = "BasePrice"
}
variable "vcores" {
  type        = number
  description = "Enter number of vCores you want to deploy"
  default     = 8
}
variable "storage_size_in_gb" {
  type        = number
  description = "Enter storage size in GB"
  default     = 32
}

Initialize Terraform

Run terraform init to initialize the Terraform deployment. This command downloads the Azure modules required to manage your Azure resources.

terraform init

Create a Terraform execution plan

Run terraform plan to create an execution plan.

terraform plan -out main.tfplan

Key points:

  • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
  • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.
  • To read more about persisting execution plans and security, see the security warning section.

Apply a Terraform execution plan

Run terraform apply to apply the execution plan to your cloud infrastructure.

terraform apply main.tfplan

Key points:

  • The terraform apply command above assumes you previously ran terraform plan -out main.tfplan.
  • If you specified a different filename for the -out parameter, use that same filename in the call to terraform apply.
  • If you didn't use the -out parameter, call terraform apply without any parameters.

Verify the results

To verify the results within the Azure portal, browse to the new resource group. The new instance will be in the new resource group after it has been deployed. To see the deployment progress keep your PowerShell open or navigate to the Azure portal, search for SQL Managed Instance and then filter all instances by status).

Clean up resources

When you no longer need the resources created via Terraform, do the following steps:

  1. Run terraform plan and specify the destroy flag.

    terraform plan -destroy -out main.destroy.tfplan
    

Key points:

  • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
  • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.
  • To read more about persisting execution plans and security, see the security warning section.
  1. Run terraform apply to apply the execution plan.

    terraform apply main.destroy.tfplan
    

Troubleshoot Terraform on Azure

Troubleshoot common problems when using Terraform on Azure

Next steps