I have tried many times yet Terraform can’t see my object resource. What could be wrong with my code?
My bucket and bucket website resource gets created, but the object resource doesn’t.
resource "aws_s3_bucket" "My_bucket" {
bucket = "my-test-bucket-for-cloudfront"
tags = {
Name = "My bucket"
}
}
resource "aws_s3_bucket_public_access_block" "public_bucket" {
bucket = aws_s3_bucket.My_bucket.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
resource "aws_s3_object" "object" {
# Use a for loop to grab every file in the folder.
# The fileset() function is used to read all the files in a folder
for_each = fileset("/travel-agency-html-template/", "**")
bucket = "my-test-bucket-for-cloudfront"
# Use each.value to grab every file inside the folder
key = each.value
source = "/travel-agency-html-template/${each.value}"
acl = "public-read"
etag = filemd5("travel-agency-html-template/${each.value}")
depends_on = [ aws_s3_bucket.My_bucket ]
}
resource "aws_s3_bucket_website_configuration" "travel_agency" {
bucket = aws_s3_bucket.My_bucket.id
index_document {
suffix = "/travel-agency-html-template/index.html"
}
error_document {
key = "/travel-agency-html-template/404.html"
}
}
>Solution :
The way you currently have the fileset specified is that it searches within the folder /travel-agency-html-template at the root of your file system. I doubt that is where the files are actually located. Instead you should e.g. use
fileset("${path.module}/travel-agency-html-template", "**")
depending on where the files are actually located relative to your terraform code.