skip to Main Content

I am attempting to create a database using terraform in Azure from a bacpac located in a storage account. According to this documentation it should be doable using the azurerm_mssql_database import block however it throws an error when attempting this.

This is the relevant section of my tf code:

resource "azurerm_mssql_database" "database" {
  name           = var.database_name
  server_id      = azurerm_mssql_server.sql_server.id
  collation      = "SQL_Latin1_General_CP1_CI_AS"
  license_type   = "LicenseIncluded"
  max_size_gb    = 4
  sku_name       = "Basic"
  import {
    storage_uri                  = var.bacpac_url
    storage_key                  = var.access_key
    storage_key_type             = "StorageAccessKey"
    administrator_login          = var.sql_user
    administrator_login_password = var.sql_password
    authentication_type          = "Sql"
  }

However running this returns the following error:

Error: Unsupported block type

on main.tf line 187, in resource "azurerm_mssql_database"
"database":
187: import {
Blocks of type "import" are not expected here.

I think it clearly confusing the import block with an import for bringing existing resources under terraform management. Is there an error in my syntax that is causing this or is there a new approach I should be using? I am using terraform version 1.5.2 and version 3.0.2 of the azurerm provider if that helps.

I have tried using create_mode however that seems to be broken as well

2

Answers


  1. The import block was enabled in version 3.27.0 of the AzureRM provider. You would need to update the provider to a minimum of that version, such as with the semantic versioning below:

    terraform {
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~> 3.27.0"
        }
      }
    }
    
    Login or Signup to reply.
  2. Support for import block for database has been added in azurerm provider version 3.27.0 and above.

    https://github.com/hashicorp/terraform-provider-azurerm/pull/18588
    enter image description here

    https://github.com/hashicorp/terraform-provider-azurerm/releases/tag/v3.27.0

    Below works (assuming using latest or version 3.27.0 of azurerm provider).

    provider "azurerm" {
      features {}
    }
    
    resource "azurerm_resource_group" "example" {
      name     = "example-resources"
      location = "West Europe"
    }
    
    resource "azurerm_mssql_server" "example" {
      name                         = "example-sqlserver"
      resource_group_name          = azurerm_resource_group.example.name
      location                     = azurerm_resource_group.example.location
      version                      = "12.0"
      administrator_login          = "4dm1n157r470r"
      administrator_login_password = "4-v3ry-53cr37-p455w0rd"
    }
    
    
    resource "azurerm_mssql_database" "database" {
      name           = "example-database-qwerty"
      server_id      = azurerm_mssql_server.example.id
      collation      = "SQL_Latin1_General_CP1_CI_AS"
      license_type   = "LicenseIncluded"
      max_size_gb    = 4
      sku_name       = "Basic"
      import {
        storage_uri                  = var.bacpac_url
        storage_key                  = var.access_key
        storage_key_type             = "StorageAccessKey"
        administrator_login          = var.sql_user
        administrator_login_password = var.sql_password
        authentication_type          = "Sql"
      }
    }
    variable "bacpac_url" {
      type = string
    }
    variable "access_key" {
      type = string
    }
    variable "sql_user" {
      type = string
    }
    variable "sql_password" {
      type = string
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search