Terraform module to create and manage a set of resources with one of MSSQL flavors:

  • Server with optional elastic pools and databases management
  • Managed Instance
  • Managed Virtual Machine

Depending on the usage, Virtual Network with Subnets delegations and/or VM with SQL support must be provisioned in advance.

NOTE: This module is meant to be used with an already created Resource Group (required only for MSSQL Server or Managed Instance entities).

The module follows Azure naming conventions. Name variables passed to the module are only suffixes used in the full resource names that are defined by Azure standards.

Abbreviations are used for resource names in most cases, based on these Azure recommendations

NOTE: Make sure your passwords meet Azure Password Restrictions

Azure MSSQL

Managed MSSQL Databases in Azure
$600
BUY
Log in to Corewide IaC registry

Once you have a Corewide Solutions Portal account, this one-time action will use your browser session to retrieve credentials:

 shellterraform login solutions.corewide.com
Provision instructions

Initialize mandatory providers:

Copy and paste into your Terraform configuration and insert the variables:

 hclmodule "tf_azure_mssql" {
  source  = "solutions.corewide.com/azure/tf-azure-mssql/azurerm"
  version = "~> 2.0.0"

  # specify module inputs here or try one of the examples below
  ...
}

Initialize the setup:

 shellterraform init
Define update strategy

Corewide DevOps team strictly follows Semantic Versioning Specification to provide our clients with products that have predictable upgrades between versions. We recommend pinning patch versions of our modules using pessimistic constraint operator (~>) to prevent breaking changes during upgrades.

To get new features during the upgrades (without breaking compatibility), use ~> 2.0 and run terraform init -upgrade

For the safest setup, use strict pinning with version = "2.0.0"

v2.0.0 released 6 months, 4 weeks ago
New version approx. every 5 weeks

Create Azure SQL Managed Instance (PaaS):

 hclresource "azurerm_resource_group" "bar" {
  name     = "bar"
  location = "eastus"
}

data "azurerm_subnet" "snet" {
  name                 = "snet-sql"
  virtual_network_name = "vnet"
  resource_group_name  = "bar"
}

module "azure_sql_db" {
  source  = "solutions.corewide.com/azure/tf-azure-mssql/azurerm"
  version = "~> 2.0"

  service_type   = "mi"
  resource_group = azurerm_resource_group.bar
  name_suffix    = "biz"

  managed_instance_parameters = {
    subnet_id = data.azurerm_subnet.snet.id
  }

  tags = {
    Application = "Web"
  }
}

Create Azure SQL DBaaS Instance:

 hclresource "azurerm_resource_group" "bar" {
  name     = "bar"
  location = "eastus"
}

module "azure_sql_db" {
  source  = "solutions.corewide.com/azure/tf-azure-mssql/azurerm"
  version = "~> 2.0"

  service_type   = "srv"
  resource_group = azurerm_resource_group.bar
  server_version = "12.0"
  name_suffix    = "biz"

  databases = [
    {
      name        = "db"
      max_size_gb = 32
    },
  ]

  elastic_pools = [
    {
      name_suffix = "biz"
      max_size_gb = 256
    },
  ]

  tags = {
    Application = "Web"
  }
}

Create an Azure SQL Database in self-managed Azure VM (the VM must be provisioned separately):

 hcldata "azurerm_virtual_machine" "foo" {
  name                = "foo"
  resource_group_name = "bar"
}

module "azure_sql_db" {
  source  = "solutions.corewide.com/azure/tf-azure-mssql/azurerm"
  version = "~> 2.0"

  service_type = "vm"

  vm_parameters = {
    vm_id = data.azurerm_virtual_machine.foo.id
  }

  tags = {
    Application = "Web"
  }
}
Variable Description Type Default Required Sensitive
service_type The type of MSSQL flavor to manage. Possible values are srv for MSSQL Server, mi for MSSQL Managed Instance, and vm for MSSQL Virtual Machine string yes no
administrator_login The Administrator login. Appliable for MSSQL Server or Managed Instance entities string mssqladmin no no
administrator_password Password associated with the administrator_login. Will be generated if not specified. Appliable for MSSQL Server or Managed Instance entities string no yes
databases List of parameters for the MSSQL Databases associated with the managed MSSQL Server list(object) [] no no
databases[*].license_type Specifies the license type applied to this database string BasePrice no no
databases[*].max_size_gb The max size of the database in gigabytes number yes no
databases[*].name The database name string yes no
databases[*].zone_redundant Defines if the database is zone redundant bool false no no
elastic_pools List of parameters for the MSSQL Azure Elastic Pools associated with the managed MSSQL Server list(object) [] no no
elastic_pools[*].license_type Specifies the license type applied to this database string BasePrice no no
elastic_pools[*].max_size_gb The max data size of the elastic pool in gigabytes number yes no
elastic_pools[*].name_suffix The name suffix of the SQL elastic pool to create string yes no
elastic_pools[*].per_db_max_capacity The maximum capacity any one database can consume number 4 no no
elastic_pools[*].per_db_min_capacity The minimum capacity all databases are guaranteed number 1 no no
elastic_pools[*].sku_capacity The scale up/out capacity, representing server's compute units number 4 no no
elastic_pools[*].sku_family The family of hardware string Gen5 no no
elastic_pools[*].sku_name Specifies the SKU Name for this elastic pool string GP_Gen5 no no
elastic_pools[*].sku_tier The tier of the particular SKU string GeneralPurpose no no
managed_instance_parameters A set of parameters for the MSSQL Azure Managed Instance object {} no no
managed_instance_parameters.license_type Type of license the Managed Instance will use string BasePrice no no
managed_instance_parameters.sku_name Specifies the SKU Name for the SQL Managed Instance string GP_Gen5 no no
managed_instance_parameters.storage_size Maximum storage space for the SQL Managed instance. This should be a multiple of 32 (GB) number 32 no no
managed_instance_parameters.subnet_id The subnet resource ID that the SQL Managed Instance will be associated with string no no
managed_instance_parameters.vcores Number of cores that should be assigned to the SQL Managed Instance number 8 no no
minimum_tls_version The Minimum TLS Version (valid values include 1.0, 1.1, 1.2). Appliable for MSSQL Server or Managed Instance entities string 1.2 no no
name_suffix Naming suffix for the MSSQL entity managed by the module. Required only for MSSQL Server or Managed Instance entities string no no
public_access_enabled Indicates whether public access is allowed. Appliable for MSSQL Server or Managed Instance entities bool false no no
resource_group The object with attributes of Resource Group in which resources should be created. Keys name and location are required. Required only for MSSQL Server or Managed Instance entities any no no
server_version The version of MSSQL server string 12.0 no no
tags Tag(s) that must be assigned to resources map(string) {} no no
vm_parameters A set of parameters for the MSSQL Virtual Machine object {} no no
vm_parameters.connectivity_type The connectivity type used for this SQL Server string PRIVATE no no
vm_parameters.license_type The SQL Server license type string PAYG no no
vm_parameters.port The SQL Server port number 1433 no no
vm_parameters.vm_id The ID of the Virtual Machine string no no
Output Description Type Sensitive
databases Contains attributes of MSSQL Databases resource no
elastic_pools Contains attributes of MSSQL Azure Elastic Pools resource no
managed_instance Contains attributes of MSSQL Azure Managed Instance computed yes
server Contains attributes of MSSQL Azure Database Server computed yes
virtual_machine Contains attributes of MSSQL Virtual Machine computed yes
Dependency Version Kind
terraform >= 1.3 CLI
hashicorp/azurerm ~> 4.0 provider
hashicorp/random ~> 3.3 provider

Not sure where to start?
Let's find your perfect match.