lambda-nodejs: NodeJS lambdas bundle multiple times if resource lookups are required
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the bug
When a CDK project depends on resource lookups to synthesize the stacks (i.e. an SSM parameter value), it runs synthesis multiple times, which results in NodejsFunction executing esbuild each time. For lambda-intensive projects, this doubles the synthesis time during deploy and can introduce unnecessary delays.
### Regression Issue
- [ ] Select this option if this issue appears to be a regression.
### Last Known Working CDK Library Version
_No response_
### Expected Behavior
Lambda NodeJS code should only be bundled once during a given cdk execution since the file asset is not impacted by stack configuration.
### Current Behavior
Observed behavior (with `cdk synth --verbose`):
* Synthesis runs once, and the lambda code is bundled as an asset with esbuild
* CDK reports "Some context information is missing. Fetching...", and fetches AWS lookup values for context
* Synthesis runs again, and the lambda code is bundled a second time
### Reproduction Steps
Steps to reproduce:
* Create a stack that looks up an SSM parameter (i.e. `StringParameter.valueFromLookup(scope, '/test')`)
* Create a NodejsFunction lambda resource
* Run `cdk synth --verbose` to see behavior
Simple CDK app for reproduction:
```
import { App, Stack } from 'aws-cdk-lib';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
class TestStack extends Stack {
constructor(scope, id, props) {
super(scope, id, props);
const paramValue = StringParameter.valueFromLookup(this, '/test');
const testLambda = new NodejsFunction(this, 'TestFunction', {
functionName: 'TestFunction',
entry: `src/test.ts`,
handler: 'index.handler'
});
}
}
const app = new App();
new TestStack(app, 'test', {
env: {
account: '123456789',
region: 'us-east-1'
}
});
```
### Possible Solution
Potential solutions:
* Do not rebuild assets within the same CLI task if the resource lookup doesn't result in parameter changes that would impact asset output.
* Pre-process resources without synth and lookup required values immediately instead of running synthesis twice
### Additional Information/Context
_No response_
### AWS CDK Library version (aws-cdk-lib)
2.212.0
### AWS CDK CLI version
2.1023.0
### Node.js Version
22
### OS
OSX
### Language
TypeScript
### Language Version
5.9.2
### Other information
_No response_
Contributor guide
Research direction
Start with the reproduction using StringParameter.valueFromLookup, NodejsFunction, and src/test.ts, then run cdk synth --verbose to observe the repeated synthesis and esbuild bundling. Trace why the asset is rebuilt after context lookup; done means the Lambda code is bundled only once during a single CDK execution without changing the synthesized result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nodejs, typescript
- Domain
- build-system, cloud, infrastructure
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100