首页 文章

Terraform应用巨大的索引值,例如EBS块存储

提问于
浏览
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已经有一个从构建过程映射到它的快照,因此该资源基本上只是用不同的快照替换它 . 我不确定这是否是我遇到问题的原因 .

我正在使用生成的资源属性来填充将被压缩并作为lambda函数上载的Python模板 . 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 . 我不知道为什么会这样 . 在控制台中,有's only a single map in the list I' m感兴趣:

> 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 资源中,并假设它每次都是相同的 . 有没有人有任何线索,为什么这首先发生?

1 回答

  • 1

    也许不是内联ebs_block_device块,而是创建一个单独的aws_ebs_volume资源,然后用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 .

相关问题