你如何将 JS 文件作为 terraform 中的模块?

How do you source a JS file as a module in terraform?

我正在尝试执行以下操作:

module “git_file” {
  source = "git::https://githubXX.com/abc.js"
}

data "archive_file" “init” {
type        = "zip"
git_file = "${module.git_file.source}"
}

我无法完成上述工作。无论使用 https:// 还是 ssh://

如何将 JS 文件作为 terraform 中的模块?

A module 块用于将 Terraform 模块及其相关资源加载到特定模块路径下的模块中。它不能按您想要的方式使用。

To call a module means to include the contents of that module into the configuration with specific values for its input variables. Modules are called from within other modules using module blocks:

module "servers" {
  source = "./app-cluster"

  servers = 5
}

来源:Calling a Child Module - Modules- Configuration Language - Terraform Docs

这有点像其他语言中的 import、require 或 include。它不能用于下载用于 Terraform 模块的文件。

可以使用http data source来执行您描述的操作:

data "http" "git_file" {
  url = "https://githubXX.com/abc.js"
}

data "archive_file" “init” {
  type        = "zip"
  git_file = data.http.git_file.body
}

这也不太可能像您预期的那样工作。你肯定需要一个 link 到 GitHub 的原始来源。

您应该考虑另一种解决方案,包括将 abc.js 放在同一个存储库中或使用 null_resource with a local_exec provisioner 通过脚本下载它。

resource "null_resource" "" {
  provisioner "local-exec" {
    command = "git clone https://github.com/..."
  }
}

然后您将在本地获得这些文件供您使用,就像您 git 自己克隆 shell 一样。我不推荐这个。它很脆弱,可能会与其他工具发生奇怪的交互。