aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.61k stars 3.9k forks source link

aws-lambda-python-alpha: Bundling - AssetHashType.OUTPUT failing cdk destroy #26345

Open edwardgale opened 1 year ago

edwardgale commented 1 year ago

Describe the bug

RuntimeError: Cannot use output hash type when bundling is not specified.

cdk destroy and cdk destroy --no-staging both failing because we have the following bundling options for our lambda:

lambda_alpha.BundlingOptions(
            asset_excludes=exclusions,
            asset_hash_type=AssetHashType.OUTPUT,
            volumes=[
                DockerVolume(
                    host_path=lib_dir,
                    container_path="/asset-input/lib",
                )
            ],
        )

We create the hash on the Output because our exclusions list is large and some files in the exclusions list are dynamic leading to changes in hash and unnecessary redeployments.

Expected Behavior

cdk destroy and cdk destroy --no-staging should destroy the stack. Creating a hash type of OUTPUT has no relevance here.

Current Behavior

The following stack trace was emitted:

jsii.errors.JavaScriptError: 
  Error: Cannot use `output` hash type when `bundling` is not specified.
      at AssetStaging.calculateHash (/private/var/folders/88/x7htgmyn7cz9ldg_w22npf_m0000gn/T/jsii-kernel-MJTiCj/node_modules/aws-cdk-lib/core/lib/asset-staging.js:2:1509)
      at AssetStaging.stageByCopying (/private/var/folders/88/x7htgmyn7cz9ldg_w22npf_m0000gn/T/jsii-kernel-MJTiCj/node_modules/aws-cdk-lib/core/lib/asset-staging.js:1:3148)
      at stageThisAsset (/private/var/folders/88/x7htgmyn7cz9ldg_w22npf_m0000gn/T/jsii-kernel-MJTiCj/node_modules/aws-cdk-lib/core/lib/asset-staging.js:1:2065)
      at Cache.obtain (/private/var/folders/88/x7htgmyn7cz9ldg_w22npf_m0000gn/T/jsii-kernel-MJTiCj/node_modules/aws-cdk-lib/core/lib/private/cache.js:1:242)
      at new AssetStaging (/private/var/folders/88/x7htgmyn7cz9ldg_w22npf_m0000gn/T/jsii-kernel-MJTiCj/node_modules/aws-cdk-lib/core/lib/asset-staging.js:1:2400)
      at new Asset (/private/var/folders/88/x7htgmyn7cz9ldg_w22npf_m0000gn/T/jsii-kernel-MJTiCj/node_modules/aws-cdk-lib/aws-s3-assets/lib/asset.js:1:736)
      at AssetCode.bind (/private/var/folders/88/x7htgmyn7cz9ldg_w22npf_m0000gn/T/jsii-kernel-MJTiCj/node_modules/aws-cdk-lib/aws-lambda/lib/code.js:1:4628)
      at new Function (/private/var/folders/88/x7htgmyn7cz9ldg_w22npf_m0000gn/T/jsii-kernel-MJTiCj/node_modules/aws-cdk-lib/aws-lambda/lib/function.js:1:7479)
      at new PythonFunction (/private/var/folders/88/x7htgmyn7cz9ldg_w22npf_m0000gn/T/jsii-kernel-MJTiCj/node_modules/@aws-cdk/aws-lambda-python-alpha/lib/function.js:40:9)
      at Kernel._create (/private/var/folders/88/x7htgmyn7cz9ldg_w22npf_m0000gn/T/tmpc8zaccia/lib/program.js:9964:29)

Reproduction Steps

Create a python lambda function with the following options:

        lambda_alpha.PythonFunction(
            self,
            "MyFunc",
            entry="/path/to/src",
            index="app.py",
            handler="lambda_handler",
            bundling=lambda_alpha.BundlingOptions(
                asset_hash_type=AssetHashType.OUTPUT,
            ),
            runtime = lambda_.Runtime.PYTHON_3_10
        )

deploy and run cdk destroy --no-staging

Possible Solution

Logic contained within https://github.com/aws/aws-cdk/blob/f15ed231e7728623df9ca3943cdfc2c9feb06e9a/packages/aws-cdk-lib/core/lib/asset-staging.ts#L501

Additional Information/Context

No response

CDK CLI Version

2.85.0 (build 4e0d726)

Framework Version

No response

Node.js Version

v18.16.0

OS

MacOs 13.4.1 (c)

Language

Python

Language Version

No response

Other information

No response

pahud commented 1 year ago

RuntimeError: Cannot use output hash type when bundling is not specified.

I didn't get it. According your repro steps, you actually provided the bundling prop, didn't you?

lambda_alpha.PythonFunction(
            self,
            "MyFunc",
            entry="/path/to/src",
            index="app.py",
            handler="lambda_handler",
            bundling=lambda_alpha.BundlingOptions(
                asset_hash_type=AssetHashType.OUTPUT,
            ),
            runtime = lambda_.Runtime.PYTHON_3_10
        )
github-actions[bot] commented 1 year ago

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

edwardgale commented 1 year ago

Bundling prop was provided. However asset_hash_type=AssetHashType.OUTPUT still fails a cdk destroy

Very basic repo to reproduce the issue here:

https://github.com/edwardgale/cdk-destroy-issue

mikey- commented 1 year ago

Seeing this with node functions on deploy as well btw. lmk if more info is required or you'd prefer it in another issue <3 ty

sercantor commented 8 months ago

Still getting this