question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error uploading object to S3 bucket: ReadRequestBody: unable to initialize upload │ caused by: seek assets/*

See original GitHub issue

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave “+1” or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

cdktf & Language Versions

cdktf: 0.9.4 Node.js: 16.14.1

Affected Resource(s)

aws_s3_bucket_object

Debug Output

https://gist.github.com/bestickley/faae600e7374e4a89a2563b713cc5c9d

Expected Behavior

Local directory should be uploaded to S3.

Actual Behavior

Error: Error uploading object to S3 bucket (terraform-20220322155938837600000002): ReadRequestBody: unable to initialize upload │ caused by: seek assets/SpaAssets/3594AD6B6CFC5A19C752DFEB9681AE72: The handle is invalid. │ │ with aws_s3_bucket_object.S3BucketObject, │ on cdk.tf.json line 239, in resource.aws_s3_bucket_object.S3BucketObject: │ 239:

Steps to Reproduce

Create a bucket like so:

#createBucket(oai: CloudfrontOriginAccessIdentity, stage: TerraformVariable) {
    const staticSiteBucket = new s3.S3Bucket(this, "StaticSiteBucket", {
      serverSideEncryptionConfiguration: {
        rule: {
          applyServerSideEncryptionByDefault: {
            sseAlgorithm: "AES256",
          },
        },
      },
    });
    const bucketToken = Token.asString(staticSiteBucket.arn);
    const bucketPolicy = new iam.DataAwsIamPolicyDocument(
      this,
      "S3BucketIamPolicyDoc",
      {
        version: "2012-10-17",
        statement: [
          {
            actions: ["s3:*"],
            condition: [
              {
                test: "Bool",
                values: ["false"],
                variable: "aws:SecureTransport",
              },
            ],
            effect: "Deny",
            principals: [{ type: "AWS", identifiers: ["*"] }],
            resources: [bucketToken, `${bucketToken}/*`],
          },
          {
            actions: ["s3:GetObject"],
            effect: "Allow",
            resources: [`${bucketToken}/*`],
            principals: [{ type: "AWS", identifiers: [oai.iamArn] }],
          },
        ],
      }
    );
    new s3.S3BucketPolicy(this, "S3BucketPolicy", {
      bucket: staticSiteBucket.id,
      policy: bucketPolicy.json,
    });
 
    const assets = new TerraformAsset(this, "SpaAssets", {
      path: "../ui/dist",
      type: AssetType.DIRECTORY,
    });
    new S3BucketObject(this, "S3BucketObject", {
      bucket: staticSiteBucket.id,
      key: "/",
      source: assets.path,
    });
    return staticSiteBucket;
  }

Verify cdk.tf.json:

"aws_s3_bucket_object": {
      "S3BucketObject": {
        "//": {
          "metadata": {
            "path": "tdmp-em-frontend/S3BucketObject",
            "uniqueId": "S3BucketObject"
          }
        },
        "bucket": "${aws_s3_bucket.StaticSiteBucket.id}",
        "key": "/${var.stage}",
        "source": "assets/SpaAssets/3594AD6B6CFC5A19C752DFEB9681AE72"
      }
    },

Verify asset exists: image

Important Factoids

References

https://www.terraform.io/cdktf/concepts/assets

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
bestickleycommented, Mar 25, 2022

After some more thought, I’d prefer to let cdktf name my bucket instead of externally defining it as the serverless example, so I’ve come up with this work around:

const dir = resolve(__dirname, "../../ui/dist");
    const files = getFilesSync(dir);
    for (const file of files) {
      const key = file.replace(dir, "").replace(/\\/g, "/");
      new S3BucketObject(this, `S3BucketObject-${key}`, {
        bucket: staticSiteBucket.id,
        key,
        source: file,
      });
    }
 
export function getFilesSync(dir: string): string[] {
  const dirents = readdirSync(dir, { withFileTypes: true });
  const files = dirents.map((dirent) => {
    const res = resolve(dir, dirent.name);
    return dirent.isDirectory() ? getFilesSync(res) : res;
  });
  return files.flat();
}

I know I could get JSON output and use aws s3 copy but I’d prefer to keep it in constructs if possible.

1reaction
jsteinichcommented, Mar 25, 2022

I think aws_s3_bucket_object only uploads a single file, not an entire directory.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve errors uploading data to or downloading data from ...
To run the SELECT INTO OUTFILE S3 or LOAD DATA FROM S3 commands using Amazon Aurora, follow the steps below: 1. Create an...
Read more >
Upload object to AWS S3 without creating a file using aws-sdk ...
Show what you are trying to upload. s3manager.UploadInput.Body is an io.Reader. Create an io.Reader using bytes.NewReader, strings.NewReader, ...
Read more >
aws_s3_bucket_object | Resources | hashicorp/aws
If you prefer to not have Terraform recreate the object, import the object using aws_s3_object . Provides an S3 object resource.
Read more >
[S3] RequestError with UploadPart call · Issue #2525 - GitHub
We are using MultipartUpload for uploading some mp3 files to S3.... ... RequestError: send request failed caused by: Put ...
Read more >
How to Set up Amazon S3 Upload Provider Plugin for ... - Strapi
You will first set up and configure an S3 storage bucket for your app. Then, you'll create a Strapi App using one of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found