AssetStaging

class aws_cdk.core.AssetStaging(scope, id, *, source_path, extra_hash=None, asset_hash=None, asset_hash_type=None, bundling=None, exclude=None, follow=None, ignore_mode=None)

Bases: Construct

Stages a file or directory from a location on the file system into a staging directory.

This is controlled by the context key ‘aws:cdk:asset-staging’ and enabled by the CLI by default in order to ensure that when the CDK app exists, all assets are available for deployment. Otherwise, if an app references assets in temporary locations, those will not be available when it exists (see https://github.com/aws/aws-cdk/issues/1716).

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.

ExampleMetadata:

fixture=_generated

Example:

# The code below shows an example of how to instantiate this type.
# The values are placeholders you should change.
import aws_cdk.core 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
        command=["command"],
        entrypoint=["entrypoint"],
        environment={
            "environment_key": "environment"
        },
        local=local_bundling,
        output_type=cdk.BundlingOutput.ARCHIVED,
        security_opt="securityOpt",
        user="user",
        volumes=[cdk.DockerVolume(
            container_path="containerPath",
            host_path="hostPath",

            # the properties below are optional
            consistency=cdk.DockerVolumeConsistency.CONSISTENT
        )],
        working_directory="workingDirectory"
    ),
    exclude=["exclude"],
    extra_hash="extraHash",
    follow=cdk.SymlinkFollowMode.NEVER,
    ignore_mode=cdk.IgnoreMode.GLOB
)
Parameters:
  • scope (Construct) –

  • id (str) –

  • source_path (str) – The source file or directory to copy from.

  • extra_hash (Optional[str]) – Extra information to encode into the fingerprint (e.g. build instructions and other inputs). Default: - hash is only based on source content

  • asset_hash (Optional[str]) – Specify a custom hash for this asset. 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. Default: - based on assetHashType

  • asset_hash_type (Optional[AssetHashType]) – Specifies the type of hash to calculate for this asset. If assetHash is configured, this option must be undefined or AssetHashType.CUSTOM. Default: - the default is AssetHashType.SOURCE, but if assetHash is explicitly specified this value defaults to AssetHashType.CUSTOM.

  • bundling (Union[BundlingOptions, Dict[str, Any], None]) – Bundle the asset by executing a command in a Docker container or a custom bundling provider. 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. 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

  • exclude (Optional[Sequence[str]]) – Glob patterns to exclude from the copy. Default: - nothing is excluded

  • follow (Optional[SymlinkFollowMode]) – A strategy for how to handle symlinks. Default: SymlinkFollowMode.NEVER

  • ignore_mode (Optional[IgnoreMode]) – The ignore behavior to use for exclude patterns. Default: IgnoreMode.GLOB

Methods

relative_staged_path(stack)

Return the path to the staged asset, relative to the Cloud Assembly (manifest) directory of the given stack.

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" }
Parameters:

stack (Stack) –

Return type:

str

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

BUNDLING_INPUT_DIR = '/asset-input'
BUNDLING_OUTPUT_DIR = '/asset-output'
absolute_staged_path

Absolute path to the asset data.

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.

asset_hash

A cryptographic hash of the asset.

is_archive

Whether this asset is an archive (zip or jar).

node

The construct tree node associated with this construct.

packaging

How this asset should be packaged.

source_hash

(deprecated) A cryptographic hash of the asset.

Deprecated:

see assetHash.

Stability:

deprecated

source_path

The absolute path of the asset as it was referenced by the user.

staged_path

(deprecated) Absolute path to the asset data.

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.

Deprecated:
  • Use absoluteStagedPath instead.

Stability:

deprecated

Static Methods

classmethod clear_asset_hash_cache()

Clears the asset hash cache.

Return type:

None

classmethod is_construct(x)

Return whether the given object is a Construct.

Parameters:

x (Any) –

Return type:

bool