AssetStaging
Stages a file or directory from a location on the file system into a staging directory.
This construct can operate in two modes: without and with "bundling". Bundling means that an additional command is run to transform the source files into a final asset that will be used for deployment.
Without bundling
Without the bundling property, the asset is just a regular file copy operation.
excludeandignoreModecontrol which files are excluded from the copy operation.followcontrols how symlinks are handled during the copy operation: the symlink can be copied, the file the symlink points to can be copied, or symlinks pointing outside the source tree can be blocked.- The asset hash is calculated based on the files that are copied into the asset, unless the hash is overridden entirely by the
assetHashproperty.
The copy-staging operation can be disabled by setting the context key 'aws:cdk:asset-staging': false. This can be useful for local development tools like SAM CLI, so that you can update the source files directly and immediately see the result in a local emulator without needing to run cdk synth every time to stage the assets. Note that in this mode, since there is no copying, exclude and follow settings have no effect, including BLOCK_EXTERNAL which will not be applied.
With bundling
With the bundling property, the transformation of source files to a final asset is done through bundling operation specified in the bundling property.
The behavior of the construct depends on the assetHashType selected:
AssetHashType.SOURCE: the asset hash will be based on the source files after applying theexclude,ignoreModeandfollowsettings. That hash will be used to skip the bundling command entirely if an asset with the given hash already exists in the assembly directory. This requires that those settings accurately model the dependencies used by the bundling operation to produce the final asset.AssetHashType.OUTPUT: the asset hash will be based on the output of the bundling operation.exclude/ignoreModeare not used, and no attempt is made to materialize any symlinks that bundling may have written.AssetHashType.CUSTOM: the asset hash will be taken from theassetHashproperty.exclude/ignoreModeare not used, and no attempt is made to materialize any symlinks that bundling may have written.
For all three modes, if follow is set to SymlinkFollowMode.BLOCK_EXTERNAL, any symlinks written by bundling that point outside the output directory will be blocked.
For bundled assets, staging can not be disabled for the benefit of local development. If you are iterating against a local emulator, you will need to run cdk synth for every asset change.
Other behavior
The stagedPath property is a stringified token that represents the location of the file or directory after staging. It will be resolved only during the "prepare" stage and may be either the original path or the staged path depending on the context setting.
The file/directory are staged based on their content hash (fingerprint). This means that only if content was changed, copy will happen.
Security notes on BLOCK_EXTERNAL symlinks
If a bundled asset is being reused from the cdk.out cache, the BLOCK_EXTERNAL symlink check is not re-run, and on an asset hash collission (made more likely in the case of a chosen CUSTOM hash) external symlinks left over from a previous unchecked synth can persist. This is unlikely to be a problem in practice, but to prevent this from happening to you, delete the cdk.out directory whenever you switch from non-BLOCK_EXTERNAL to BLOCK_EXTERNAL follow mode.
| Source | core/lib/asset-staging.ts |
|---|
Example
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import * as cdk from 'aws-cdk-lib';
declare const dockerImage: cdk.DockerImage;
declare const localBundling: cdk.ILocalBundling;
const assetStaging = new cdk.AssetStaging(this, 'MyAssetStaging', {
sourcePath: 'sourcePath',
// the properties below are optional
assetHash: 'assetHash',
assetHashType: cdk.AssetHashType.SOURCE,
bundling: {
image: dockerImage,
// the properties below are optional
bundlingFileAccess: cdk.BundlingFileAccess.VOLUME_COPY,
command: ['command'],
entrypoint: ['entrypoint'],
environment: {
environmentKey: 'environment',
},
local: localBundling,
network: 'network',
outputType: cdk.BundlingOutput.ARCHIVED,
platform: 'platform',
securityOpt: 'securityOpt',
user: 'user',
volumes: [{
containerPath: 'containerPath',
hostPath: 'hostPath',
// the properties below are optional
consistency: cdk.DockerVolumeConsistency.CONSISTENT,
}],
volumesFrom: ['volumesFrom'],
workingDirectory: 'workingDirectory',
},
exclude: ['exclude'],
extraHash: 'extraHash',
follow: cdk.SymlinkFollowMode.NEVER,
ignoreMode: cdk.IgnoreMode.GLOB,
});
# The code below shows an example of how to instantiate this type.
# The values are placeholders you should change.
import aws_cdk as cdk
# docker_image: cdk.DockerImage
# local_bundling: cdk.ILocalBundling
asset_staging = cdk.AssetStaging(self, "MyAssetStaging",
source_path="sourcePath",
# the properties below are optional
asset_hash="assetHash",
asset_hash_type=cdk.AssetHashType.SOURCE,
bundling=cdk.BundlingOptions(
image=docker_image,
# the properties below are optional
bundling_file_access=cdk.BundlingFileAccess.VOLUME_COPY,
command=["command"],
entrypoint=["entrypoint"],
environment={
"environment_key": "environment"
},
local=local_bundling,
network="network",
output_type=cdk.BundlingOutput.ARCHIVED,
platform="platform",
security_opt="securityOpt",
user="user",
volumes=[cdk.DockerVolume(
container_path="containerPath",
host_path="hostPath",
# the properties below are optional
consistency=cdk.DockerVolumeConsistency.CONSISTENT
)],
volumes_from=["volumesFrom"],
working_directory="workingDirectory"
),
exclude=["exclude"],
extra_hash="extraHash",
follow=cdk.SymlinkFollowMode.NEVER,
ignore_mode=cdk.IgnoreMode.GLOB
)
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import software.amazon.awscdk.*;
DockerImage dockerImage;
ILocalBundling localBundling;
AssetStaging assetStaging = AssetStaging.Builder.create(this, "MyAssetStaging")
.sourcePath("sourcePath")
// the properties below are optional
.assetHash("assetHash")
.assetHashType(AssetHashType.SOURCE)
.bundling(BundlingOptions.builder()
.image(dockerImage)
// the properties below are optional
.bundlingFileAccess(BundlingFileAccess.VOLUME_COPY)
.command(List.of("command"))
.entrypoint(List.of("entrypoint"))
.environment(Map.of(
"environmentKey", "environment"))
.local(localBundling)
.network("network")
.outputType(BundlingOutput.ARCHIVED)
.platform("platform")
.securityOpt("securityOpt")
.user("user")
.volumes(List.of(DockerVolume.builder()
.containerPath("containerPath")
.hostPath("hostPath")
// the properties below are optional
.consistency(DockerVolumeConsistency.CONSISTENT)
.build()))
.volumesFrom(List.of("volumesFrom"))
.workingDirectory("workingDirectory")
.build())
.exclude(List.of("exclude"))
.extraHash("extraHash")
.follow(SymlinkFollowMode.NEVER)
.ignoreMode(IgnoreMode.GLOB)
.build();
// Install: dotnet add package Amazon.CDK.Lib
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
using Amazon.CDK;
DockerImage dockerImage;
ILocalBundling localBundling;
var assetStaging = new AssetStaging(this, "MyAssetStaging", new AssetStagingProps {
SourcePath = "sourcePath",
// the properties below are optional
AssetHash = "assetHash",
AssetHashType = AssetHashType.SOURCE,
Bundling = new BundlingOptions {
Image = dockerImage,
// the properties below are optional
BundlingFileAccess = BundlingFileAccess.VOLUME_COPY,
Command = new [] { "command" },
Entrypoint = new [] { "entrypoint" },
Environment = new Dictionary<string, string> {
{ "environmentKey", "environment" }
},
Local = localBundling,
Network = "network",
OutputType = BundlingOutput.ARCHIVED,
Platform = "platform",
SecurityOpt = "securityOpt",
User = "user",
Volumes = new [] { new DockerVolume {
ContainerPath = "containerPath",
HostPath = "hostPath",
// the properties below are optional
Consistency = DockerVolumeConsistency.CONSISTENT
} },
VolumesFrom = new [] { "volumesFrom" },
WorkingDirectory = "workingDirectory"
},
Exclude = new [] { "exclude" },
ExtraHash = "extraHash",
Follow = SymlinkFollowMode.NEVER,
IgnoreMode = IgnoreMode.GLOB
});
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// The code below shows an example of how to instantiate this type.
// The values are placeholders you should change.
import "github.com/aws/aws-cdk-go/awscdk"
var dockerImage DockerImage
var localBundling ILocalBundling
assetStaging := cdk.NewAssetStaging(this, jsii.String("MyAssetStaging"), &AssetStagingProps{
SourcePath: jsii.String("sourcePath"),
// the properties below are optional
AssetHash: jsii.String("assetHash"),
AssetHashType: cdk.AssetHashType_SOURCE,
Bundling: &BundlingOptions{
Image: dockerImage,
// the properties below are optional
BundlingFileAccess: cdk.BundlingFileAccess_VOLUME_COPY,
Command: []*string{
jsii.String("command"),
},
Entrypoint: []*string{
jsii.String("entrypoint"),
},
Environment: map[string]*string{
"environmentKey": jsii.String("environment"),
},
Local: localBundling,
Network: jsii.String("network"),
OutputType: cdk.BundlingOutput_ARCHIVED,
Platform: jsii.String("platform"),
SecurityOpt: jsii.String("securityOpt"),
User: jsii.String("user"),
Volumes: []DockerVolume{
&DockerVolume{
ContainerPath: jsii.String("containerPath"),
HostPath: jsii.String("hostPath"),
// the properties below are optional
Consistency: cdk.DockerVolumeConsistency_CONSISTENT,
},
},
VolumesFrom: []*string{
jsii.String("volumesFrom"),
},
WorkingDirectory: jsii.String("workingDirectory"),
},
Exclude: []*string{
jsii.String("exclude"),
},
ExtraHash: jsii.String("extraHash"),
Follow: cdk.SymlinkFollowMode_NEVER,
IgnoreMode: cdk.IgnoreMode_GLOB,
})
Initializer
new AssetStaging(scope: Construct, id: string, props: AssetStagingProps)
AssetStaging(scope: Construct, id: str, props: AssetStagingProps)
new AssetStaging(Construct scope, String id, AssetStagingProps props)
new AssetStaging(Construct scope, string id, AssetStagingProps props)
NewAssetStaging(Scope Construct, Id *string, Props AssetStagingProps)
Construct Props
assetHashoptionalstringasset_hashoptionalstrassetHashoptionalStringAssetHashoptionalstringAssetHashoptional*string #Default: based on assetHashType
notes
If assetHashType is set it must be set to AssetHashType.CUSTOM. For consistency, this custom hash will be SHA256 hashed and encoded as hex. The resulting hash will be the asset hash.
NOTE: the hash is used in order to identify a specific revision of the asset, and used for optimizing and caching deployment activities related to this asset such as packaging, uploading to Amazon S3, etc. If you chose to customize the hash, you will need to make sure it is updated every time the asset changes, or otherwise it is possible that some deployments will not be invalidated.
assetHashTypeoptionalAssetHashTypeasset_hash_typeoptionalAssetHashTypeassetHashTypeoptionalAssetHashTypeAssetHashTypeoptionalAssetHashTypeAssetHashTypeoptionalAssetHashType #Default: the default is AssetHashType.SOURCE, but if assetHash is explicitly specified this value defaults to AssetHashType.CUSTOM.
If assetHash is configured, this option must be undefined or AssetHashType.CUSTOM.
bundlingoptionalBundlingOptionsbundlingoptionalBundlingOptionsbundlingoptionalBundlingOptionsBundlingoptionalBundlingOptionsBundlingoptionalBundlingOptions #Default: uploaded as-is to S3 if the asset is a regular file or a .zip file, archived into a .zip file and uploaded to S3 otherwise
The asset path will be mounted at /asset-input. The Docker container is responsible for putting content at /asset-output. The content at /asset-output will be zipped and used as the final asset.
excludeoptionalstring[]excludeoptionaltyping.List[str]excludeoptionalList<String>Excludeoptionalstring[]Excludeoptional*[]*string #Default: nothing is excluded
notes
Without bundling
When used without the bundling property, the asset is just a regular file copy operation. In that case, this controls the files that will be included in the copy. Files matching the exclude patterns will not be included in the copy.
With bundling
When used with the bundling property, the asset is produced by a bundling operation. In that case, the value of exclude is only meaningful if the assetHashType is set to AssetHashType.SOURCE, and ignored for other asset hash types. The exclude patterns will be used to determine which files contribute to the asset, and the bundling command is skipped if the source files have not changed since the last bundling operation.
NOTE: The bundling operation still has access to all files in the source path, regardless of the exclude patterns. It is your responsibility to ensure the exclude pattern accurately matches the files the bundling operation will actually access and base its final output on.
For OUTPUT or CUSTOM asset hash types, the exclude pattern is ignored.
extraHashoptionalstringextra_hashoptionalstrextraHashoptionalStringExtraHashoptionalstringExtraHashoptional*string #Default: hash is only based on the file fingerprint
followoptionalSymlinkFollowModefollowoptionalSymlinkFollowModefollowoptionalSymlinkFollowModeFollowoptionalSymlinkFollowModeFollowoptionalSymlinkFollowMode = SymlinkFollowMode.EXTERNAL #notes
Symlinks may be copied, materialized or rejected. Note that if symlinks are copied they should be relative; absolute symlinks will lose their meaning if the cloud assembly is moved to another machine.
Without bundling
When used without the bundling property, the asset is just a regular file copy operation. In that case, symlinks are materialized or blocked with respect to the source path root as they are being copied.
With bundling
When used with the bundling property, the asset is produced by a bundling operation. In that case, the bundling command is fully in charge of producing a set of files in an output directory, and the AssetStaging construct will never affect symlink materialization.
If the assetHashType is AssetHashType.SOURCE, this flag affects what the source hash is based on, and the recommended value is one of SymlinkFollowMode.EXTERNAL or SymlinkFollowMode.BLOCK_EXTERNAL.
If the SymlinkFollowMode.BLOCK_EXTERNAL mode is used:
- Symlinks pointing outside the source tree will be blocked, but only if using
AssetHashType.SOURCE(not for any of the other asset hash types). - Symlinks pointing outside the bundle output will be blocked.
ignoreModeoptionalIgnoreModeignore_modeoptionalIgnoreModeignoreModeoptionalIgnoreModeIgnoreModeoptionalIgnoreModeIgnoreModeoptionalIgnoreMode #Default: IgnoreMode.GLOB for file assets, IgnoreMode.DOCKER for image assets (requires that the @aws-cdk/aws-ecr-assets:dockerIgnoreSupport feature flag is set, which is recommended)
Properties
absoluteStagedPathstringabsolute_staged_pathstrabsoluteStagedPathStringAbsoluteStagedPathstringAbsoluteStagedPath*string #notes
If asset staging is disabled, this will just be the source path or a temporary directory used for bundling.
If asset staging is enabled it will be the staged path.
IMPORTANT: If you are going to call addFileAsset(), use relativeStagedPath() instead.
BUNDLING_INPUT_DIRstringBUNDLING_INPUT_DIRstrBUNDLING_INPUT_DIRStringBUNDLING_INPUT_DIRstringBUNDLING_INPUT_DIR*string static #BUNDLING_OUTPUT_DIRstringBUNDLING_OUTPUT_DIRstrBUNDLING_OUTPUT_DIRStringBUNDLING_OUTPUT_DIRstringBUNDLING_OUTPUT_DIR*string static #It will end up as an archive either if it's a directory (which the CDK will ZIP) or if it's a file that's an archive.
packagingFileAssetPackagingpackagingFileAssetPackagingpackagingFileAssetPackagingPackagingFileAssetPackagingPackagingFileAssetPackaging #Methods
clearAssetHashCache(): void@staticmethod def clear_asset_hash_cache() -> Nonepublic static void clearAssetHashCache()public static void ClearAssetHashCache()func ClearAssetHashCache() static #isConstruct(x: any): boolean@staticmethod def is_construct(x: Any) -> boolpublic static Boolean isConstruct(Object x)public static bool IsConstruct(object x)func IsConstruct(X interface{}) *bool static #notes · 1 parameter · returns
Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.
Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.
Parameters
xxxxX— Any object.
Returns: true if x is an object created from a class which extends Construct.
relativeStagedPath(stack: Stack): stringdef relative_staged_path(self, stack: Stack) -> strpublic String relativeStagedPath(Stack stack)public string RelativeStagedPath(Stack stack)func RelativeStagedPath(Stack Stack) *string #notes
Only returns a relative path if the asset was staged, returns an absolute path if it was not staged.
A bundled asset might end up in the outDir and still not count as "staged"; if asset staging is disabled we're technically expected to reference source directories, but we don't have a source directory for the bundled outputs (as the bundle output is written to a temporary directory). Nevertheless, we will still return an absolute path.
A non-obvious directory layout may look like this:
CLOUD ASSEMBLY ROOT
+-- asset.12345abcdef/
+-- assembly-Stage
+-- MyStack.template.json
+-- MyStack.assets.json <- will contain { "path": "../asset.12345abcdef" }
toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string #with(...mixins: IMixin[]): IConstructdef with(self, mixins: IMixin) -> IConstructpublic IConstruct with(IMixin mixins)public IConstruct With(IMixin mixins)func With(Mixins IMixin) IConstruct #Mixins are applied in order. The list of constructs is captured at the start of the call, so constructs added by a mixin will not be visited. Use multiple with() calls if subsequent mixins should apply to added constructs.
1 parameter · returns
Parameters
mixinsmixinsmixinsmixinsMixins— The mixins to apply.
Returns: This construct for chaining