Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

Organization

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

バージョン

➤ 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.

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What are the problem?