简体   繁体   English

Terraform Azure - 将现有的 Azure Z9778840A0100CB30C982Z876A2 移动到数据库池中

[英]Terraform Azure - Move an existing Azure SQL Database into an Elastic Pool

I have an existing Azure SQL Server and 1 database that wasn't created into an Elastic Pool initially.我有一个现有的 Azure SQL 服务器和 1 个最初未创建到弹性池中的数据库。 Terraform has deployed this and kept the state. Terraform 已经部署了这个并保留了 state。

# Define SQL Server 1
resource "azurerm_mssql_server" "go-1" {
  name                          = "sql-sandbox-server01
  resource_group_name           = data.azurerm_resource_group.env-resourcegroup.name
  location                      = data.azurerm_resource_group.env-resourcegroup.location
  version                       = var.azsqlserver1version
  administrator_login           = var.azsqlserver1sauser
  administrator_login_password  = random_password.sql-password.result
  public_network_access_enabled = "true" # set to false with vNet integration
}
# Define SQL Database 1 - non-ElasticPool
resource "azurerm_mssql_database" "go-1" {
  name                = "sqldb-sandbox-01"
  server_id           = azurerm_mssql_server.go-1.id
  sku_name            = "Basic"
}

Since the decision now to use Elastic Pools has been reached (for this single database and others to follow) the database "sqldb-sandbox-01" now already has tables and data in it.由于现在已经决定使用弹性池(对于这个单一数据库和其他数据库),数据库“sqldb-sandbox-01”现在已经包含表和数据。

I've added this to my main.tf file...and it works fine, the elastic pool gets created...我已将此添加到我的 main.tf 文件中……它工作正常,创建了弹性池……

resource "azurerm_sql_elasticpool" "go-1" {
  name                = "sqlep-sandbox-pool01
  resource_group_name = data.azurerm_resource_group.env-resourcegroup.name
  location            = data.azurerm_resource_group.env-resourcegroup.location
  server_name         = azurerm_mssql_server.go-1.name
  edition             = "Basic"
  dtu                 = 50
  db_dtu_min          = 0
  db_dtu_max          = 5
  pool_size           = 5000
}

My question is...How do I move the existing "sqldb-sandbox-01" into the Elastic Pool in Terraform without it destroying the database and recreating it?我的问题是......如何将现有的“sqldb-sandbox-01”移动到 Terraform 的弹性池中,而不破坏数据库并重新创建它?

I attempted this, just adding the single line elastic_pool_id, but as the documentation states it will destroy/create the database again...我尝试了这个,只是添加了单行 elastic_pool_id,但正如文档所述,它将再次破坏/创建数据库......

# Define SQL Database 1 - non-ElasticPool
resource "azurerm_mssql_database" "go-1" {
  name                = var.azsqldb1name
  server_id           = azurerm_mssql_server.go-1.id
  sku_name            = var.azsqldb1sku
  elastic_pool_id     = azurerm_sql_elasticpool.go-1.id
}

I would be grateful to hear from anyone that has been in the same position and managed to find a way.我将很高兴听到任何曾在同一个 position 中并设法找到方法的人的来信。

To move an existing same-server database into an Elastic Pool is easily achieved in the Azure Portal GUI, so I was hoping for something similar here.在 Azure 门户 GUI 中可以轻松实现将现有的同一服务器数据库移动到弹性池中,因此我希望在这里有类似的东西。 I did some searching around but couldn't find anything specific to this straightforward task.我做了一些搜索,但找不到任何特定于这个简单任务的东西。

Thanks in advance提前致谢

For the existing Azure SQL database and Elastic Pool.对于现有的 Azure SQL 数据库和弹性池。 Directly adding the single line elastic_pool_id in the block will force a new resource to be created.在块中直接添加单行elastic_pool_id将强制创建新资源。 Even this display is not obvious in the Azure portal.甚至这个显示在 Azure 传送门中也不明显。

在此处输入图像描述

Instead of doing this, you could use local PowerShell scripts to add the existing database to the new Elastic Pool.您可以使用本地 PowerShell 脚本将现有数据库添加到新的弹性池,而不是这样做。 The local-exec provisioner invokes a local executable after a resource is created. local-exec 供应器在创建资源后调用本地可执行文件。

Here is a working sample on my side.这是我这边的一个工作示例。

resource "null_resource" "add_pool" {
 
  provisioner "local-exec" {

  command = <<-EOT
   Set-AzSqlDatabase `
   -ResourceGroupName "${azurerm_resource_group.example.name}" `
   -ServerName "${azurerm_mssql_server.example.name}" `
   -DatabaseName "${azurerm_mssql_database.test.name}" `
   -ElasticPoolName "${azurerm_sql_elasticpool.go-1.name}"
  EOT
    
  interpreter = ["PowerShell", "-Command"]

 }

}

This actually ended up easier than anticipated, after quite a lot of testing.经过大量测试后,这实际上比预期的要容易。

My original database segment looked like this...我原来的数据库段看起来像这样......

# Define SQL Database 1 - non-ElasticPool
resource "azurerm_mssql_database" "go-1" {
  name                = var.azsqldb1name
  server_id           = azurerm_mssql_server.go-1.id
  sku_name            = "Basic"
}

It made sense to just move the database to an elastic pool manually, rather than try and do it in-code.手动将数据库移动到弹性池而不是尝试在代码中执行它是有意义的。

Once the database had moved to the elastic pool, I noticed the Azure ID behind the database did not change.数据库移到弹性池后,我注意到数据库后面的 Azure ID 没有改变。

I then updated the Terraform with a change to the sku_name, and the addition of the elastic_pool_id...然后我更新了 Terraform,更改了 sku_name,并添加了 elastic_pool_id ...

resource "azurerm_mssql_database" "go-1" {
  name                = var.azsqldb1name
  server_id           = azurerm_mssql_server.go-1.id
  sku_name            = "ElasticPool"
  elastic_pool_id     = azurerm_sql_elasticpool.go-1.id
}

Run the Terraform Plan again, No infrastructure changes detected, it has worked and doesn't want to destroy anything.再次运行 Terraform 计划,未检测到基础设施更改,它已经工作并且不想破坏任何东西。

In summary : Do the move of the stand-alone database to the Elastic Pool manually总结:手动将独立数据库移动到弹性池

Update your Terraform for the database in question with a change to... sku_name ...and the addition of the elastic_pool_id为有问题的数据库更新 Terraform 并更改为... sku_name ...并添加elastic_pool_id

Thanks to all those that assisted me with this question感谢所有帮助我解决这个问题的人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM