Редагувати

Поділитися через


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

Prerequisites

  • Azure subscription: If you don't have an Azure subscription, create a free account before you begin.

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_version = ">= 1.0"
    
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = ">= 3.0, < 4.0"
        }
        random = {
          source  = "hashicorp/random"
          version = ">= 3.0"
        }
      }
    }
    
    provider "azurerm" {
      features {
        resource_group {
          prevent_deletion_if_contains_resources = false
        }
      }
    }
    
  3. 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     = "${random_pet.prefix.id}-rg"
      location = var.location
    }
    
    # Create security group
    resource "azurerm_network_security_group" "example" {
      name                = "${random_pet.prefix.id}-nsg"
      location            = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
    }
    
    # Create a virtual network
    resource "azurerm_virtual_network" "example" {
      name                = "${random_pet.prefix.id}-vnet"
      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                 = "${random_pet.prefix.id}-subnet"
      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                          = "${random_pet.prefix.id}-rt"
      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
    
      depends_on = [azurerm_subnet_network_security_group_association.example]
    }
    
    # Create managed instance
    resource "azurerm_mssql_managed_instance" "main" {
      name                         = "${random_pet.prefix.id}-mssql"
      resource_group_name          = azurerm_resource_group.example.name
      location                     = azurerm_resource_group.example.location
      subnet_id                    = azurerm_subnet.example.id
      administrator_login          = "${replace(random_pet.prefix.id, "-", "")}admin"
      administrator_login_password = random_password.password.result
      license_type                 = var.license_type
      sku_name                     = var.sku_name
      vcores                       = var.vcores
      storage_size_in_gb           = var.storage_size_in_gb
    
      depends_on = [azurerm_subnet_route_table_association.example]
    }
    
    resource "random_password" "password" {
      length      = 20
      min_lower   = 1
      min_upper   = 1
      min_numeric = 1
      min_special = 1
      special     = true
    }
    
    resource "random_pet" "prefix" {
      prefix = var.prefix
      length = 1
    }
    
  4. Create a file named variables.tf and insert the following code:

    variable "prefix" {
      type        = string
      default     = "mi"
      description = "Prefix of the resource name"
    }
    
    variable "location" {
      type        = string
      description = "Enter the location where you want to deploy the resources"
      default     = "eastus"
    }
    
    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 provider required to manage your Azure resources.

terraform init -upgrade

Key points:

  • The -upgrade parameter upgrades the necessary provider plugins to the newest version that complies with the configuration's version constraints.

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.

Apply a Terraform execution plan

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

terraform apply main.tfplan

Key points:

  • The example terraform apply command 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.
  2. 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