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] layer and function assets bundled through lambda.Code.fromAsset bundling option keep getting re-uploaded and new versions published despite nothing having changed

See original GitHub issue

I have a lambda function that uses two lambda layers to optimize for my use case (slow upload speed where I live).

It contains 3 assets:

  • layer asset for ‘other’ jars (the third party dependencies that get changed the least often)
const otherDepsLayer = new lambda.LayerVersion(this, 'OtherJars', {
    compatibleRuntimes: [props.runtime],
    code: lambda.Code.fromAsset(props.basePath, {
        bundling: {
            image: props.runtime.bundlingDockerImage,
            volumes: [m2Volume],
            command: ['bash', '-c', otherDepsCommand],
        },
    }),
});
  • layer asset for ‘our’ jars (the other modules in a maven reactor project that may have a higher rate of change)
const ourDepsLayer = new lambda.LayerVersion(this, 'OurJars', {
    compatibleRuntimes: [props.runtime],
    code: lambda.Code.fromAsset(props.basePath, {
        bundling: {
            image: props.runtime.bundlingDockerImage,
            volumes: [m2Volume],
            command: ['bash', '-c', ourDepsCommand],
        },
    }),
});
  • lambda function classes asset (the module with the highest rate of change when I develop it)
this.lambdaFunction = new lambda.Function(this, "Function", {
    ...props,
    layers: [otherDepsLayer, ourDepsLayer],
    code: lambda.Code.fromAsset(props.basePath, {
        bundling: {
            image: props.runtime.bundlingDockerImage,
            volumes: [m2Volume],
            command: ['bash', '-c', commandToBundleClasses],
        },
    }),
})

When I run cdk deploy twice in a row (within modifying anything in between), I expect:

  • the ‘other’ jars asset not be uploaded, no new layer version to be created
  • the ‘our’ jars asset not be uploaded, no new layer version to be created
  • the lambda function classes assets not to be uploaded, no new lambda function to be created
  • no new asset.* directory to be created in cdk.out

What happens

  • the ‘other’ jars asset is uploaded again (it says start: Publishing then success: Published)
  • a new ‘other’ jars layer version is created
  • the ‘our’ jars asset is uploaded again (it says start: Publishing then success: Published)
  • a new ‘our’ jars layer version is created
  • the lambda function classes assets is uploaded again (it says start: Publishing then success: Published)
  • a new lambda function version is created

I was going to spend time making sure ‘our’ jars are created in a reproducible manner to ensure the checksums are always the same. But that will be for nought if there is no intelligence in the CDK for asset content deduplication.

This is already how SAM CLI works, so I’d expect CDK to have the same behaviour.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lestephanecommented, Sep 18, 2020

Sorry for the delay. Adding AssetHashType.Bundle helps. But it feels counter intuitive to have to specify this when someone is using bundling right next to it. Shouldn’t the very presence of the bundling property swich the AssetHashType to Bundle by default?

code: lambda.Code.fromAsset(props.basePath, {
    assetHashType: AssetHashType.BUNDLE,                // <<<<<<<<<< this...
    bundling: {                                         // <<<<<<<<<< ..could be derived from this 
        image: props.runtime.bundlingDockerImage,
        volumes: [m2Volume],
        command: ['bash', '-c', otherDepsCommand],
    },
}),
0reactions
lestephanecommented, Sep 22, 2020

I guess I don’t understand the difference between SOURCE and BUNDLE yet (or I kinda do, but I think the way it’s explained is insufficient and / or misleading).

The API documentation for AssetHashType should really explain what’s going on, and why one would want to use one vs the other. The optimal answer will vary based on the runtime for example, so that even choosing a default for the user is fraught for risks.

Documentation:

/**
 * The type of asset hash
 */
export declare enum AssetHashType {
    /**
     * Based on the content of the source path
     */
    SOURCE = "source",
    /**
     * Based on the content of the bundled path
     */
    BUNDLE = "bundle",
Read more comments on GitHub >

github_iconTop Results From Across the Web

aws-cdk/aws-lambda module - AWS Documentation
To produce a new lambda version each time the lambda function is modified, the currentVersion property under the hood, computes a new logical...
Read more >
Creating Lambda Layers with TypeScript and CDK
First we're setting up the Lambda Layer, and using Code.fromAsset to grab the code we made for the layer. Next we're setting up...
Read more >
How to install external modules in a Python Lambda Function ...
new Function (this, 'Function', { code: Code.fromAsset(path.join(__dirname, 'my-python-handler'), { bundling: { ...
Read more >
5 Ways To Bundle a Lambda Function Within an AWS CDK ...
Have you ever tried to publish a CDK construct that was using a Lambda function, for example to create a custom resource or...
Read more >
How to Create AWS CDK Lambda Functions? 4 Easy Steps
This article provides you with a stepwise guide to easily create and deploy your AWS CDK Lambda function.
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