对本地文件创建的依赖

Nil*_*oud 3 terraform

我正在按照示例https://github.com/terraform-aws-modules/terraform-aws-eks/blob/master/aws_auth.tf使用 Terraform 设置 EKS 集群,现在我有两个 Terraform 文件:

kubeconfig.tf

resource "local_file" "kubeconfig" {
  content  = "${data.template_file.kubeconfig.rendered}"
  filename = "tmp/kubeconfig"
}

data "template_file" "kubeconfig" {
  template = "${file("template/kubeconfig.tpl")}"
...
}
Run Code Online (Sandbox Code Playgroud)

aws-auth.tf

resource "null_resource" "update_config_map_aws_auth" {
  provisioner "local-exec" {
    command = "kubectl apply -f tmp/config-map-aws-auth_${var.cluster-name}.yaml --kubeconfig /tmp/kubeconfig"
  }

  ...
 }
Run Code Online (Sandbox Code Playgroud)

当我运行这个 local-exec 命令失败

输出:错误:stat tmp/kubeconfig:没有这样的文件或目录

在第二次运行时它成功了。我认为该文件是在 local-exec 尝试使用它之后创建的,并且 local-exec 应该依赖于文件资源。所以我尝试通过使用插值(隐式依赖)来表达依赖关系,如下所示:

resource "null_resource" "update_config_map_aws_auth" {
  provisioner "local-exec" {
    command = "kubectl apply -f tmp/config-map-aws-auth_${var.cluster-name}.yaml --kubeconfig ${resource.local_file.kubeconfig.filename}"
  }
Run Code Online (Sandbox Code Playgroud)

但这总是给我

错误:资源“null_resource.update_config_map_aws_auth”供应商local-exec(#1):变量resource.local_file.kubeconfig.filename中引用了未知资源“resource.local_file”

yda*_*coR 6

resource.在最后一个代码块中使用插值时不需要该部分。

当 Terraform 刚启动时,它只有资源,因此您无需说某物是资源,因为这是唯一的情况。然后,他们添加了所需的一些分化的命名等等这些获取模块和数据源module.,并data.因此Terraform可以告诉的资源和数据源等分开。

所以你可能想要这样的东西:

resource "local_file" "kubeconfig" {
  content  = "${data.template_file.kubeconfig.rendered}"
  filename = "tmp/kubeconfig"
}

data "template_file" "kubeconfig" {
  template = "${file("template/kubeconfig.tpl")}"
  ...
}

resource "null_resource" "update_config_map_aws_auth" {
  provisioner "local-exec" {
    command = "kubectl apply -f tmp/config-map-aws-auth_${var.cluster-name}.yaml --kubeconfig ${local_file.kubeconfig.filename}"
  }
}
Run Code Online (Sandbox Code Playgroud)