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

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

Terraform not recognizing import block when creating SQL Server database

问题

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

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

  1. resource "azurerm_mssql_database" "database" {
  2. name = var.database_name
  3. server_id = azurerm_mssql_server.sql_server.id
  4. collation = "SQL_Latin1_General_CP1_CI_AS"
  5. license_type = "LicenseIncluded"
  6. max_size_gb = 4
  7. sku_name = "Basic"
  8. import {
  9. storage_uri = var.bacpac_url
  10. storage_key = var.access_key
  11. storage_key_type = "StorageAccessKey"
  12. administrator_login = var.sql_user
  13. administrator_login_password = var.sql_password
  14. authentication_type = "Sql"
  15. }
  16. }

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

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:

  1. resource "azurerm_mssql_database" "database" {
  2. name = var.database_name
  3. server_id = azurerm_mssql_server.sql_server.id
  4. collation = "SQL_Latin1_General_CP1_CI_AS"
  5. license_type = "LicenseIncluded"
  6. max_size_gb = 4
  7. sku_name = "Basic"
  8. import {
  9. storage_uri = var.bacpac_url
  10. storage_key = var.access_key
  11. storage_key_type = "StorageAccessKey"
  12. administrator_login = var.sql_user
  13. administrator_login_password = var.sql_password
  14. authentication_type = "Sql"
  15. }

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提供程序)。

  1. provider "azurerm" {
  2. features {}
  3. }
  4. resource "azurerm_resource_group" "example" {
  5. name = "example-resources"
  6. location = "West Europe"
  7. }
  8. resource "azurerm_mssql_server" "example" {
  9. name = "example-sqlserver"
  10. resource_group_name = azurerm_resource_group.example.name
  11. location = azurerm_resource_group.example.location
  12. version = "12.0"
  13. administrator_login = "4dm1n157r470r"
  14. administrator_login_password = "4-v3ry-53cr37-p455w0rd"
  15. }
  16. resource "azurerm_mssql_database" "database" {
  17. name = "example-database-qwerty"
  18. server_id = azurerm_mssql_server.example.id
  19. collation = "SQL_Latin1_General_CP1_CI_AS"
  20. license_type = "LicenseIncluded"
  21. max_size_gb = 4
  22. sku_name = "Basic"
  23. import {
  24. storage_uri = var.bacpac_url
  25. storage_key = var.access_key
  26. storage_key_type = "StorageAccessKey"
  27. administrator_login = var.sql_user
  28. administrator_login_password = var.sql_password
  29. authentication_type = "Sql"
  30. }
  31. }
  32. variable "bacpac_url" {
  33. type = string
  34. }
  35. variable "access_key" {
  36. type = string
  37. }
  38. variable "sql_user" {
  39. type = string
  40. }
  41. variable "sql_password" {
  42. type = string
  43. }

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).

  1. provider "azurerm" {
  2. features {}
  3. }
  4. resource "azurerm_resource_group" "example" {
  5. name = "example-resources"
  6. location = "West Europe"
  7. }
  8. resource "azurerm_mssql_server" "example" {
  9. name = "example-sqlserver"
  10. resource_group_name = azurerm_resource_group.example.name
  11. location = azurerm_resource_group.example.location
  12. version = "12.0"
  13. administrator_login = "4dm1n157r470r"
  14. administrator_login_password = "4-v3ry-53cr37-p455w0rd"
  15. }
  16. resource "azurerm_mssql_database" "database" {
  17. name = "example-database-qwerty"
  18. server_id = azurerm_mssql_server.example.id
  19. collation = "SQL_Latin1_General_CP1_CI_AS"
  20. license_type = "LicenseIncluded"
  21. max_size_gb = 4
  22. sku_name = "Basic"
  23. import {
  24. storage_uri = var.bacpac_url
  25. storage_key = var.access_key
  26. storage_key_type = "StorageAccessKey"
  27. administrator_login = var.sql_user
  28. administrator_login_password = var.sql_password
  29. authentication_type = "Sql"
  30. }
  31. }
  32. variable "bacpac_url" {
  33. type = string
  34. }
  35. variable "access_key" {
  36. type = string
  37. }
  38. variable "sql_user" {
  39. type = string
  40. }
  41. variable "sql_password" {
  42. type = string
  43. }

答案2

得分: 1

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

  1. terraform {
  2. required_providers {
  3. azurerm = {
  4. source = "hashicorp/azurerm"
  5. version = "~> 3.27.0"
  6. }
  7. }
  8. }
英文:

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:

  1. terraform {
  2. required_providers {
  3. azurerm = {
  4. source = "hashicorp/azurerm"
  5. version = "~> 3.27.0"
  6. }
  7. }
  8. }

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:

确定