(aws-lambda / assets): Ability to use token in Code.fromAsset, or stage lambda code lazily
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the feature
Provide lazy strings or inputs to Code.fromAsset:
```ts
import { Lazy, Stack, StackProps } from "aws-cdk-lib";
import * as lambda from "aws-cdk-lib/aws-lambda";
import { Construct } from "constructs";
import { join } from "path";
export class CdkTestStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
let path = Lazy.string({ produce: () => join(__dirname, "lmb") });
new lambda.Function(this, "CdkTestFunction1", {
code: lambda.Code.fromAsset(path),
handler: "hello.handler",
runtime: lambda.Runtime.NODEJS_18_X,
});
}
}
```
### Use Case
In some situations, I might not have all of the lambda code available for my CDK app available before I run `cdk synth` or `cdk deploy`. For example, in Winglang (language that uses CDK constructs under the hood) we're trying to generate some lambda code dynamically based on the graph of resources in the construct tree. In this case, I won't know the exact contents of my lambda code until the entire tree is constructed (but we can assume the contents will be known before `app.synth()` is called).
### Proposed Solution
_No response_
### Other Information
I tried extending the abstract `Code` class and implementing the `bind()` method, but looking at the existing implementation of `AssetCode` [here](https://github.com/aws/aws-cdk/blob/109b2abe4c713624e731afa1b82c3c1a3ba064c9/packages/aws-cdk-lib/aws-lambda/lib/code.ts), I couldn't figure out a way to instantiate `s3_assets.Asset` lazily. Even if I use the `bundling` option from the assets module described [here](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_s3_assets-readme.html#asset-bundling), it seems like the bundling command gets invoked while the tree is being created (in other words, in the middle of the evaluation of `new MyCdkStack()`), not afterwards.
### Acknowledgements
- [ ] I may be able to implement this feature request
- [ ] This feature might incur a breaking change
### CDK version used
2.92.0
### Environment details (OS name and version, etc.)
macOS
Contributor guide
Research direction
Read packages/aws-cdk-lib/aws-lambda/lib/code.ts and trace Code.fromAsset and AssetCode first; compare this path with the assets bundling behavior described in the issue. Done means a supported lazy input can defer Lambda asset staging until synth or deploy while preserving existing fromAsset behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100