2017-10-03 7 views
1

null_resourceを使用してマップ変数をtriggersに列挙し、この列挙の結果を別のリソースに使用しようとしています。null_resourceとトリガーを使用してマップ変数を列挙する

resource "null_resource" "dummy" { 
    count = "${length(var.file_map)}" 

    triggers { 
    filename = "${element(keys(var.file_map), count.index)}" 
    content = "${var.file_map[element(keys(var.file_map), count.index)]}" 
    } 
} 

variable "file_map" { 
    type = "map" 

    default = { 
    "foo.txt" = "foo" 
    "bar.txt" = "bar" 
    } 
} 

出力:

An execution plan has been generated and is shown below. 
Resource actions are indicated with the following symbols: 
    + create 

Terraform will perform the following actions: 

    + null_resource.dummy[0] 
     id:    <computed> 
     triggers.%:  "2" 
     triggers.content: "bar" 
     triggers.filename: "bar.txt" 

    + null_resource.dummy[1] 
     id:    <computed> 
     triggers.%:  "2" 
     triggers.content: "foo" 
     triggers.filename: "foo.txt" 


Plan: 2 to add, 0 to change, 0 to destroy. 

をしかし、私は別のリソースに列挙の結果を使用しようとすると、それは失敗します。

これは動作します

resource "local_file" "some_files" { 
    content = "${null_resource.dummy.triggers.content}" 
    filename = "${null_resource.dummy.triggers.filename}" 
} 

resource "null_resource" "dummy" { 
    count = "${length(var.file_map)}" 

    triggers { 
    filename = "${element(keys(var.file_map), count.index)}" 
    content = "${var.file_map[element(keys(var.file_map), count.index)]}" 
    } 
} 

variable "file_map" { 
    type = "map" 

    default = { 
    "foo.txt" = "foo" 
    "bar.txt" = "bar" 
    } 
} 

出力:

Error running plan: 1 error(s) occurred: 

* local_file.some_files: 1 error(s) occurred: 

* local_file.some_files: Resource 'null_resource.dummy' not found for variable 
         'null_resource.dummy.triggers.content' 

動作させる方法はありますか?

答えて

1

elementと同様の使用方法は、local_fileで同じことを行う必要があります。実行terraform apply

resource "local_file" "some_files" { 
    count = "${length(var.file_map)}" 

    content = "${element(null_resource.dummy.*.triggers.content, count.index)}" 
    filename = "${element(null_resource.dummy.*.triggers.filename, count.index)}" 
} 

resource "null_resource" "dummy" { 
    count = "${length(var.file_map)}" 

    triggers { 
    filename = "${element(keys(var.file_map), count.index)}" 
    content = "${var.file_map[element(keys(var.file_map), count.index)]}" 
    } 
} 

variable "file_map" { 
    type = "map" 

    default = { 
    "foo.txt" = "foo" 
    "bar.txt" = "bar" 
    } 
} 

、それはあなたが詳細をお知りになりたい場合は、cat terraform.tfstateはあなたにこれがどのように機能するかについての詳細を与える二つのファイル

$ cat bar.txt 
bar 
$ cat foo.txt 
foo 

を生成します。

+0

うわー、ちょうどうわー。どうもありがとうございます。私は[splat hack](https://github.com/hashicorp/terraform/issues/11566#issuecomment-289417805)を使用しようとしていましたが失敗しました。今はすべて理にかなっています。 – beatcracker

関連する問題