Unable to Provision Source Files for S3 Bucket in Terraform

40 Views Asked by At

Currently trying to use Terraform to create an S3 Bucket, set it up for static hosting and configurations, and load source files from my local machine.

Below is the code I have, everything is working as intended, but the source files are not loading into the bucket.

provider "aws" {
  region = "us-east-1"
}

#creates the bucket, names it sullyelh.com, tf var is sullyelhcom
resource "aws_s3_bucket" "sullyelhcom" {
  bucket = "sullyelh.com"

  tags = {
    Name = "Resume"
  }
}

#configures the bucket to use index.html/error.html for hosting
resource "aws_s3_bucket_website_configuration" "sullyelhcom" {
  bucket = aws_s3_bucket.sullyelhcom.id

  index_document {
    suffix = "index.html"
  }

  error_document {
    key = "error.html"
  }
}

#Loads the index and error files from my local machine
resource "aws_s3_object" "provision_source_files" {
  bucket = aws_s3_bucket.sullyelhcom.id

  for_each = fileset("aws-resume/", "**/*.*")

  key = each.value
  source = "aws-resume/${each.value}"
  content_type = file("aws-resume/${each.value}")
}

#essentially unchecks the block-public-access settings on s3 bucket
resource "aws_s3_bucket_public_access_block" "sullyelhcom" {
  bucket = aws_s3_bucket.sullyelhcom.id

  block_public_acls       = false
  block_public_policy     = false
  ignore_public_acls      = false
  restrict_public_buckets = false
}

#read policy for public
resource "aws_s3_bucket_policy" "sullyelhcom" {
  bucket = aws_s3_bucket.sullyelhcom.id
  policy = jsonencode({
    Version = "2012-10-17",
    Statement = [
      {
        Sid       = "PublicReadGetObject",
        Effect    = "Allow",
        Principal = "*",
        Action    = "s3:GetObject",
        Resource  = "${aws_s3_bucket.sullyelhcom.arn}/*"
      }
    ]
  })
}

Some more context: I am trying to upload a file structure like this:

aws-resume\
- static-files\
-- css\
-- js\
- index.html
- resume.html
- errro.html
0

There are 0 best solutions below