GCP Cloud SQL 未能删除实例,因为 `deletion_protection` 设置为 true [英] GCP Cloud SQL failed to delete instance because `deletion_protection` is set to true
问题描述
我有一个用于配置 Cloud SQL 实例的 tf 脚本,还有几个数据库和一个管理员用户.我已重命名该实例,因此创建了一个新实例,但 terraform 在删除旧实例时遇到了问题.
I have a tf script for provisioning a Cloud SQL instance, along with a couple of dbs and an admin user. I have renamed the instance, hence a new instance was created but terraform is encountering issues when it comes to deleting the old one.
Error: Error, failed to delete instance because deletion_protection is set to true. Set it to false to proceed with instance deletion
我尝试将 deletion_protection
设置为 false
,但我一直收到同样的错误.有没有办法检查哪些资源需要将 deletion_protection
设置为 false 才能被删除?我只是将它添加到 google_sql_database_instance
资源中.
I have tried setting the deletion_protection
to false
but I keep getting the same error. Is there a way to check which resources need to have the deletion_protection
set to false in order to be deleted?
I have only added it to the google_sql_database_instance
resource.
我的 tf 脚本:
// Provision the Cloud SQL Instance
resource "google_sql_database_instance" "instance-master" {
name = "instance-db-${random_id.random_suffix_id.hex}"
region = var.region
database_version = "POSTGRES_12"
project = var.project_id
settings {
availability_type = "REGIONAL"
tier = "db-f1-micro"
activation_policy = "ALWAYS"
disk_type = "PD_SSD"
ip_configuration {
ipv4_enabled = var.is_public ? true : false
private_network = var.network_self_link
require_ssl = true
dynamic "authorized_networks" {
for_each = toset(var.is_public ? [1] : [])
content {
name = "Public Internet"
value = "0.0.0.0/0"
}
}
}
backup_configuration {
enabled = true
}
maintenance_window {
day = 2
hour = 4
update_track = "stable"
}
dynamic "database_flags" {
iterator = flag
for_each = var.database_flags
content {
name = flag.key
value = flag.value
}
}
user_labels = var.default_labels
}
deletion_protection = false
depends_on = [google_service_networking_connection.cloudsql-peering-connection, google_project_service.enable-sqladmin-api]
}
// Provision the databases
resource "google_sql_database" "db" {
name = "orders-placement"
instance = google_sql_database_instance.instance-master.name
project = var.project_id
}
// Provision a super user
resource "google_sql_user" "admin-user" {
name = "admin-user"
instance = google_sql_database_instance.instance-master.name
password = random_password.user-password.result
project = var.project_id
}
// Get latest CA certificate
locals {
furthest_expiration_time = reverse(sort([for k, v in google_sql_database_instance.instance-master.server_ca_cert : v.expiration_time]))[0]
latest_ca_cert = [for v in google_sql_database_instance.instance-master.server_ca_cert : v.cert if v.expiration_time == local.furthest_expiration_time]
}
// Get SSL certificate
resource "google_sql_ssl_cert" "client_cert" {
common_name = "instance-master-client"
instance = google_sql_database_instance.instance-master.name
}
推荐答案
好像你的代码要重新创建这个 sql 实例.但是您当前的 tfstate 文件包含 deletion_protection
参数的 true
值的实例代码.在这种情况下,您首先需要在 tfstate 文件中手动将此参数的值更改为 false
或在运行 terraform apply 的代码中添加
deletion_protection = true
之后的命令(注意:您的代码不应该重新创建实例).在这些操作之后,你可以对你的 SQL 实例做任何事情
Seems like your code going to recreate this sql-instance. But your current tfstate file contains an instance-code with true
value for deletion_protection
parameter. In this case, you need first of all change value of this parameter to false
manually in tfstate file or by adding deletion_protection = true
in the code with running terraform apply
command after that (beware: your code shouldn't do a recreation of the instance). And after this manipulations, you can do anything with your SQL instance
这篇关于GCP Cloud SQL 未能删除实例,因为 `deletion_protection` 设置为 true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!