AWS CDK: Uploaded file must be a non-empty zip
Asked Answered
P

3

6

I have written a simple hello world lambda function to deploy but after the command cdk deploy it is giving this error. Can someone please guide about this?

enter image description here

Prevaricator answered 14/12, 2021 at 22:42 Comment(2)
Well the error message says you have uploaded an empty zip file.Soninlaw
Maybe related to #65917181?Ruinous
R
5

Steps helps to resolve it...

  • delete cdk.out (directory)

  • run command

    • cdk synth
    • cdk bootstrap
    • cdk deploy
Recurved answered 29/9, 2022 at 18:38 Comment(0)
S
4

This issue might be caused by https://github.com/aws/aws-cdk/issues/12536. You should try:

  1. Upgrading node.js version
  2. Deleting cdk.out
  3. Upgrade to latest CDK version
  4. Delete the asset directly from S3 (bucket will be something like cdk-hnb659fds-assets-<ACCOUNT NUMBER>-<REGION>)
  5. Deploy again

CDK doesn't reupload the asset unless it changed. That's why deleting it and maybe forcing a change after upgrading node.js is required.

If all else fails, try the script I wrote that downloads the asset, fixes it by rezipping, and uploads it again. It's expecting to run in the root of your project as it looks for cdk.out.

#!/bin/bash
set -ex

ASSEMBLY_DIRECTORY=`jq -r '.artifacts[] | select(.type == "cdk:cloud-assembly") | .properties.directoryName' cdk.out/manifest.json`
ASSET_MANIFESTS=`jq -r '.artifacts[] | select(.type == "cdk:asset-manifest") | .properties.file' cdk.out/$ASSEMBLY_DIRECTORY/manifest.json`
cd cdk.out/$ASSEMBLY_DIRECTORY
ASSETS=`jq -r '.files[].destinations[] | "s3://" + .bucketName + "/" + .objectKey' $ASSET_MANIFESTS | grep zip`

TMP=`mktemp -d`
cd $TMP

for ASSET in $ASSETS
do
    if aws s3 ls $ASSET; then
        aws s3 cp $ASSET pkg.zip
        mkdir s
        cd s
        if ! unzip ../pkg.zip; then echo bad zip; fi
        rm ../pkg.zip
        zip -r ../pkg.zip * .gitempty
        aws s3 cp ../pkg.zip $ASSET
        cd ..
        rm -rf s
    fi
done

rm -rf $TMP

You can confirm you're having the same issue I was having by downloading the asset zip file. Try extracting it with unzip. If it complains about the checksum or CRC, you had the same issue.

Sudor answered 17/12, 2021 at 19:22 Comment(2)
Making a change to my TypeScript lambda and then redeploying fixed this issue for meBorderline
Thanks! Was running around in circles trying to fix this for the past couple days. Making a change to my lambda python function also fixed this issue for me.Simonson
W
0

For me it occurred in WSL2.

It turned out it was introduced when I accidentaly npm ied in windows console.

Solution was then:

in WSL2:

  • rm -r node_modules
  • rm -r cdk.out
  • npm i
  • cdk synth

Then cdk deploy worked as expected. No bootstrapping necessary.

Wideman answered 2/11, 2022 at 22:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.