英文:
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"
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论