Amazon web services Terraform destroy无法清理spot fleet ec2实例

Amazon web services Terraform destroy无法清理spot fleet ec2实例,amazon-web-services,amazon-ec2,terraform,Amazon Web Services,Amazon Ec2,Terraform,当同一个tf文件在不同的工作区运行时,Terraform destroy不会销毁它创建的spot实例 我要求我的CI基础设施使用spot EC2实例创建和销毁ECS群集,以构建和测试我的代码。 为此,我使用terraform创建aws\U spot\U fleet\U请求,并提供一组启动配置。 因为我将在repo中有多个分支,所以我将使用uniq工作区名称为每个分支运行这个terraform。 当两个分支应用我的terraform时,它会正确地基于工作区创建唯一的实例。 然而,当其中一个到达运行

当同一个tf文件在不同的工作区运行时,Terraform destroy不会销毁它创建的spot实例

我要求我的CI基础设施使用spot EC2实例创建和销毁ECS群集,以构建和测试我的代码。 为此,我使用terraform创建aws\U spot\U fleet\U请求,并提供一组启动配置。 因为我将在repo中有多个分支,所以我将使用uniq工作区名称为每个分支运行这个terraform。 当两个分支应用我的terraform时,它会正确地基于工作区创建唯一的实例。 然而,当其中一个到达运行terraform destroy的阶段时,它只是等待销毁,并以下面的错误结束

ws_spot_fleet_request.arm_ct_spot_资源:仍在销毁。。。(ID:,经过3m30s) aws\U spot\U fleet\U请求。arm\U ct\U spot\U资源:仍在销毁。。。(ID:,经过3m40秒) aws\U spot\U fleet\U请求。arm\U ct\U spot\U资源:仍在销毁。。。(ID:,经过3M50秒) aws\U spot\U fleet\U请求。arm\U ct\U spot\U资源:仍在销毁。。。(ID:,已过4毫秒) aws\U spot\U fleet\U请求。arm\U ct\U spot\U资源:仍在销毁。。。(ID:,经过4m10s) aws\U spot\U fleet\U请求。arm\U ct\U spot\U资源:仍在销毁。。。(ID:,经过4M20秒) aws\U spot\U fleet\U请求。arm\U ct\U spot\U资源:仍在销毁。。。(ID:,经过4m30s) aws\U spot\U fleet\U请求。arm\U ct\U spot\U资源:仍在销毁。。。(ID:,经过4M40秒) aws\U spot\U fleet\U请求。arm\U ct\U spot\U资源:仍在销毁。。。(ID:,经过4M50秒) aws\U spot\U fleet\U请求。arm\U ct\U spot\U资源:仍在销毁。。。(ID:,已过5毫秒) 释放状态锁。这可能需要一些时间

错误:应用计划时出错:

发生1个错误:

  • aws_spot_fleet_request.arm_ct_spot_resource(销毁):发生1个错误:

  • aws_spot_fleet_request.arm_ct_spot_资源:删除spot请求时出错():fleet仍有(1)个正在运行的实例

Terraform在遇到错误时不会自动回滚。 相反,您的地形状态文件已使用 成功完成的所有资源。请纠正错误 并再次应用以增量方式更改您的基础结构

resource "aws_spot_fleet_request" "arm_ct_spot_resource" {
  iam_fleet_role                      = "${aws_iam_role.fleet.arn}"
  target_capacity                     = "${var.instance_count}"
  terminate_instances_with_expiration = true
  allocation_strategy                 = "lowestPrice"
  wait_for_fulfillment                = true

  launch_specification {
    instance_type               = "t3.2xlarge"
    ami                         = "${data.aws_ami.ecs_agent_image.id}"
    vpc_security_group_ids      = ["${aws_security_group.security_group_sg.id}"]
    subnet_id                   = "${element(data.terraform_remote_state.environment_state.vpc_service_subnet_ids_2, 0)}"
    iam_instance_profile        = "${aws_iam_instance_profile.arm_iam_profile.name}"
    associate_public_ip_address = true
    key_name                    = "${var.key_name}"
    weighted_capacity           = 1

    # Tags defined in locals only.
    tags = "${merge(
      local.common_tags,
      map(
        "Name", "aws_instance for ${var.environment_id}"
      )
    )}"

    root_block_device {
      volume_size = "${var.disk_size}"
    }

    user_data = <<EOF
#!/bin/bash
echo ECS_CLUSTER="${aws_ecs_cluster.arm_cluster.name}" >> /etc/ecs/ecs.config
EOF
  }

  launch_specification {
    instance_type               = "c5.9xlarge"
    ami                         = "${data.aws_ami.ecs_agent_image.id}"
    vpc_security_group_ids      = ["${aws_security_group.security_group_sg.id}"]
    subnet_id                   = "${element(data.terraform_remote_state.environment_state.vpc_service_subnet_ids_2, 0)}"
    iam_instance_profile        = "${aws_iam_instance_profile.arm_iam_profile.name}"
    associate_public_ip_address = true
    key_name                    = "${var.key_name}"
    weighted_capacity           = 4

    # Tags defined in locals only.
    tags = "${merge(
      local.common_tags,
      map(
        "Name", "aws_instance for ${var.environment_id}"
      )
    )}"

    root_block_device {
      volume_size = "${var.disk_size}"
    }

    user_data = <<EOF
#!/bin/bash
echo ECS_CLUSTER="${aws_ecs_cluster.arm_cluster.name}" >> /etc/ecs/ecs.config
EOF
  }
}
资源“aws现场车队请求”“arm现场资源”{
iam_fleet_role=“${aws_iam_role.fleet.arn}”
target_capacity=“${var.instance_count}”
终止\u实例,且\u过期=true
分配策略=“最低价格”
等待实现=真
启动规范{
实例类型=“t3.2xlarge”
ami=“${data.aws\u ami.ecs\u agent\u image.id}”
vpc_security_group_id=[“${aws_security_group.security_group_sg.id}”]
subnet_id=“${element(data.terraform_remote_state.environment_state.vpc_service_subnet_ids_2,0)}”
iam_实例_profile=“${aws_iam_实例_profile.arm_iam_profile.name}”
关联\u公共\u ip\u地址=真
key\u name=“${var.key\u name}”
加权容量=1
#仅在局部变量中定义的标记。
tags=“${merge(
local.common_标签,
地图(
名称“${var.environment\u id}的aws\u实例”
)
)}"
根块设备{
卷大小=“${var.disk\u size}”
}
用户数据=/etc/ecs/ecs.config
EOF
}
启动规范{
实例_type=“c5.9xlarge”
ami=“${data.aws\u ami.ecs\u agent\u image.id}”
vpc_security_group_id=[“${aws_security_group.security_group_sg.id}”]
subnet_id=“${element(data.terraform_remote_state.environment_state.vpc_service_subnet_ids_2,0)}”
iam_实例_profile=“${aws_iam_实例_profile.arm_iam_profile.name}”
关联\u公共\u ip\u地址=真
key\u name=“${var.key\u name}”
加权容量=4
#仅在局部变量中定义的标记。
tags=“${merge(
local.common_标签,
地图(
名称“${var.environment\u id}的aws\u实例”
)
)}"
根块设备{
卷大小=“${var.disk\u size}”
}
用户数据=/etc/ecs/ecs.config
EOF
}
}

听起来您正在将多个地形资源部署到同一个共享组件。如果您销毁了一个地形资源,并且仍然部署了使用该组件的其他资源,那么您将无法按预期销毁。这是使用共享安全组的EC2资源的典型情况,如果您尝试销毁仍然连接到EC2的安全组,则销毁将失败