S3 api error NoSuchKey: The specified key does not exist

I am deploying MWAA and need a few files on bucket. I am uploading files using the following code:

resource "aws_s3_object" "reqs" {
  for_each = fileset("files/", "*")
  bucket   = aws_s3_bucket.mwaa-bucket.id
  key      = each.value
  source   = "files/${each.value}"
  etag     = filemd5("files/${each.value}")

  depends_on = [
    aws_s3_bucket.mwaa-bucket, 
    aws_s3_bucket_public_access_block.mwaa-bucket, 
    aws_s3_bucket_versioning.mwaa-bucket, 
    aws_s3_bucket_server_side_encryption_configuration.mwaa-bucket
  ]
}

But always getting the error “api error NoSuchKey: The specified key does not exist” despite the file was uploaded successfully and exists on s3 bucket

│ Error: listing tags for S3 (Simple Storage) Object (arn:aws:s3:::bucket/requirements.txt): operation error S3: GetObjectTagging, https response error StatusCode: 404, RequestID: 2AXGKGM2B3QEGVEB, HostID: fEZyZ1HwAdsszGaOnBUzyeOvGs/kHGfh61QwozR4uVP8dCjMLHmYDEkv8HnH6k9n5QPE0RN6DeM=, api error NoSuchKey: The specified key does not exist.
│ 
│   with module.base.aws_s3_object.reqs["requirements.txt"],
│   on base/main.tf line 614, in resource "aws_s3_object" "reqs":
│  614: resource "aws_s3_object" "reqs" {
│ 

Any way to avoid this issue ?

The fileset function returns a set, so for_each would not have set a value. Your configuration should work once you replace all instances of each.value with each.key.

Result exactly the same.

Switched to local-exec

resource "aws_s3_bucket" "mwaa-bucket" {
  bucket = "${var.project_name}-mwaa-bucket"
  force_destroy = true

  provisioner "local-exec" {
    command = "aws s3 cp files/requirements.txt s3://${self.id}/requirements.txt ; aws s3 cp files/plugins.zip s3://${self.id}/plugins.zip"
  }
}