1

我正在使用 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 脚本需要volume-id来自此实例的 EBS 块设备。

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}"
    }
}

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

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上面的资源中,并假设它每次都相同。有没有人知道为什么会发生这种情况?

4

1 回答 1

1

也许不是内联ebs_block_device块,而是创建一个单独的aws_ebs_volume资源,然后使用aws_volume_attachment附加它。然后引用aws_ebs_volume.name.id属性来获取你需要的ID。

示例(从中的示例代码扩展aws_volume_attachment):

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。

于 2018-04-24T16:09:16.563 回答