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.

Assets: Zip archive is corrupted for Lambda code

See original GitHub issue

Reproduction Steps

I have a regular Lambda stack that looks something like:

const lambdaDeleteDDBFn = new Function(this, 'LambdaToDeleteDDB', {
    runtime: Runtime.NODEJS_10_X,
    handler: 'delete-ddb-table-lambda.handle',
    // The path should be relative to the root directory.
    code: Code.asset('./lib/lambda'),
    timeout: Duration.seconds(300),
    environment: {
        REGION: this.region,
        TABLE_NAME: tableName
    }
});

The ./lib/lambda directory looks normal as well which includes already compiled JS files:

$ ll lib/lambda/
total 64
-rw-r--r--  1 user  1896053708  5324  3 Jan 09:17 create-invalid-stack-lambda.js
-rw-r--r--  1 user  1896053708  4719  3 Jan 09:17 custom-resource-utils.js
-rw-r--r--  1 user  1896053708  3501  3 Jan 09:17 delete-ddb-table-lambda.js
-rw-r--r--  1 user  1896053708  1148  2 Jan 15:25 custom-resource-utils.ts
-rw-r--r--  1 user  1896053708   870  2 Jan 15:18 delete-ddb-table-lambda.ts
-rw-r--r--  1 user  1896053708  1401  2 Jan 14:10 create-invalid-stack-lambda.ts

Once CDK app is build, it generates cdk.out with bunch of template JSON files as well as the asset.XXX directory that is a copy of the ./lib/lambda directory.

$ ll build/cdk.out/asset.12efd52ab96c4083197e09d00af8f35f6f4f02b951217130bede554ac178685b
total 64
-rw-r--r--  1 user  1896053708  5324  3 Jan 09:18 create-invalid-stack-lambda.js
-rw-r--r--  1 user  1896053708  1401  3 Jan 09:18 create-invalid-stack-lambda.ts
-rw-r--r--  1 user  1896053708  4719  3 Jan 09:18 custom-resource-utils.js
-rw-r--r--  1 user  1896053708  1148  3 Jan 09:18 custom-resource-utils.ts
-rw-r--r--  1 user  1896053708  3501  3 Jan 09:18 delete-ddb-table-lambda.js
-rw-r--r--  1 user  1896053708   870  3 Jan 09:18 delete-ddb-table-lambda.ts

We can confirm that bytes sizes of files match the original files.

Error Log

Then this directory gets zipped by CDK by the zipDirectory() function which, however, generates a corrupted ZIP file. I think the underlying archiver NPM module has a bug somewhere.

$ unzip -t build/cdk.out/asset.12efd52ab96c4083197e09d00af8f35f6f4f02b951217130bede554ac178685b.zip
Archive:  build/cdk.out/asset.12efd52ab96c4083197e09d00af8f35f6f4f02b951217130bede554ac178685b.zip
    testing: create-invalid-stack-lambda.js   bad CRC 28acfbde  (should be 6bd7e1c9)
file #2:  bad zipfile offset (local header sig):  2731
file #3:  bad zipfile offset (local header sig):  5009
file #4:  bad zipfile offset (local header sig):  5771
    testing: delete-ddb-table-lambda.js   OK
    testing: delete-ddb-table-lambda.ts   OK
At least one error was detected in build/cdk.out/asset.12efd52ab96c4083197e09d00af8f35f6f4f02b951217130bede554ac178685b.zip.

When I try to force unzip locally, the following error is displayed:

Unable to expand "asset.12efd52ab96c4083197e09d00af8f35f6f4f02b951217130bede554ac178685b.zip` into "cdk.out".

The deployment to the actual stack also fails with the following error:

Could not unzip uploaded file. Please check your file, then try to upload again. (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: 853d7206-2f80-422e-a7ff-01c15b960ed9)

When I check the file size, I see the following:

$ ll build/cdk.out/
-rw-r--r--  1 user  1896053708    10218  3 Jan 09:53 asset.12efd52ab96c4083197e09d00af8f35f6f4f02b951217130bede554ac178685b - CORRECT.zip
-rw-r--r--@ 1 user  1896053708     9280  3 Jan 09:18 asset.12efd52ab96c4083197e09d00af8f35f6f4f02b951217130bede554ac178685b.zip

Note that the file size of the correct zip which I manually generated is 10KB while the actual one generated by CDK is 9KB.

Environment

  • CLI Version : 1.17.1
  • Framework Version: 1.17.1
  • OS : macOS Mojave
  • Language : TypeScript

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
mattiLeBlanccommented, Oct 31, 2020

With CDK 1.67, all of a sudden I got the same error when deploying from my command line to my cloud env:

Could not unzip uploaded file. Please check your file, then try to upload again. (Service: AWSLambdaInternal; Status Code: 400; Error Code: Inval Even going back to master branch, to code that is currently deployed, I would get the same error. I just cleaned my CDKtoolkit assets folder in S3 and then I could deploy again. Then I went back to my new branch and was able to deploy too. So weird.

6reactions
seawattscommented, Jun 11, 2020

I’m getting the same issue here as well. @eladb the zip files in the cdk.out folder are just the asset.* files for me.

The error that I get from running cdk deploy MyStack is

 2/5 | 1:40:29 PM | UPDATE_FAILED        | AWS::Lambda::Function                       | Watchful/AlarmFunction (WatchfulAlarmFunction84AB3947) Could not unzip uploaded file. Please check your file, then try to upload again. (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: 9b1a383e-857a-4422-a664-297df2e02b13)

The error I get then I try to run unzip cdk.out/asset.0ea27e9d503ee9ddf6c3eb85001baf39528958392bb3ebef1ec069c68450c946.zip is

Archive:  cdk.out/asset.0ea27e9d503ee9ddf6c3eb85001baf39528958392bb3ebef1ec069c68450c946.zip
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of cdk.out/asset.0ea27e9d503ee9ddf6c3eb85001baf39528958392bb3ebef1ec069c68450c946.zip or
        cdk.out/asset.0ea27e9d503ee9ddf6c3eb85001baf39528958392bb3ebef1ec069c68450c946.zip.zip, and cannot find cdk.out/asset.0ea27e9d503ee9ddf6c3eb85001baf39528958392bb3ebef1ec069c68450c946.zip.ZIP, period. 

I’m on aws-cdk: 1.45.0 and a Macbook Pro 10.15.4

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js AWS Lambda + Archiver lib - Error in zip file creation
The problem is that you are not being able to zip your files properly, this can occurs by many issues, including:.
Read more >
AWS::Lambda::Function Code - AWS CloudFormation
For a .zip file deployment package, you can specify the location of an object in Amazon S3 ... (Node.js and Python) The source...
Read more >
Upload binary files to S3 using AWS API Gateway ... - Medium
Upload binary files to S3 using AWS API Gateway with AWS Lambda. How to use AWS API Gateway endpoint with Python AWS Lambda...
Read more >
@aws-cdk/aws-lambda - npm
zip file in the local filesystem which will be zipped and uploaded to S3 before deployment. See also bundling asset code. lambda.Code.fromDockerBuild(path, ...
Read more >
aws.lambda.Function - Pulumi
Function("testLambda", { code: new pulumi.asset.FileArchive("lambda_function_payload.zip"), role: iamForLambda.arn, handler: "index.test", ...
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