首页 文章

对本地文件创建的依赖性

提问于
浏览
1

我按照示例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'provisioner local-exec(#1):变量resource.local_file.kubeconfig.filename中引用的未知资源'resource.local_file'

1 回答

  • 2

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

相关问题