Sub topic 11
Assets: bundling Lambda code and files
This chapter assumes you have defined a stack with a Lambda function, bootstrapped an environment, and run cdk deploy at least once. Those steps appear in earlier chapters.
What an asset is
An asset is a local file or directory that your stack needs at deploy time. A common example is the source code of a Lambda function. CloudFormation templates can only describe resources. They cannot carry your code. The CDK solves this by packaging the file, uploading it to the bootstrap bucket, and writing the bucket location into the template.
How assets move through the workflow
During cdk synth, the CDK zips a directory (or copies a file) into the cdk.out folder and computes a hash of the contents. During cdk deploy, it uploads the package to the S3 staging bucket created by bootstrapping. The template then refers to that bucket and key.
Because the key includes the content hash, unchanged code is not uploaded again. Changed code produces a new key, which makes CloudFormation update the function.
Using assets for Lambda code
The Code.fromAsset method (Code.from_asset in Python) takes a path relative to where you run the CDK app, usually the project root.
import * as lambda from 'aws-cdk-lib/aws-lambda';
const fn = new lambda.Function(this, 'Processor', {
runtime: lambda.Runtime.NODEJS_20_X,
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda'),
});fn = _lambda.Function(
self,
"Processor",
runtime=_lambda.Runtime.PYTHON_3_12,
handler="index.handler",
code=_lambda.Code.from_asset("lambda"),
)The lambda directory must contain a file matching the handler. For index.handler, that means an index.js or index.py with an exported handler function.
Other kinds of assets
Lambda code is the most common case, but the same mechanism applies elsewhere:
| Asset type | Typical use | Where it is stored |
|---|---|---|
| File or directory (`Code.fromAsset`, `s3-assets`) | Lambda code, scripts, config files | Bootstrap S3 bucket |
| Docker image (`DockerImageCode.fromImageAsset`) | Container-based Lambda or ECS tasks | Bootstrap ECR repository |
Common errors
- Wrong path. A path such as
lambdais resolved from the working directory, not from the stack file. If you runcdkfrom another folder, synthesis fails with a "Cannot find asset" error. - Missing bootstrap. If the environment was not bootstrapped, deployment fails because the staging bucket does not exist.
- Large or unwanted files. Everything in the directory is zipped, including
node_modulesor test data. Keep the asset directory small, or use theexcludeoption ofCode.fromAsset. - Dependencies not installed. The CDK zips what is in the folder. If your handler needs libraries, install them in that folder first, or use a bundling construct such as
NodejsFunctionorPythonFunction, which build the dependencies for you.
Exercise
You change one line in lambda/index.js and run cdk deploy. What does the CDK do with the asset, and why does CloudFormation update the function?
Show answer
The directory hash changes, so the CDK builds a new zip and uploads it to the bootstrap bucket under a new key. The template now points to the new key, so CloudFormation sees a changed property on the function and updates it.
Exercise
Your cdk synth fails because the lambda folder cannot be found, although the folder exists in the project. Name one likely cause and a fix.
Show answer
You probably ran the command from a different directory, because Code.fromAsset('lambda') resolves relative to the working directory. Run the command from the project root, or build the path with path.join(__dirname, ...).