更新 EBS 卷的大小而不替换旧卷并重新启动 EC2 实例

问题描述

我正在创建 EC2 实例和多个不同大小的 EBS 卷。下面给出了创建相同的 terraform 脚本。

resource "aws_ebs_volume" "ebs_volume" {
  for_each = {
    0 = var.influxdb_data_volume_size
    1 = var.influxdb_wal_volume_size
    2 = var.telegraf_volume_size
    3 = var.grafana_volume_size
    4 = var.data_volume_size
  }
  availability_zone = var.availability_zone
  size              = each.value
  type              = "gp2"
  tags = {
    name = "${var.tag}-ebs-volume"
    billing = var.tag
  }
}

# section for the EBS volume attachment

resource "aws_volume_attachment" "volume_attachement" {
  count       = var.ebs_volume_count
  volume_id   = aws_ebs_volume.ebs_volume[count.index].id
  device_name = "${element(var.ec2_device_names,count.index)}"
  instance_id = aws_instance.ec2_production.id
}

# section for the aws_key_pair 
resource "aws_key_pair" "key_pair" {
  key_name   = "${var.tag}-key-pair"
  public_key = var.key_pair
  tags = {
    name = "${var.tag}-key-pair"
    billing = var.tag
  }

}
resource "aws_instance" "ec2_production" {
  # count         = "${var.ec2_instance_count}"
  ami           = var.ami
  availability_zone = var.availability_zone
  instance_type =  var.ec2_instance_type
  subnet_id = aws_subnet.subnet.id
  associate_public_ip_address = true
  vpc_security_group_ids = [aws_security_group.security_group_access_internet.id]
  # disable_api_termination = true
  key_name = "${var.tag}-key-pair"
  ebs_block_device {
    device_name = "/dev/sda1"
    volume_type = "gp2"
    volume_size = var.root_volume_size
  }
  tags = {
    name = "${var.tag}-ec2"
    billing = var.tag
  }
}

aws_instance 资源中定义的卷是大小为 40GB 的根卷,我正在 aws_ebs_volume 资源中创建多个不同大小的卷。

现在我试图通过更新 variable.tf 文件将根卷的大小从 40GB 更改为 60GB。

当我使用 terraform plan 命令时,它会创建 6 个资源并销毁 6 个资源,其中包括 EBS 卷和 EC2 实例。

现在,我想增加任何根卷或任何其他重新创建或销毁任何内容的卷的大小。

下面我给出了 terraform plan

输出
-/+ resource "aws_instance" "ec2_production" {
      ~ arn                                  = "arn:aws:ec2:us-west-1:666268854852:instance/i-0ac516fab494df462" -> (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-0ac516fab494df462" -> (kNown after apply)
      ~ instance_initiated_shutdown_behavior = "stop" -> (kNown after apply)
      ~ instance_state                       = "running" -> (kNown after apply)
      ~ ipv6_address_count                   = 0 -> (kNown after apply)
      ~ ipv6_addresses                       = [] -> (kNown after apply)
      - monitoring                           = false -> null
      + outpost_arn                          = (kNown after apply)
      + password_data                        = (kNown after apply)
      + placement_group                      = (kNown after apply)
      ~ primary_network_interface_id         = "eni-0fd71c3a3e41237be" -> (kNown after apply)
      ~ private_dns                          = "ip-10-0-16-27.us-west-1.compute.internal" -> (kNown after apply)
      ~ private_ip                           = "10.0.16.27" -> (kNown after apply)
      + public_dns                           = (kNown after apply)
      ~ public_ip                            = "13.56.209.171" -> (kNown after apply)
      ~ secondary_private_ips                = [] -> (kNown after apply)
      ~ security_groups                      = [] -> (kNown after apply)
        tags                                 = {
            "billing" = "att"
            "name"    = "ec2"
        }
      ~ tenancy                              = "default" -> (kNown after apply)
        # (10 unchanged attributes hidden)

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

      - ebs_block_device { # forces replacement
          - delete_on_termination = false -> null
          - device_name           = "/dev/sdf" -> null
          - encrypted             = false -> null
          - iops                  = 300 -> null
          - tags                  = {
              - "billing" = "att"
              - "name"    = "volume"
            } -> null
          - throughput            = 0 -> null
          - volume_id             = "vol-08569e15ccd8f1aad" -> null
          - volume_size           = 100 -> null
          - volume_type           = "gp2" -> null
        }
      - ebs_block_device { # forces replacement
          - delete_on_termination = false -> null
          - device_name           = "/dev/sdg" -> null
          - encrypted             = false -> null
          - iops                  = 150 -> null
          - tags                  = {
              - "billing" = "att"
              - "name"    = "volume"
            } -> null
          - throughput            = 0 -> null
          - volume_id             = "vol-0e8c7cd7830fa2e02" -> null
          - volume_size           = 50 -> null
          - volume_type           = "gp2" -> null
        }
      - ebs_block_device { # forces replacement
          - delete_on_termination = false -> null
          - device_name           = "/dev/sdh" -> null
          - encrypted             = false -> null
          - iops                  = 100 -> null
          - tags                  = {
              - "billing" = "att"
              - "name"    = "volume"
            } -> null
          - throughput            = 0 -> null
          - volume_id             = "vol-0f989100daa275774" -> null
          - volume_size           = 20 -> null
          - volume_type           = "gp2" -> null
        }
      - ebs_block_device { # forces replacement
          - delete_on_termination = false -> null
          - device_name           = "/dev/sdi" -> null
          - encrypted             = false -> null
          - iops                  = 100 -> null
          - tags                  = {
              - "billing" = "att"
              - "name"    = "volume"
            } -> null
          - throughput            = 0 -> null
          - volume_id             = "vol-03cd5be3fea05aa45" -> null
          - volume_size           = 20 -> null
          - volume_type           = "gp2" -> null
        }
      - ebs_block_device { # forces replacement
          - delete_on_termination = false -> null
          - device_name           = "/dev/sdj" -> null
          - encrypted             = false -> null
          - iops                  = 150 -> null
          - tags                  = {
              - "billing" = "att"
              - "name"    = "volume"
            } -> null
          - throughput            = 0 -> null
          - volume_id             = "vol-023e380eebbf2c91f" -> null
          - volume_size           = 50 -> null
          - volume_type           = "gp2" -> 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           = 60
          + volume_type           = "gp2"
        }

      ~ 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                  = 120 -> (kNown after apply)
          + kms_key_id            = (kNown after apply)
          ~ tags                  = {} -> (kNown after apply)
          ~ throughput            = 0 -> (kNown after apply)
          ~ volume_id             = "vol-054112f2f104ff54e" -> (kNown after apply)
          ~ volume_size           = 40 -> (kNown after apply)
          ~ volume_type           = "gp2" -> (kNown after apply)
        }
    }

  # aws_volume_attachment.volume_attachement[0] must be replaced
-/+ resource "aws_volume_attachment" "volume_attachement" {
      ~ id          = "vai-1464807002" -> (kNown after apply)
      ~ instance_id = "i-0ac516fab494df462" -> (kNown after apply) # forces replacement
        # (2 unchanged attributes hidden)
    }

  # aws_volume_attachment.volume_attachement[1] must be replaced
-/+ resource "aws_volume_attachment" "volume_attachement" {
      ~ id          = "vai-3274211254" -> (kNown after apply)
      ~ instance_id = "i-0ac516fab494df462" -> (kNown after apply) # forces replacement
        # (2 unchanged attributes hidden)
    }

  # aws_volume_attachment.volume_attachement[2] must be replaced
-/+ resource "aws_volume_attachment" "volume_attachement" {
      ~ id          = "vai-1831356480" -> (kNown after apply)
      ~ instance_id = "i-0ac516fab494df462" -> (kNown after apply) # forces replacement
        # (2 unchanged attributes hidden)
    }

  # aws_volume_attachment.volume_attachement[3] must be replaced
-/+ resource "aws_volume_attachment" "volume_attachement" {
      ~ id          = "vai-374757704" -> (kNown after apply)
      ~ instance_id = "i-0ac516fab494df462" -> (kNown after apply) # forces replacement
        # (2 unchanged attributes hidden)
    }

  # aws_volume_attachment.volume_attachement[4] must be replaced
-/+ resource "aws_volume_attachment" "volume_attachement" {
      ~ id          = "vai-3725815030" -> (kNown after apply)
      ~ instance_id = "i-0ac516fab494df462" -> (kNown after apply) # forces replacement
        # (2 unchanged attributes hidden)
    }

Plan: 6 to add,0 to change,6 to destroy.

有什么方法可以在不破坏任何东西的情况下实现更新卷大小吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)