【问题标题】:Ec2 instance detroyed and created on every apply在每次应用时销毁和创建 Ec2 实例
【发布时间】:2021-06-26 08:11:24
【问题描述】:

我正在使用 Terraform 在 AWS 中管理我的基础设施,我已经使用 terraform 启动了一个堡垒实例。我的问题是,每次我执行 terraform plan 时,terraform 都会告诉我它将被销毁并重新创建该堡垒实例,然后 terraform apply 会执行此操作。

这是我的代码。

resource "aws_instance" "bastion" {
  ami = var.ami_id
  instance_type = "t2.micro"
  key_name = var.key_name
  monitoring = false
  vpc_security_group_ids = [aws_security_group.`bastion_sg`.id]
  subnet_id = var.subnet_id_private
  iam_instance_profile = aws_iam_instance_profile.instance_profile.name
  user_data = data.template_file.script.rendered

   tags = merge(
    {
      "Name" = local.name_prefix
    },
    var.default_tags,
  )

  ebs_block_device {
    device_name = "/dev/sda1"
    volume_size = 8
    volume_type = "gp2"
    delete_on_termination = true

  }
  
}

这是 terrafrom 计划输出

  # module.bastion.aws_instance.bastion must be replaced
-/+ resource "aws_instance" "bastion" {
      ~ arn                          = "arn:aws:ec2:xx-xxxx-x:xxxxx:instance/i-xxxxxxxxxxxxxx" -> (known after apply)
      ~ associate_public_ip_address  = false -> (known after apply)
      ~ availability_zone            = "xx-xxxx-xx" -> (known after apply)
      ~ cpu_core_count               = 1 -> (known after apply)
      ~ cpu_threads_per_core         = 1 -> (known after apply)
      - disable_api_termination      = false -> null
      - ebs_optimized                = false -> null
      - hibernation                  = false -> null
      + host_id                      = (known after apply)
      ~ id                           = "i-xxxxxxxxxxxxxx" -> (known after apply)
      ~ instance_state               = "running" -> (known after apply)
      ~ ipv6_address_count           = 0 -> (known after apply)
      ~ ipv6_addresses               = [] -> (known after apply)
      + outpost_arn                  = (known after apply)
      + password_data                = (known after apply)
      + placement_group              = (known after apply)
      ~ primary_network_interface_id = "eni-xxxxxxxxxxxxxx" -> (known after apply)
      ~ private_dns                  = "ip-xx-xxx-xxx-xxxxx.xx-xxxx-x.compute.internal" -> (known after apply)
      ~ private_ip                   = "xx.xxx.x.xx" -> (known after apply)
      + public_dns                   = (known after apply)
      + public_ip                    = (known after apply)
      ~ secondary_private_ips        = [] -> (known after apply)
      ~ security_groups              = [] -> (known after apply)
      ~ tenancy                      = "default" -> (known after apply)
        # (10 unchanged attributes hidden)

      - credit_specification {
          - cpu_credits = "standard" -> null
        }

      + ebs_block_device { # forces replacement
          + delete_on_termination = true
          + device_name           = "/dev/sda1"
          + encrypted             = (known after apply)
          + iops                  = (known after apply)
          + kms_key_id            = (known after apply)
          + snapshot_id           = (known after apply)
          + throughput            = (known after apply)
          + volume_id             = (known after apply)
          + volume_size           = 8
          + volume_type           = "gp2"
        }
      - ebs_block_device { # forces replacement
          - delete_on_termination = true -> null
          - device_name           = "/dev/sda1" -> null
          - encrypted             = false -> null
          - iops                  = 100 -> null
          - snapshot_id           = "snap-xxxxxxxxxxxxxx" -> null
          - tags                  = {} -> null
          - throughput            = 0 -> null
          - volume_id             = "vol-xxxxxxxxxxxxxx" -> null
          - volume_size           = 8 -> null
          - volume_type           = "gp2" -> null
        }

      ~ enclave_options {
          ~ enabled = false -> (known after apply)
        }

      + ephemeral_block_device {
          + device_name  = (known after apply)
          + no_device    = (known after apply)
          + virtual_name = (known after apply)
        }

      ~ metadata_options {
          ~ http_endpoint               = "enabled" -> (known after apply)
          ~ http_put_response_hop_limit = 1 -> (known after apply)
          ~ http_tokens                 = "optional" -> (known after apply)
        }

      + network_interface {
          + delete_on_termination = (known after apply)
          + device_index          = (known after apply)
          + network_interface_id  = (known after apply)
        }

      ~ root_block_device {
          ~ delete_on_termination = true -> (known after apply)
          ~ device_name           = "/dev/sda1" -> (known after apply)
          ~ encrypted             = false -> (known after apply)
          ~ iops                  = 100 -> (known after apply)
          + kms_key_id            = (known after apply)
          ~ tags                  = {} -> (known after apply)
          ~ throughput            = 0 -> (known after apply)
          ~ volume_id             = "vol-xxxxxxxxxxxxxx" -> (known after apply)
          ~ volume_size           = 8 -> (known after apply)
          ~ volume_type           = "gp2" -> (known after apply)
        }
    }

【问题讨论】:

  • 您能否分享terraform plan 输出,因为它会说明导致替换的原因。

标签: amazon-web-services amazon-ec2 terraform terraform-provider-aws


【解决方案1】:

所以是 EBS 卷导致了替换。

最可能的原因是它无法附加到/dev/sda1,因此当 Terraform 查看计划时,它发现其磁盘不在 sda1 上并决定需要更换实例。

您需要在实例运行时确认这一点,以查看您的卷已附加到的位置(这可以在实例上或控制台中)。

很可能根卷已附加到 sda1,将您的卷推到别处。

如果是这种情况,那么您需要更改此卷的挂载点,或者如果您打算将其作为根卷,则使用 root_block_device

【讨论】:

  • 谢谢@apr_1985
猜你喜欢
  • 1970-01-01
  • 2021-06-16
  • 2013-04-27
  • 2019-06-16
  • 2021-11-28
  • 2019-02-19
  • 2014-02-02
  • 1970-01-01
  • 2018-01-03
相关资源
最近更新 更多