如何减少模块中的样板配置?

How to reduce boilerplate provisioning in modules?

所以我有两个完全相同的 Terraform 模块...例外是有一个额外的远程执行步骤

模块 A

  provisioner "remote-exec" {
    inline = [
      "sudo chmod 777 -R ${var.setup_files_destination}",
      "sudo ${var.setup_files_destination}/scripts/setup.sh ${var.setup_files_destination}",
    ]

    connection {
      user        = "ec2-user"
      private_key = "${file(var.private_key_location)}"
    }
  }

模块 B - 区别在于要执行一个额外的脚本

  provisioner "remote-exec" {
    inline = [
      "sudo chmod 777 -R ${var.setup_files_destination}",
      "sudo ${var.setup_files_destination}/scripts/setup.sh ${var.setup_files_destination}",
    ]

    connection {
      user        = "ec2-user"
      private_key = "${file(var.private_key_location)}"
    }
  }

  provisioner "remote-exec" {
    inline = [
      "sudo chmod 777 -R ${var.setup_files_destination}",
      "${var.setup_files_destination}/scripts/setup_special.sh ${var.setup_files_destination}",
    ]

    connection {
      user        = "ec2-user"
      private_key = "${file(var.private_key_location)}"
    }
  }

除此脚本差异外,模块具有完全相同的步骤。重复的 terraform 代码量约为 50 行。

我想在 Terraform 中做什么

所以在 Terraform 方面,我想指定要作为列表执行的脚本,比如

变量

variable "commands_to_execute" {
  type = "list"
}

普通步骤

provisioner "remote-exec" {
  inline = "[${var.commands_to_execute}]"

  connection {
    user        = "ec2-user"
    private_key = "${file(var.private_key_location)}"
  }
}

共同步骤的感知使用

module "instances" {
  ... rest of declaration...
  commands_to_execute = [
    "sudo chmod 777 -R ${var.setup_files_destination}",
    "sudo ${var.setup_files_destination}/scripts/setup.sh ${var.setup_files_destination}",
  ]
}

当我 运行 这个时,我得到

Error: module 'instances': unknown variable referenced: 'setup_files_destination'; define it with a 'variable' block

所以setup_files_destination是在模块内部定义的变量,而不是在使用模块的.tf文件中。它有一个默认值,实际上是远程实例上 shell 脚本目录的位置。

我想在模块中保留 setup_files_destination。所以我 寻找的是一种将变量 的插值推迟到实际使用模块时的方法。 在 terraform 中这可能吗?

不可能按照您尝试的方式延迟插值,您可以使用 null_resource 并计数来实现您想要做的事情。当您在模块初始化

中定义变量run_additional_setup = true时,这只会运行脚本

示例:

resource "null_resource" "additional_setup" {
  count      = "${var.run_additional_setup == true ? 1 : 0}"
  depends_on = ["aws_instance.my_instance"]

  connection {
    user        = "ec2-user"
    private_key = "${file(var.private_key_location)}"
  }

  provisioner "remote-exec" {
    inline = [
      "sudo chmod 777 -R ${var.setup_files_destination}",
      "${var.setup_files_destination}/scripts/setup_special.sh ${var.setup_files_destination}",
    ]
  }
}