Terraform 为实例 EBS 块存储应用巨大的索引值

Terraform applying huge index value for instance EBS block store

我正在使用 Terraform(通过 Terragrunt 调用,如果相关)从 AMI 创建实例并安装现有卷:

resource "aws_instance" "jenkins_master_with_snap" {
    count                   = "${var.master_with_snapshot}"
    ami                     = "${var.jenkins_ami}"
    instance_type           = "${var.jenkins_instance_type}"
    iam_instance_profile    = "${data.terraform_remote_state.global.jenkins_profile_name}"
    subnet_id               = "${data.aws_subnet.jenkins_subnet_with_snap.id}"
    key_name                = "${var.key_name}"
    vpc_security_group_ids  = [
        "${aws_security_group.jenkins_master_target_sg.id}",
        "${data.terraform_remote_state.cicd.cicd_sg_ipa}"
    ]

    ebs_block_device {
        snapshot_id = "${var.master_snapshot_id}"
        device_name = "${var.jenkins_volume_device}"
        volume_type = "gp2"
    }
}

值得注意的是,用于创建此资源的 AMI 已从构建过程中映射到它的快照,因此此资源基本上只是将其替换为不同的快照。我不确定这是否就是我遇到问题的原因。

我正在使用生成的资源属性来填充一个 Python 模板,该模板将被压缩并作为 lambda 函数上传。 Python 脚本需要来自此实例的 EBS 块设备的 volume-id

data "template_file" "ebs_backup_lambda_with_snapshot_template" {
    count       = "${var.master_with_snapshot}"
    template    = "${file("${path.module}/jenkins_lambda_ebs_backup.py.tpl")}"

    vars {
        volume_id = "${aws_instance.jenkins_master_with_snap.ebs_block_device.???.volume_id}"
    }
}

关于实际问题:我不知道如何正确引用上面 template_file 资源的 vars 部分中的卷 ID。这是结果状态:

ebs_block_device.#                                = 1
ebs_block_device.1440725774.delete_on_termination = true
ebs_block_device.1440725774.device_name           = /dev/xvdf
ebs_block_device.1440725774.encrypted             = true
ebs_block_device.1440725774.iops                  = 900
ebs_block_device.1440725774.snapshot_id           = snap-1111111111111
ebs_block_device.1440725774.volume_id             = vol-1111111111111
ebs_block_device.1440725774.volume_size           = 300
ebs_block_device.1440725774.volume_type           = gp2
ebs_optimized                                     = false
root_block_device.#                               = 1
root_block_device.0.delete_on_termination         = false
root_block_device.0.iops                          = 0
root_block_device.0.volume_id                     = vol-1111111111111
root_block_device.0.volume_size                   = 8
root_block_device.0.volume_type                   = standard

问题是 EBS 卷的索引是那个疯狂的整数 1440725774。我不知道为什么会这样。在控制台中,我感兴趣的列表中只有一张地图:

> aws_instance.jenkins_master_with_snap.ebs_block_device
[
  {    delete_on_termination = 1  device_name = /dev/xvdf  encrypted = 1  iops = 900  snapshot_id = snap-1111111111111  volume_id = vol-1111111111111  volume_size = 300  volume_type = gp2}
]

似乎引用任何这些键的唯一方法是直接使用该索引值:

> aws_instance.jenkins_master_with_snap.ebs_block_device.1440725774.volume_id
vol-1111111111111

当我不知道索引是什么时,有什么方法可以可靠地引用像这样的列表中的单个元素吗?我不能只是将该整数硬编码到上面的 template_file 资源中并假设它每次都相同。有没有人知道为什么会发生这种情况?

也许不用内联 ebs_block_device block, create a separate aws_ebs_volume resource, then attach it with an aws_volume_attachment。然后引用 aws_ebs_volume.name.id 属性来获取您需要的 ID。

示例(扩展自 aws_volume_attachment 中的 example code):

resource "aws_volume_attachment" "ebs_att" {
  device_name = "/dev/sdh"
  volume_id   = "${aws_ebs_volume.example.id}"
  instance_id = "${aws_instance.web.id}"
}

resource "aws_instance" "web" {
  ami               = "ami-21f78e11"
  availability_zone = "us-west-2a"
  instance_type     = "t1.micro"
  tags {
    Name = "HelloWorld"
  }

  subnet_id = "<REDACTED>"
}

resource "aws_ebs_volume" "example" {
  availability_zone = "us-west-2a"
  size              = 1
}

data "template_file" "example" {
    template    = "Your volume ID is $${volume_id}"

    vars {
     volume_id = "${aws_ebs_volume.example.id}"
    }
}

output "custom_template" {
  value = "${data.template_file.example.rendered}"
}

结果输出:

Outputs:

custom_template = Your volume ID is vol-0b1064d4ca6f89a15

然后您可以在您的模板变量中使用 ${aws_ebs_volume.example.id} 来填充您的 lambda。