aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Assets: Zip archive is corrupted for Lambda code

Open kadishmal opened this issue 6 years ago • 17 comments

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

kadishmal avatar Jan 03 '20 17:01 kadishmal

@kadishmal how did you obtain the .zip file created by the CDK? It's not supposed to be under the cdk.out directory...

eladb avatar Jan 07 '20 07:01 eladb

Any news on this issue?

I have a similar issue. When I use a zip as a source code for a Lambda, the zip that is copied to the S3 bucket is corrupt

rubenjgarcia avatar Feb 25 '20 13:02 rubenjgarcia

Corrupted in what way? Can you share some details?

eladb avatar Feb 25 '20 17:02 eladb

No response yet from the requesters. Closing.

Please re-open with more details. Thanks!

nija-at avatar Mar 03 '20 17:03 nija-at

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

seawatts avatar Jun 11 '20 20:06 seawatts

Did you ever figure this out @seawatts @kadishmal @rubenjgarcia? One of our developers is experiencing the same error now.

brennick avatar Aug 26 '20 00:08 brennick

We resolved the issue, the S3 bucket had corrupt files. We cleared out the entire S3 bucket of arn:aws:s3:::cdk-hiudfsaf-assets-34723248234-us-west-2 (replaced IDs in S3 folder name) and now deployments are working again.

brennick avatar Aug 26 '20 18:08 brennick

CDK version 1.67.0 - getting the same error. Code.from_asset(path-to-a-zip-file). In documentation it clearly states: path (str) – Either a directory with the Lambda code bundle or a .zip file.

I can find the zip file in the assets folder of cdk bucket. Downloaded it and tried to open and indeed it fails to open. Windows says the file is invalid. Thats it.

@eladb Any thoughts on this?

laimonassutkus avatar Oct 13 '20 11:10 laimonassutkus

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.

mattiLeBlanc avatar Oct 31 '20 08:10 mattiLeBlanc

@mattiLeBlanc thank you saved my time :)

bpcrao avatar Dec 10 '20 14:12 bpcrao

@ookangzheng I had the same version and the same issue. I have reverted cdk to 1.128.0 and did what was said there https://github.com/aws/aws-cdk/issues/5636#issuecomment-719901174 removed my failing deploy hash zip file from s3 bucket and it all works again

seyfer avatar Jan 25 '22 16:01 seyfer

I had the same problem when bundling the lambda code when calling lambda.code.fromAsset. ended up zipping the /asset-output folder myself, so the CDK will upload the zip file as is (CDK uploads the zip file as is if it is the only file in the output folder) added this command at the end of bundling.command array.

zip -rm bundling.zip .

this will zip and remove source files.

jacwak avatar Jan 26 '22 04:01 jacwak

Can this please be reopened? I am also experiencing this.

louisa-ba avatar Nov 21 '22 01:11 louisa-ba

@nija-at a week isn't a reasonable stale period to close an issue. there are quite a few users experiencing the same. please be prudent and reopen so they can contribute.

shellscape avatar Dec 14 '22 19:12 shellscape

This hit me just now with cdk 2.72.1

lschierer avatar Apr 05 '23 02:04 lschierer

And back at it again with cdk v2.87 SMFH

ETisREAL avatar Jul 13 '23 08:07 ETisREAL

I had the same issue. It seems that somehow the index.py file under "cdk.out\assset." directory was 0 bytes.
Deleting the "cdk.out\asset." directory and the zipfile from the "cdk assets" bucket fixed it

angelosc avatar Jul 20 '23 11:07 angelosc