@aws-cdk/cdk-assets-lib

中文文档
1.5.2发布于 2026/10/31Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 87.4万

CDK Asset Publishing Library

原始使用文档
文档目录6 节
  1. Overview
  2. Usage
  3. Manifest Example
  4. Placeholders
  5. Docker image credentials
  6. Using Drop-in Docker Replacements

cdk-assets

A library for publishing CDK assets to AWS environments.

This is a library, for integrating into applications like the CDK Toolkit CLI. If you just want to upload assets as a user, use the cdk-assets CLI

Overview

@aws-cdk/cdk-assets-lib requires an asset manifest file called assets.json, in a CDK CloudAssembly (cdk.out/assets.json). It will take the assets listed in the manifest, prepare them as required and upload them to the locations indicated in the manifest.

Currently the following asset types are supported:

  • Files and archives, uploaded to S3
  • Docker Images, uploaded to ECR
  • Files, archives, and Docker images built by external utilities

S3 buckets and ECR repositories to upload to are expected to exist already.

We expect assets to be immutable, and we expect that immutability to be reflected both in the asset ID and in the destination location. This reflects itself in the following behaviors:

  • If the indicated asset already exists in the given destination location, it will not be packaged and uploaded.
  • If some locally cached artifact (depending on the asset type a file or an image in the local Docker cache) already exists named after the asset's ID, it will not be packaged, but will be uploaded directly to the destination location.

For assets build by external utilities, the contract is such that cdk-assets expects the utility to manage dedupe detection as well as path/image tag generation. This means that cdk-assets will call the external utility every time generation is warranted, and it is up to the utility to a) determine whether to do a full rebuild; and b) to return only one thing on stdout: the path to the file/archive asset, or the name of the local Docker image.

Usage

Usage looks like this:

const manifest = AssetManifest.fromPath(args.path);
const someAssets = manifest.select([DestinationPattern.parse('...')]);

// Or implement your own AWS host
const aws: IAws = new DefaultAwsClient('PROFILENAME');

// Or implement your own progress listener
const progressListener: IProgressListener = new ConsoleProgress();


const pub = new AssetPublishing(manifest, {
  aws,
  progressListener,
});

await pub.publish();

Manifest Example

An asset manifest looks like this:

{
  "version": "1.22.0",
  "files": {
    "7aac5b80b050e7e4e168f84feffa5893": {
      "source": {
        "path": "some_directory",
        "packaging": "zip"
      },
      "destinations": {
        "us-east-1": {
          "region": "us-east-1",
          "assumeRoleArn": "arn:aws:iam::12345789012:role/my-account",
          "bucketName": "MyBucket",
          "objectKey": "7aac5b80b050e7e4e168f84feffa5893.zip"
        }
      }
    },
    "3dfe2b80b050e7e4e168f84feff678d4": {
      "source": {
        "executable": ["myzip"]
      },
      "destinations": {
        "us-east-1": {
          "region": "us-east-1",
          "assumeRoleArn": "arn:aws:iam::12345789012:role/my-account",
          "bucketName": "MySpecialBucket",
          "objectKey": "3dfe2b80b050e7e4e168f84feff678d4.zip"
        }
      }
    }
  },
  "dockerImages": {
    "b48783c58a86f7b8c68a4591c4f9be31": {
      "source": {
        "directory": "dockerdir"
      },
      "destinations": {
        "us-east-1": {
          "region": "us-east-1",
          "assumeRoleArn": "arn:aws:iam::12345789012:role/my-account",
          "repositoryName": "MyRepository",
          "imageTag": "b48783c58a86f7b8c68a4591c4f9be31",
          "imageUri": "123456789012.dkr.ecr.us-east-1.amazonaws.com/MyRepository:1234567891b48783c58a86f7b8c68a4591c4f9be31"
        }
      }
    },
    "d92753c58a86f7b8c68a4591c4f9cf28": {
      "source": {
        "executable": ["mytool", "package", "dockerdir"]
      },
      "destinations": {
        "us-east-1": {
          "region": "us-east-1",
          "assumeRoleArn": "arn:aws:iam::12345789012:role/my-account",
          "repositoryName": "MyRepository2",
          "imageTag": "d92753c58a86f7b8c68a4591c4f9cf28",
          "imageUri": "123456789987.dkr.ecr.us-east-1.amazonaws.com/MyRepository2:1234567891b48783c58a86f7b8c68a4591c4f9be31"
        }
      }
    }
  }
}

Placeholders

The destination block of an asset manifest may contain the following region and account placeholders:

  • ${AWS::Region}
  • ${AWS::AccountId}

These will be substituted with the region and account IDs currently configured on the AWS SDK (through environment variables or ~/.aws/... config files).

  • The ${AWS::AccountId} placeholder will not be re-evaluated after performing the AssumeRole call.
  • If ${AWS::Region} is used, it will principally be replaced with the value in the region key. If the default region is intended, leave the region key out of the manifest at all.

Docker image credentials

For Docker image asset publishing, cdk-assets will docker login with credentials from ECR GetAuthorizationToken prior to building and publishing, so that the Dockerfile can reference images in the account's ECR repo.

cdk-assets can also be configured to read credentials from both ECR and SecretsManager prior to build by creating a credential configuration at '~/.cdk/cdk-docker-creds.json' (override this location by setting the CDK_DOCKER_CREDS_FILE environment variable). The credentials file has the following format:

{
  "version": "1.0",
  "domainCredentials": {
    "domain1.example.com": {
      "secretsManagerSecretId": "mySecret", // Can be the secret ID or full ARN
      "roleArn": "arn:aws:iam::0123456789012:role/my-role" // (Optional) role with permissions to the secret
    },
    "domain2.example.com": {
      "ecrRepository": true,
      "roleArn": "arn:aws:iam::0123456789012:role/my-role" // (Optional) role with permissions to the repo
    }
  }
}

If the credentials file is present, docker will be configured to use the docker-credential-cdk-assets credential helper for each of the domains listed in the file. This helper will assume the role provided (if present), and then fetch the login credentials from either SecretsManager or ECR.

Using Drop-in Docker Replacements

By default, the AWS CDK will build and publish Docker image assets using the docker command. However, by specifying the CDK_DOCKER environment variable, you can override the command that will be used to build and publish your assets.

版本与发布记录

@aws-cdk/cdk-assets-lib 共发布 34 个版本,首次发布于 2025/06/25;过去 12 个月发布了 29 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.5.2latest2026/10/318 天前
1.5.12026/10/0830 天前
1.5.02026/09/042 个月前
1.4.172026/08/272 个月前
1.4.162026/08/202 个月前
1.4.152026/07/233 个月前

关于 @aws-cdk/cdk-assets-lib 的常见问题

@aws-cdk/cdk-assets-lib 是什么?

CDK Asset Publishing Library

如何安装 @aws-cdk/cdk-assets-lib?

在项目目录运行 npm install @aws-cdk/cdk-assets-lib,或使用 pnpm add / yarn add / bun add。

@aws-cdk/cdk-assets-lib 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws-cdk/cdk-assets-lib 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@aws-cdk/cdk-assets-lib 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aws-cdk/cdk-assets-lib 可以商用吗?

@aws-cdk/cdk-assets-lib 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws-cdk/cdk-assets-lib 的下载量有多少?

根据 npm 官方下载统计,@aws-cdk/cdk-assets-lib 在 2026/10/08 – 2026/11/06 这 30 天内被下载 874,419 次,日均约 29,147 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws-cdk/cdk-assets-lib 还在维护吗?

最新版本 1.5.2 发布于 2026/10/31(8 天前);至今共发布 34 个版本,其中过去 12 个月发布了 29 个;首次发布于 2025/06/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。