Terraform Cloud – “path” 参数的值无效:文件不存在

huangapple go评论75阅读模式
英文:

Terraform Cloud - Invalid value for "path" parameter: no file exists

问题

从我的本地迁移到Terraform Cloud后,我收到了以下消息。我的代码库在Gitlab中。我认为可以像我在本地做的那样引用文件,对吗?

在我的Terraform文件中,我引用文件如下:

resource "aws_instance" "server" {
  ami           = var.aws_ami_id
  subnet_id     = "${element(var.public_subnet.*.id, 3)}"
  key_name      = var.ec2_key_pair
  instance_type = "t2.micro"
  vpc_security_group_ids = [var.aws_security_group.main-sg.id]
  associate_public_ip_address = true
  user_data       = file("user-data-server.sh")

  tags = {
    Name = "Server"
  }
}

文件"user-data-server.sh"位于与此Terraform脚本相同的目录中。

英文:

Moving to terraform cloud from my local receiving the below message. My codebase is in Gitlab. I presume its supported to reference the file just like I did locally?

> on ../modules/instances/instance.tf line 32, in resource "aws_instance" "server":
user_data = file("user-data-server.sh")
while calling file(path)
Invalid value for "path" parameter: no file exists at "user-data-server.sh"; this function works only with files that are distributed as part of the configuration source code, so if this file will be created by a resource in this configuration you must instead obtain this result from an attribute of that resource.

My terraform file code references file as:

resource "aws_instance" "server" {
  ami           = var.aws_ami_id
  subnet_id     = "${element(var.public_subnet.*.id, 3)}"
  key_name      = var.ec2_key_pair
  instance_type = "t2.micro"
  vpc_security_group_ids = [var.aws_security_group.main-sg.id]
  associate_public_ip_address = true
  user_data       = file("user-data-server.sh")

  tags = {
    Name = "Server"
  }
}

The file user-data-server.sh is in the same directory as this terraform script.

答案1

得分: 1

根据显示的错误信息,Terraform 无法在指定的位置找到脚本。由于使用了模块来创建 EC2 实例,并且指定的文件路径好像是在与根模块存储在同一目录内一样,这导致了错误。为了解决这个问题,Terraform 提供了几个内置选项之一是 path.module

path.module 是表达式所放置的模块的文件系统路径。我们不建议在写操作中使用 path.module,因为它在使用远程或本地模块源时会产生不同的行为。多次调用本地模块会使用相同的源目录,每次调用都会覆盖 path.module 中的数据。这可能导致竞态条件和意外结果。

为了修复问题,请使用以下代码:

resource "aws_instance" "server" {
  ami           = var.aws_ami_id
  subnet_id     = "${element(var.public_subnet.*.id, 3)}"
  key_name      = var.ec2_key_pair
  instance_type = "t2.micro"
  vpc_security_group_ids = [var.aws_security_group.main-sg.id]
  associate_public_ip_address = true
  user_data       = file("${path.module}/user-data-server.sh")
  tags = {
    Name = "Server"
  }
}
英文:

Based on the error that is displayed, terraform cannot find the script at the location specified. Since a module is used to create the EC2 instance, and the path to the file specified is as if it were inside of the same directory where the root module is stored, that has caused the error. To help with that, terraform has several built-in options, one of which is path.module:

> path.module is the filesystem path of the module where the expression is placed. We do not recommend using path.module in write operations because it can produce different behavior depending on whether you use remote or local module sources. Multiple invocations of local modules use the same source directory, overwriting the data in path.module during each call. This can lead to race conditions and unexpected results.

To fix the error from the question, this should help:

resource "aws_instance" "server" {
  ami           = var.aws_ami_id
  subnet_id     = "${element(var.public_subnet.*.id, 3)}"
  key_name      = var.ec2_key_pair
  instance_type = "t2.micro"
  vpc_security_group_ids = [var.aws_security_group.main-sg.id]
  associate_public_ip_address = true
  user_data       = file("${path.module}/user-data-server.sh")
  tags = {
    Name = "Server"
  }
}

huangapple
  • 本文由 发表于 2023年7月23日 19:10:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定