How to access a file from the module

I’m trying to create an S3 bucket with the policy as part of the module.
instead of EOF construct for policy, I’ve been thinking to use a file (with the policy). But I’m getting the :

Invalid function argument

What is the proper way to access another file in a module?

Code:

cat S3.tf
...
resource "aws_s3_bucket" "this" {...}
...
resource "aws_s3_bucket_policy" "SSL" {
  bucket = aws_s3_bucket.this.id
  policy = file("ssl.json")
}

in the module directory:

ls -l ../../../NGG/modules/AWS/S3/
total 8
-rwxrwx--- 1 root vboxsf 1071 Dec  9 17:24 input.tf
-rwxrwx--- 1 root vboxsf    0 Dec  9 17:24 outputs.tf
-rwxrwx--- 1 root vboxsf 1614 Dec 15 12:04 S3.tf
-rwxrwx--- 1 root vboxsf  527 Dec 15 11:58 ssl.json

on the flip side, the following code works great, but i’m not using it in the module:


resource "aws_iam_policy" "EC2_Execute" {
  description = "Policy allows a limited execution of EC2s"
  name = "EC2_Execute"
  path = "/"
  policy = file("policy/EC2-execute.json")
}

You need to use ${path.module} as part of the path so that it can find the file correctly.

1 Like

thank you @stuart-c . worked.