Terraform AWS:无法在通过aws_launch_configuration启动的 AWS EC2 实例中重复使用以前创建的root_block_device



我已经使用terraform将ELK堆栈部署到AWS ECS。几周来一切都运行得很好,但两天前我不得不重新启动实例。

遗憾的是,新实例没有依赖于现有卷来装载根块设备。因此,我所有的弹性搜索数据不再可用于Kibana实例。

数据仍在上一个卷上,当前未使用。

所以我尝试了很多方法来把这本书附在";dev/xvda";但没有例如:

  • 使用ebs_block_device而不是root_block_device
  • 交换";dev/xvda";当实例已在运行时

我使用的是带有aws_launch_configuration的aws_autoscaling_group。

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:

  • 从快照创建Linux AMI

在地形中,有专门用于此目的的aws_ami。

以下地形脚本示例了三个步骤中的过程:

  1. 创建给定卷的快照
  2. 从快照创建AMI
  3. 从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"
}
}

相关内容

  • 没有找到相关文章

最新更新