首页 > 解决方案 > 在销毁时撤销客户端 vpn 入口

问题描述

我正在尝试撤销 Terrafrom 中“销毁”的 vpn 客户端入口规则。terraform 0.12 一切正常 不幸的是,升级到 0.14 版后,相同的方法不再有效。这是我所拥有的:

resource "null_resource" "client_vpn_ingress" {
  provisioner "local-exec" {
    when = create
    command = "aws ec2 authorize-client-vpn-ingress --client-vpn-endpoint-id ${aws_ec2_client_vpn_endpoint.vpn_endpoint.id} --target-network-cidr ${var.vpc_cidr_block} --authorize-all-groups --region ${var.aws_region} --profile ${var.profile}"
}
  provisioner "local-exec" {
    when = destroy
    command = "aws ec2 revoke-client-vpn-ingress --client-vpn-endpoint-id ${aws_ec2_client_vpn_endpoint.vpn_endpoint.id} --target-network-cidr ${var.vpc_cidr_block} --revoke-all-groups --region ${var.aws_region} --profile ${var.profile}"
}
}

这是错误消息:

错误:来自销毁配置器的无效引用

在 vpn_client_endpoint.tf 第 84 行,在资源“null_resource”“client_vpn_ingress”中:84:command = “aws ec2 revoke-client-vpn-ingress --client-vpn-endpoint-id ${aws_ec2_client_vpn_endpoint.vpn_endpoint.id} --target -network-cidr ${var.vpc_cidr_block} --revoke-all-groups --region ${var.aws_region} --profile ${var.profile}"

销毁时供应商及其连接配置只能通过“self”、“count.index”或“each.key”引用相关资源的属性。

在销毁阶段对其他资源的引用可能会导致依赖循环并与 create_before_destroy 交互不良。

不幸的是,我不再能够使用 Terraform 0.12

有谁知道如何在版本 >= 0.14 的 'terraform destroy' 上撤销它?

标签: amazon-web-servicesterraform

解决方案


从 Terraform AWS 提供商的 2.70.0 版本开始(请参阅此 GitHub 评论),您现在可以执行以下操作:

resource "aws_ec2_client_vpn_authorization_rule" "vpn_auth_rule" {
  depends_on             = [
    aws_ec2_client_vpn_endpoint.vpn
  ]
  client_vpn_endpoint_id = aws_ec2_client_vpn_endpoint.vpn.id
  target_network_cidr    = "0.0.0.0/0"
  authorize_all_groups   = true
}

这样,入口规则将被 Terraform 作为状态中的一流资源处理,您不必担心是否/何时执行命令。


推荐阅读