LoginSignup
9
5

More than 3 years have passed since last update.

terraform module内でtemplatefile関数を実行した際に、ファイルが見つけられなくて困った

Posted at

バージョン

➤ terraform --version
Terraform v0.12.6

ディレクトリ構成

.
└── ecs_task_definition
    ├── main.tf
    └── templates
        └── container_definitions.tpl.json

リソース

resource "aws_ecs_task_definition" "default" {
  ~~ 省略 ~~

  templatefile("./templates/container_definitions.tpl.json", { hoge = "fuga" }
}

この時に no file exists になってしまう。

➤ terraform plan

Error: Error in function call

  on ./modules/ecs_task_definition/main.tf line 54, in resource "aws_ecs_task_definition" "default":
  54:   container_definitions    = templatefile("./templates/container_definitions.tpl.json", { hoge = fuga })

Call to function "templatefile" failed: no file exists at
templates/container_definitions.tpl.json.

解決法

相対パスではなく、 path.module を使うと解決した。

resource "aws_ecs_task_definition" "default" {
  ~~ 省略 ~~

  templatefile("${path.module}/templates/container_definitions.tpl.json", { hoge = "fuga" }
}

参考: https://www.terraform.io/docs/configuration/functions/templatefile.html

余談

軽くしか探していないが、path.moduleに関してのドキュメントは見つけられなかった。
terraform consoleで path object が存在することがわかるが、どういったattributeがあるのかはわからない。ソースコード読むべきだろうなあ

> path


> Error: Invalid "path" attribute

  on  line 0:
  (source code not available)

The "path" object does not have an attribute named "".


Error: Invalid reference

  on <console-input> line 1:
  (source code not available)

The "path" object cannot be accessed directly. Instead, access one of its
attributes.

9
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5