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

(aws-lambda-nodejs): commandHooks is called twice when local bundling #31973

Open
1 task
ayame113 opened this issue Nov 1, 2024 · 1 comment · May be fixed by #32776
Open
1 task

(aws-lambda-nodejs): commandHooks is called twice when local bundling #31973

ayame113 opened this issue Nov 1, 2024 · 1 comment · May be fixed by #32776
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@ayame113
Copy link

ayame113 commented Nov 1, 2024

Describe the bug

It seems that the commandHook is being called twice when using local-bundling.

Regression Issue

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

Last Known Working CDK Version

No response

Expected Behavior

In my opinion, a commandHooks should only be executed once.

Current Behavior

commandHooks are executed twice (only in case of local bundling)

Reproduction Steps

    new NodejsFunction(this, functionName, {
      ...
      bundling: {
        ...
        commandHooks: {
          afterBundling(inputDir: string, outputDir: string) {
            console.log("afterBundling called!!", { inputDir, outputDir });
            return [];
          },
          beforeInstall() {
            return [];
          },
        },
      },
    });

When I run the above code, the console.log in afterBundling is printed twice:

afterBundling called!! { inputDir: '/asset-input', outputDir: '/asset-output' }
afterBundling called!! {
  inputDir: '.....',
  outputDir: '...../cdk.out/bundling-temp-c436e743f59b19e9baa436794ed62fc21b27f5aaf0bd7b10792cca6423e4dbe3'
}

This only seems to happen with local bundling, not docker bundling.

Is this expected behavior?

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.164.0 (build 75cf2e0)

Framework Version

No response

Node.js Version

v22.8.0

OS

Mac

Language

TypeScript

Language Version

5.6.3

Other information

No response

@ayame113 ayame113 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Nov 1, 2024
@pahud pahud self-assigned this Nov 1, 2024
@pahud
Copy link
Contributor

pahud commented Nov 1, 2024

Yes, it's reproducible

export class MyStack3 extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    // Define a NodeJS function with the afterBunding hook
    const myFunction = new lambdaNodejs.NodejsFunction(this, 'MyFunction', {
      entry: path.join(__dirname, '../bundling/function.ts'), // Path to the entry point of your function
      handler: 'handler', // Name of the exported handler function
      bundling: {
        // externalModules: ['aws-sdk'], // Modules to consider as external
        commandHooks: {
          beforeInstall(inputDir, outputDir) {
            // Perform any necessary setup or configuration before installing dependencies
            console.log('Running before installing dependencies...');
            return [];
          },
          beforeBundling(inputDir, outputDir) {
            // Perform any necessary setup or configuration before bundling
            console.log('Running before bundling...');
            return [];
          },
          afterBundling(inputDir, outputDir) {
            // Perform any necessary cleanup or configuration after bundling
            console.log('!!!!!!!!!Running after bundling...');
            return [];
          },
        },
        }
      })
  }
}
 % npx cdk diff
Running before bundling...
!!!!!!!!!Running after bundling...
Bundling asset dummy-stack/MyFunction/Code/Stage...
Running before bundling...
!!!!!!!!!Running after bundling...

We'll bring this up to the team for inputs.

@pahud pahud added p2 effort/medium Medium work item – several days of effort labels Nov 1, 2024
@pahud pahud removed their assignment Nov 1, 2024
@pahud pahud removed the needs-triage This issue or PR still needs to be triaged. label Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
2 participants