I have used the following code:
module "instance" {
for_each = var.worker_private_ip
source = "../../modules/ec2"
env = var.env
project_name = var.project_name
ami = var.ami
instance_type = var.instance_type
subnet_id = module.vpc.subnet_id
vpc_security_group_ids = [module.security_group.security_group_id]
private_ip = each.value
key_name = var.public_key_name
user_data = file("${path.module}/startup.sh.tpl")
}
When I send terraform plan/apply command, I receive an error
path.module is "."
│ Invalid value for "path" parameter: no file exists at "./startup.sh.tpl";
So, terraform searching for file in current directory, but I expect for searching in (../../modules/ec2/)
I have startup.sh.tpl file in module folder and if I set user_data to:
user_data = file("../../modules/ec2/startup.sh.tpl")
everything is fine, also if I move sciprt to current directory everything ok too, plan and apply commands are correct
What am I doing wrong? Why ${path.module} points to the current directory ".", but not to ../../modules/ec2/?
From the docs:
path.moduleis the filesystem path of the module where the expression is placed.
This means that it will return the relative path between your project's root folder and the location where path.module is used.
For example:
.tf file which is inside your ../../modules/ec2/ folder, it will return ../../modules/ec2;main.tf (or any .tf file) in your project's root folder, it will return .In your case, if the user data is inside the module, probably there would be need for it to make it as an input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With