Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli: Uploaded file must be a non-empty zip #32869

Open
1 task
mostafasany opened this issue Jan 13, 2025 · 5 comments
Open
1 task

Cli: Uploaded file must be a non-empty zip #32869

mostafasany opened this issue Jan 13, 2025 · 5 comments
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort p2 package/tools Related to AWS CDK Tools or CLI

Comments

@mostafasany
Copy link

Describe the bug

Title: CDK Deploy Creates Empty Bootstrap File: "Uploaded file must be a non-empty zip" Error


Description:

When running cdk deploy, the deployment process fails with the following error:

Error: Uploaded file must be a non-empty zip (Service: Lambda, Status Code: 400)

This issue occurs due to the creation of an empty or invalid bootstrap file in the cdk.out directory.


Environment:

  • CDK Version: 2.175.1
  • Node.js Version: 18
  • OS: macOS
  • Additional Details:
    • Manually clearing the cdk.out folder does not resolve the issue.
    • File permissions and ownership of the cdk.out directory appear correct.
    • Running cdk bootstrap before deployment does not prevent the error.

Logs and Debug Information:
Relevant logs from cdk deploy --verbose:

Error: Uploaded file must be a non-empty zip
Screenshot 2025-01-13 at 01 04 39

Here is package.json of the CDK project written in TS

{
  "name": "cdk",
  "version": "0.1.0",
  "bin": {
    "cdk": "bin/cdk.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "cdk": "cdk",
    "cdk:deploy:default": "cdk deploy --profile default",
    "cdk:deploy:dev": "ENV=dev cdk deploy",
    "cdk:synth:dev": "ENV=dev cdk synth",
    "cdk:deploy:stage": "ENV=stage cdk deploy",
    "cdk:synth:stage": "ENV=stage cdk synth",
    "cdk:deploy:prod": "ENV=prod cdk deploy",
    "cdk:synth:prod": "ENV=prod cdk synth"

  },
  "devDependencies": {
    "@types/jest": "^29.5.14",
    "@types/node": "22.10.5",
    "aws-cdk": "^2.175.1",
    "jest": "^29.7.0",
    "ts-jest": "^29.2.5",
    "ts-node": "^10.9.2",
    "typescript": "~5.7.3"
  },
  "dependencies": {
    "aws-cdk-lib": "^2.175.1",
    "cdk-ecr-deployment": "^3.1.4",
    "constructs": "^10.4.2",
    "source-map-support": "^0.5.21"
  }
}

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

Expected Behavior:

The deployment should proceed without errors, and all generated files, including the bootstrap file, should be valid and non-empty.


Current Behavior

Observed Behavior:

The cdk.out directory contains an empty or invalid file for the relevant custom resource, leading to the error:

Error: Uploaded file must be a non-empty zip (Service: Lambda, Status Code: 400)

Reproduction Steps

  1. Create a CDK stack containing resources like AWS::Lambda::Function or AWS::S3::Bucket.
  2. Run:
    npm install
    cdk synth
    cdk deploy
  3. Observe the error during deployment.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.175.1

Framework Version

No response

Node.js Version

18

OS

macOS

Language

TypeScript

Language Version

No response

Other information

No response

@mostafasany mostafasany added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 13, 2025
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Jan 13, 2025
@pahud
Copy link
Contributor

pahud commented Jan 13, 2025

Create a CDK stack containing resources like AWS::Lambda::Function or AWS::S3::Bucket.

Are you able to provide a self-containing minimal reproducible code snippets that I can paste into my IDE and see what happens there?

@pahud pahud added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p3 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jan 13, 2025
@jarrodallan
Copy link

@pahud I am also facing this issue when using particularly on the Custom Resource Lambda for ECR Deployments when using the ecr_assets.DockerImageAsset to build my docker images in my CDK stack. Below is an example of the code I am using and when I run the synth I also see a zero byte bootstrap file in the cdk.out directory - CDK version 2.175.1 (build afe6e87).

When the stack tries to deploy I receive the error -
Custom::CDKECRDeployment... (CustomCDKECRDeployment...) Resource handler returned message: "Uploaded file must be a non-empty zip (Service: Lambda, Status Code: 400 ...

When I synth the stack using cdk version 2.160.0 (build 7a8ae02) the bootstrap file is non-zero as expected but I haven't been able to test a deploy with the lower version yet.

import time
from os import environ, listdir
from os.path import basename, dirname, isfile, join

import __main__
import cdk_ecr_deployment as imagedeploy
from aws_cdk import Duration, RemovalPolicy, Stack
from aws_cdk import aws_ecr as ecr
from aws_cdk import aws_ecr_assets as ecr_assets
from aws_cdk import aws_iam as iam
from constructs import Construct


def strip_string_suffix(string, suffix):
    return string[0 : -len(suffix)]


class DemoDockerImagesStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # Create repository
        repository = ecr.Repository(
            self,
            "Repository",
            removal_policy=RemovalPolicy.DESTROY,
            repository_name="demo",
        )
        # Add repository lifecycle rule
        repository.add_lifecycle_rule(
            max_image_age=Duration.days(1),
            tag_status=ecr.TagStatus.UNTAGGED,
        )

        # List files in the containers directory that end with `.Dockerfile`
        suffix = ".Dockerfile"
        path = dirname(__main__.__file__) + "/containers"
        dockerfiles = [
            f for f in listdir(path) if isfile(f"{path}/{f}") and f.endswith(suffix)
        ]
        for dockerfile in dockerfiles:
            tag = strip_string_suffix(dockerfile, suffix)
            self.deploy_docker_image(repository, tag, f"{path}/{dockerfile}")

    def deploy_docker_image(self, repository, tag, file):
        image = ecr_assets.DockerImageAsset(
            self,
            f"{tag}Image",
            directory=dirname(file),
            file=basename(file),
            extra_hash=str(time.time()),
        )
        deployment = imagedeploy.ECRDeployment(
            self,
            f"{tag}Deployment",
            dest=imagedeploy.DockerImageName(repository.repository_uri_for_tag(tag)),
            src=imagedeploy.DockerImageName(image.image_uri),
        )

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jan 15, 2025
@mostafasany
Copy link
Author

@jarrodallan

Comment:

Can you try using these versions? When I downgraded aws-cdk-lib to 2.143.1, it worked. However, I occasionally observed that the Docker image gets uploaded to ECR with zero bytes.

"devDependencies": {
  "aws-cdk": "^2.173.4"
},
"dependencies": {
  "aws-cdk-lib": "^2.143.1",
  "cdk-ecr-deployment": "^3.0.23",
  "constructs": "^10.4.2"
}

To overcome the Zero Bytes docker image i do to things

  • Clean CDK.out folder
  • Delete the empty image from ECR

@jarrodallan
Copy link

@mostafasany - Thank you those versions did get my builds working again for the moment. Much appreciated.

Curious to learn what in the later version is causing this bug.

@pahud
Copy link
Contributor

pahud commented Jan 30, 2025

related #33201
I'm bringing it up to the team for further input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort p2 package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

No branches or pull requests

3 participants