gpt4 book ai didi

amazon-web-services - Terraform AWS : Couldn't reuse previously created root_block_device with AWS EC2 instance launched with aws_launch_configuration

转载 作者:行者123 更新时间:2023-12-03 14:37:46 26 4
gpt4 key购买 nike

我已经使用terraform将ELK堆栈部署到了AWS ECS。几周来,所有程序都运行良好,但是2天前,我不得不重新启动实例。
令人遗憾的是,新实例不依赖现有卷来安装根块设备。因此,我的所有Elasticsearch数据都不再对我的Kibana实例可用。
上一卷中的数据仍在此处,当前未使用。
因此,我尝试了很多事情来使该卷挂载在“dev/xvda”上,但没有一个示例:

  • 使用ebs_block_device代替使用
  • 的root_block_device
  • 当实例已经在运行时,交换“dev/xvda”

  • 我正在将aws_autoscaling_group与aws_launch_configuration一起使用。
    resource "aws_launch_configuration" "XXX" {
    name = "XXX"
    image_id = data.aws_ami.latest_ecs.id
    instance_type = var.INSTANCE_TYPE
    security_groups = [var.SECURITY_GROUP_ID]
    associate_public_ip_address = true
    iam_instance_profile = "XXXXXX"

    spot_price = "0.04"
    lifecycle {
    create_before_destroy = true

    }

    user_data = templatefile("${path.module}/ecs_agent_conf_options.tmpl",
    {
    cluster_name = aws_ecs_cluster.XXX.name
    }
    )

    //The volume i want to reuse was created with this configuration. I though it would
    //be enough to reuse the same volume. It doesn't.
    root_block_device {
    delete_on_termination = false
    volume_size = 50
    volume_type = "gp2"
    }
    }

    resource "aws_autoscaling_group" "YYY" {
    name = "YYY"
    min_size = var.MIN_INSTANCES
    max_size = var.MAX_INSTANCES
    desired_capacity = var.DESIRED_CAPACITY
    health_check_type = "EC2"
    availability_zones = ["eu-west-3b"]
    launch_configuration = aws_launch_configuration.XXX.name

    vpc_zone_identifier = [
    var.SUBNET_1_ID,
    var.SUBNET_2_ID]

    }

    我是否会错过一些显而易见的事情?

    最佳答案

    遗憾的是,您无法将卷作为根卷附加到实例。
    您要做的是根据您的音量创建一个自定义AMI 。这涉及创建卷的快照,然后构造AMI:

  • Creating a Linux AMI from a snapshot

  • 在terraform中,有专门用于此目的的 aws_ami
    以下terraform脚本以 的三个步骤举例说明了该过程:
  • 创建给定卷的快照
  • 从快照
  • 创建 AMI
  • 从AMI
  • 创建 实例
    provider "aws" {
    # your data
    }


    resource "aws_ebs_snapshot" "snapshot" {
    volume_id = "vol-0ff4363a40eb3357c" # <-- your EBS volume ID
    }


    resource "aws_ami" "my" {
    name = "my-custom-ami"

    virtualization_type = "hvm"
    root_device_name = "/dev/xvda"

    ebs_block_device {
    device_name = "/dev/xvda"
    snapshot_id = aws_ebs_snapshot.snapshot.id
    volume_type = "gp2"
    }
    }

    resource "aws_instance" "web" {

    ami = aws_ami.my.id

    instance_type = "t2.micro"
    # key_name = "<your-key-name>"

    tags = {
    Name = "InstanceFromCustomAMI"
    }
    }

    关于amazon-web-services - Terraform AWS : Couldn't reuse previously created root_block_device with AWS EC2 instance launched with aws_launch_configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63205968/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com