Terraform在创建SQL Server数据库时无法识别导入块。

huangapple go评论87阅读模式
英文:

Terraform not recognizing import block when creating SQL Server database

问题

我正在尝试使用Terraform在Azure中创建一个数据库,该数据库位于存储账户中的bacpac文件中。根据这份文档,可以使用azurerm_mssql_database的import块来实现,但是在尝试时会出现错误。

这是我tf代码中相关的部分:

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"
  }
}

然而,运行时会返回以下错误:

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.

我认为它明显将import块与将现有资源引入Terraform管理的导入混淆了。是否有语法错误导致此问题,或者是否有新的方法我应该使用?我正在使用Terraform版本1.5.2和azurerm提供程序的版本3.0.2,如果有帮助的话。

我尝试过使用create_mode,但似乎也有问题。

英文:

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

答案1

得分: 2

在azurerm提供程序版本3.27.0及以上中,已添加对数据库导入块的支持。

以下是可行的(假设使用最新版本或3.27.0版本的azurerm提供程序)。

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
}

Terraform在创建SQL Server数据库时无法识别导入块。

英文:

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
Terraform在创建SQL Server数据库时无法识别导入块。

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
}

答案2

得分: 1

import块在AzureRM提供程序的3.27.0版本中被启用。您需要将提供程序更新到至少该版本,例如使用下面的语义版本控制:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.27.0"
    }
  }
}
英文:

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"
}
}
}

huangapple
  • 本文由 发表于 2023年8月9日 03:11:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定