【问题标题】:Dependency on local file creation依赖本地文件创建
【发布时间】:2019-01-27 13:00:16
【问题描述】:

我正在按照示例 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")}"
...
}

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

  ...
 }

当我运行它时,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}"
  }

但这总是给我

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

【问题讨论】:

  • 在最后一个代码块中使用插值时不需​​要resource. 部分。当 Terraform 刚开始时,它只有资源,所以你不需要说某物是资源,因为这是唯一的情况。然后他们添加了模块和数据源,这些模块和数据源需要在命名上有所区别,因此它们得到 module.data.,因此 Terraform 可以区分资源和数据源等。
  • 谢谢!那行得通。

标签: terraform


【解决方案1】:

在最后一个代码块中使用插值时,您不需要 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}"
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 2018-02-20
    • 2020-01-24
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多