Here is a solution for uploading a directory (including subdirectories) to s3 while setting the content-type.
locals {
mime_types = {
".html" = "text/html"
".css" = "text/css"
".js" = "application/javascript"
".ico" = "image/vnd.microsoft.icon"
".jpeg" = "image/jpeg"
".png" = "image/png"
".svg" = "image/svg+xml"
}
}
resource "aws_s3_object" "upload_assets" {
bucket = aws_s3_bucket.www_bucket.bucket
for_each = fileset(var.build_path, "**")
key = each.value
source = "${var.build_path}/${each.value}"
content_type = lookup(local.mime_types, regex("\\.[^.]+$", each.value), null)
etag = filemd5("${var.build_path}/${each.value}")
}
var.build_path
is the directory containing your assets. This line:
content_type = lookup(local.mime_types, regex("\\.[^.]+$", each.value), null)
gets the file extension by matching the regex and then use provided locals map to lookup the correct content_type
Credit: https://engineering.statefarm.com/blog/terraform-s3-upload-with-mime/