TerraformAsset. You can exclude files from an asset,
fold extra inputs into its hash, and plug in a bundler that builds the source
before it’s staged
(cdk-terrain#371,
#433,
#442,
#464,
#472). Existing
assets that don’t use the new options behave as before.
The cdktn package ships the extension interface but no bundler. You write
your own IAssetBundler or use one from a separate package.
New TerraformAsset options
Bundlers
A bundler implementsIAssetBundler. Its bundle() method reads
options.source, writes into the scratch directory options.outputDir, and
returns a BundleResult:
BundleResult.directory(path): the output is a directory tree. CDKTN packs it according to the assettype, for example as a zip forAssetType.ARCHIVE.BundleResult.file(path): the output is a finished single file, such as a zip or a tarball. CDKTN stages it as is, so the assettypemust beAssetType.FILE. SetoutputFileNameon the bundler to name the staged file.BundleResult.declined(): the bundler can’t run here. Inside aChainBundler, the next bundler gets a turn.
ChainBundler.of(...) tries bundlers in order until one runs, for example a
local build first and a Docker build as the fallback. BundlerKey builds the
bundlerKey string from ordered parts. CDKTN folds the bundlerKey into the
asset hash, so a changed build changes the asset’s identity.
main.ts
SOURCE hashing, the build runs during synthesis and the
hash covers the source plus the bundlerKey. With AssetHashType.OUTPUT, the
build runs when you create the asset, and the hash covers what the bundler
produced. Use OUTPUT when the bundlers in a chain can produce different
output for the same source.
Ignore strategies
ExcludeIgnoreStrategy is the built-in matcher behind exclude. To match
differently, implement IIgnoreStrategy. Its ignores() method receives
each entry’s path and whether it’s a directory. To re-include a path under an
excluded directory, set pruneExcludedDirectories to false so that CDKTN
walks into excluded directories:
ignoreStrategy on TerraformAsset.
Lower-level building blocks
AssetStagingis the constructTerraformAssetuses to hash and stage content. It takes asourcePath, anIAssetPackaging, and the hashing, exclusion, and bundler options above.AssetPackaging.FILE,AssetPackaging.DIRECTORY, andAssetPackaging.ZIPare the built-in packagings.IAssetPackaginglets you add your own format.AssetHash.of()computes an asset’s content hash without staging it.- The
cdktn:assetHashSaltcontext key adds a salt to every asset hash in the app.
Breaking changes
IAssetPackaginghas two new required members,acceptsDirectorySourceandomitsDirectoryEntries. Custom implementations must add them.- A custom
assetHashmay only contain letters, digits,_,., and-. Values such asfeature/my-branchnow throw. TerraformAsset.assetHashis read-only. The setter is gone from the Java, C#, Python, and Go bindings.