Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is Terraform Not Picking My Object Resource?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading