对本地文件创建的依赖性



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

当我运行这个命令时,本地执行命令失败

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

在第二次运行时,它成功。我认为该文件是在 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 :中引用的未知资源"resource.local_file" 变量 resource.local_file.kubeconfig.filename

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

最新更新