![](/img/trans.png)
[英]How to optionally configure root_block_device in EC2 instance using terraform
[英]Terraform AWS : Couldn't reuse previously created root_block_device with AWS EC2 instance launched with aws_launch_configuration
我已經使用 terraform 將 ELK 堆棧部署到 AWS ECS。 幾周前一切都運行良好,但兩天前我不得不重新啟動實例。
遺憾的是,新實例不依賴現有卷來掛載根塊設備。 所以我的所有 elasticsearch 數據不再可用於我的 Kibana 實例。
數據還在這里,在以前的卷上,目前沒有使用。
因此,我嘗試了很多方法來將此卷附加到“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:
在 terraform 中,有專門用於此目的的aws_ami 。
以下 terraform 腳本通過三個步驟舉例說明了該過程:
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"
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.