Terraform 在条件为假后不破坏资源
Terraform not destroying resource after condition is false
我正在使用 terraform 创建 vpc 端点。我的条件是,如果我们在 .tfvars 文件中提供子网 ID,它会创建端点,否则不会。
创建端点时代码正在运行。
问题:
创建端点后,如果我从 .tfvars 文件中删除子网 ID,它只会从端点中删除子网。
期望:
它应该破坏端点。
下面是我的代码:
resource.tf
resource "aws_vpc_endpoint" "CloudFormation" {
count = var.cf_subnet_ids != [] ? 1 : 0
vpc_id = var.vpc_id
service_name = data.aws_vpc_endpoint_service.cloudformation.service_name
vpc_endpoint_type = "Interface"
security_group_ids = var.security_group_ids
subnet_ids = var.cf_subnet_ids
private_dns_enabled = true
}
resource "aws_vpc_endpoint" "Monitoring" {
count = var.mntr_subnet_ids != [] ? 1 : 0
vpc_id = var.vpc_id
service_name = data.aws_vpc_endpoint_service.monitoring.service_name
vpc_endpoint_type = "Interface"
security_group_ids = var.security_group_ids
subnet_ids = var.mntr_subnet_ids
private_dns_enabled = true
}
module.tf
module "VPC1" {
source = "./endpoints"
count = var.vpc_endpoints[0].vpc_id != "" ? 1 : 0
vpc_id = var.vpc_endpoints[0].vpc_id
cf_subnet_ids = var.vpc_endpoints[0].endpointCloudFormationPerVPC
mntr_subnet_ids = var.vpc_endpoints[0].endpointMonitoringPerVPC
}
module "VPC2" {
source = "./endpoints"
count = var.vpc_endpoints[1].vpc_id != "" ? 1 : 0
vpc_id = var.vpc_endpoints[1].vpc_id
cf_subnet_ids = var.vpc_endpoints[1].endpointCloudFormationPerVPC
mntr_subnet_ids = var.vpc_endpoints[1].endpointMonitoringPerVPC
}
env.tfvars
vpc_endpoints = [
{
vpc_id = "vpc-0a7c8cb62ae12ecb0"
vpc_cidr = ["10.150.2.0/23"]
endpointCloudFormationPerVPC = ["subnet-0367288ea9b4a0656", "subnet-0779a62471a3ee5b6"]
endpointMonitoringPerVPC = []
},
{
vpc_id = "vpc-0b085d19c3c35617f"
vpc_cidr = ["10.150.0.0/23"]
endpointCloudFormationPerVPC = ["subnet-0367288ea9b4a0656", "subnet-0779a62471a3ee5b6"]
endpointMonitoringPerVPC = ["subnet-0fd8da6ec6672c759"]
}
]
请帮忙。
它创建了 3 个端点。 2 个 CF 和 1 个带有上述脚本的监控。如果我将 .tfvars 修改为以下,它应该删除监控端点。相反,它会从端点中删除子网,因为它会修改端点而不是破坏它。
vpc_endpoints = [
{
vpc_id = "vpc-0a7c8cb62ae12ecb0"
vpc_cidr = ["10.150.2.0/23"]
endpointCloudFormationPerVPC = ["subnet-0367288ea9b4a0656"]
endpointMonitoringPerVPC = []
},
{
vpc_id = "vpc-0b085d19c3c35617f"
vpc_cidr = ["10.150.0.0/23"]
endpointCloudFormationPerVPC = ["subnet-0367288ea9b4a0656"]
endpointMonitoringPerVPC = []
}
目前在您声明的模块参数中,您正在为 cf_subnet_ids
分配以下值:
var.vpc_endpoints[0].endpointCloudFormationPerVPC
鉴于您在问题中发布的 vpc_endpoints
变量,合并 list(object)
后此参数值将是 null
。因此,当我们在您的资源配置中解析此值时,它将是:
resource "aws_vpc_endpoint" "CloudFormation" {
count = null != [] ? 1 : 0
...
}
由于null
不等于空列表构造函数[]
,这将解析为三元1
中的第一个返回值。这意味着该资源仍将以 1
的计数进行管理,并且您的资源不会被删除。
开始修复配置并解决此意外行为的最简单和最佳实践方法是将 <= 0.11 元参数 count
转换为 >= 0.12 元参数 for_each
: documentation.
我正在使用 terraform 创建 vpc 端点。我的条件是,如果我们在 .tfvars 文件中提供子网 ID,它会创建端点,否则不会。 创建端点时代码正在运行。
问题: 创建端点后,如果我从 .tfvars 文件中删除子网 ID,它只会从端点中删除子网。 期望: 它应该破坏端点。
下面是我的代码: resource.tf
resource "aws_vpc_endpoint" "CloudFormation" {
count = var.cf_subnet_ids != [] ? 1 : 0
vpc_id = var.vpc_id
service_name = data.aws_vpc_endpoint_service.cloudformation.service_name
vpc_endpoint_type = "Interface"
security_group_ids = var.security_group_ids
subnet_ids = var.cf_subnet_ids
private_dns_enabled = true
}
resource "aws_vpc_endpoint" "Monitoring" {
count = var.mntr_subnet_ids != [] ? 1 : 0
vpc_id = var.vpc_id
service_name = data.aws_vpc_endpoint_service.monitoring.service_name
vpc_endpoint_type = "Interface"
security_group_ids = var.security_group_ids
subnet_ids = var.mntr_subnet_ids
private_dns_enabled = true
}
module.tf
module "VPC1" {
source = "./endpoints"
count = var.vpc_endpoints[0].vpc_id != "" ? 1 : 0
vpc_id = var.vpc_endpoints[0].vpc_id
cf_subnet_ids = var.vpc_endpoints[0].endpointCloudFormationPerVPC
mntr_subnet_ids = var.vpc_endpoints[0].endpointMonitoringPerVPC
}
module "VPC2" {
source = "./endpoints"
count = var.vpc_endpoints[1].vpc_id != "" ? 1 : 0
vpc_id = var.vpc_endpoints[1].vpc_id
cf_subnet_ids = var.vpc_endpoints[1].endpointCloudFormationPerVPC
mntr_subnet_ids = var.vpc_endpoints[1].endpointMonitoringPerVPC
}
env.tfvars
vpc_endpoints = [
{
vpc_id = "vpc-0a7c8cb62ae12ecb0"
vpc_cidr = ["10.150.2.0/23"]
endpointCloudFormationPerVPC = ["subnet-0367288ea9b4a0656", "subnet-0779a62471a3ee5b6"]
endpointMonitoringPerVPC = []
},
{
vpc_id = "vpc-0b085d19c3c35617f"
vpc_cidr = ["10.150.0.0/23"]
endpointCloudFormationPerVPC = ["subnet-0367288ea9b4a0656", "subnet-0779a62471a3ee5b6"]
endpointMonitoringPerVPC = ["subnet-0fd8da6ec6672c759"]
}
]
请帮忙。
它创建了 3 个端点。 2 个 CF 和 1 个带有上述脚本的监控。如果我将 .tfvars 修改为以下,它应该删除监控端点。相反,它会从端点中删除子网,因为它会修改端点而不是破坏它。
vpc_endpoints = [
{
vpc_id = "vpc-0a7c8cb62ae12ecb0"
vpc_cidr = ["10.150.2.0/23"]
endpointCloudFormationPerVPC = ["subnet-0367288ea9b4a0656"]
endpointMonitoringPerVPC = []
},
{
vpc_id = "vpc-0b085d19c3c35617f"
vpc_cidr = ["10.150.0.0/23"]
endpointCloudFormationPerVPC = ["subnet-0367288ea9b4a0656"]
endpointMonitoringPerVPC = []
}
目前在您声明的模块参数中,您正在为 cf_subnet_ids
分配以下值:
var.vpc_endpoints[0].endpointCloudFormationPerVPC
鉴于您在问题中发布的 vpc_endpoints
变量,合并 list(object)
后此参数值将是 null
。因此,当我们在您的资源配置中解析此值时,它将是:
resource "aws_vpc_endpoint" "CloudFormation" {
count = null != [] ? 1 : 0
...
}
由于null
不等于空列表构造函数[]
,这将解析为三元1
中的第一个返回值。这意味着该资源仍将以 1
的计数进行管理,并且您的资源不会被删除。
开始修复配置并解决此意外行为的最简单和最佳实践方法是将 <= 0.11 元参数 count
转换为 >= 0.12 元参数 for_each
: documentation.