aws-cdk-lib.AssetStaging

class AssetStaging (construct)

LanguageType name
.NETAmazon.CDK.AssetStaging
Gogithub.com/aws/aws-cdk-go/awscdk/v2#AssetStaging
Javasoftware.amazon.awscdk.AssetStaging
Pythonaws_cdk.AssetStaging
TypeScript (source)aws-cdk-lib » AssetStaging

Implements IConstruct, IDependable

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.

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,
    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,
});

Initializer

new AssetStaging(scope: Construct, id: string, props: AssetStagingProps)

Parameters

  • scope Construct
  • id string
  • props AssetStagingProps

Construct Props

NameTypeDescription
sourcePathstringThe source file or directory to copy from.
assetHash?stringSpecify a custom hash for this asset.
assetHashType?AssetHashTypeSpecifies the type of hash to calculate for this asset.
bundling?BundlingOptionsBundle the asset by executing a command in a Docker container or a custom bundling provider.
exclude?string[]File paths matching the patterns will be excluded.
extraHash?stringExtra information to encode into the fingerprint (e.g. build instructions and other inputs).
follow?SymlinkFollowModeA strategy for how to handle symlinks.
ignoreMode?IgnoreModeThe ignore behavior to use for exclude patterns.

sourcePath

Type: string

The source file or directory to copy from.


assetHash?

Type: string (optional, default: based on assetHashType)

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.


assetHashType?

Type: AssetHashType (optional, default: the default is AssetHashType.SOURCE, but if assetHash is explicitly specified this value defaults to AssetHashType.CUSTOM.)

Specifies the type of hash to calculate for this asset.

If assetHash is configured, this option must be undefined or AssetHashType.CUSTOM.


bundling?

Type: BundlingOptions (optional, 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)

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.


exclude?

Type: string[] (optional, default: nothing is excluded)

File paths matching the patterns will be excluded.

See ignoreMode to set the matching behavior. Has no effect on Assets bundled using the bundling property.


extraHash?

Type: string (optional, default: hash is only based on source content)

Extra information to encode into the fingerprint (e.g. build instructions and other inputs).


follow?

Type: SymlinkFollowMode (optional, default: SymlinkFollowMode.NEVER)

A strategy for how to handle symlinks.


ignoreMode?

Type: IgnoreMode (optional, default: IgnoreMode.GLOB)

The ignore behavior to use for exclude patterns.

Properties

NameTypeDescription
absoluteStagedPathstringAbsolute path to the asset data.
assetHashstringA cryptographic hash of the asset.
isArchivebooleanWhether this asset is an archive (zip or jar).
nodeNodeThe tree node.
packagingFileAssetPackagingHow this asset should be packaged.
sourcePathstringThe absolute path of the asset as it was referenced by the user.
static BUNDLING_INPUT_DIRstringThe directory inside the bundling container into which the asset sources will be mounted.
static BUNDLING_OUTPUT_DIRstringThe directory inside the bundling container into which the bundled output should be written.

absoluteStagedPath

Type: string

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.


assetHash

Type: string

A cryptographic hash of the asset.


isArchive

Type: boolean

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


node

Type: Node

The tree node.


packaging

Type: FileAssetPackaging

How this asset should be packaged.


sourcePath

Type: string

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


static BUNDLING_INPUT_DIR

Type: string

The directory inside the bundling container into which the asset sources will be mounted.


static BUNDLING_OUTPUT_DIR

Type: string

The directory inside the bundling container into which the bundled output should be written.

Methods

NameDescription
relativeStagedPath(stack)Return the path to the staged asset, relative to the Cloud Assembly (manifest) directory of the given stack.
toString()Returns a string representation of this construct.
static clearAssetHashCache()Clears the asset hash cache.

relativeStagedPath(stack)

public relativeStagedPath(stack: Stack): string

Parameters

  • stack Stack

Returns

  • string

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

toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


static clearAssetHashCache()

public static clearAssetHashCache(): void

Clears the asset hash cache.