@aws-blocks/bb-app-setting

中文文档
0.4.0发布于 2026/11/02Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 6.7万

A single application configuration value backed by SSM Parameter Store.

原始使用文档
文档目录11 节
  1. API
  2. Options
  3. Error Handling
  4. Examples
  5. Plain String Setting
  6. Typed Object with Schema
  7. Secret
  8. Explicit Name (Cross-Service Reads)
  9. Validation Rules
  10. Scaling & Cost (AWS)
  11. Local Development

AppSetting

A single application configuration value backed by SSM Parameter Store.

When to use: You need a non-sensitive configuration value (feature flag, API URL, threshold) stored as a single SSM parameter. Each AppSetting instance maps to exactly one parameter.

When NOT to use: For a production secret (API key, OAuth client secret, database connection string, token), use Secret — it is the recommended home for credentials that protect a production app, in line with AWS guidance. AppSetting with secret: true does encrypt the value at rest and is free, so it is a fine fallback for a low-value secret in a prototype — but it is not the recommended place for production secrets. If you need structured key-value data with conditional writes and queries, use KVStore or DistributedTable.

Design & mock parity details: DESIGN.md

API

const setting = new AppSetting(scope, id, options)
MethodReturnsDescription
get()Promise<T>Read the current value.
put(value)Promise<void>Update the value at runtime.

Options

OptionTypeRequiredDescription
namestringNoSSM parameter path. When omitted, derived from the scope tree as /${fullId}, guaranteeing uniqueness within the stack.
valueTNoInitial value. Required for non-secret parameters. Must not be provided when secret is set.
schemaStandardSchemaV1<T>NoRuntime validation schema (Zod, Valibot, ArkType). Infers T from the schema. Cannot be used with secret.
secretbooleanNoWhen true, creates an SSM SecureString encrypted with the default aws/ssm KMS key (or kmsKeyArn when set). Cannot be used with schema or value. For a production secret, prefer Secret (AWS Secrets Manager) — see When NOT to use above.
kmsKeyArnstringNoARN of a customer-managed KMS key to encrypt the SecureString, instead of the default aws/ssm key. Only valid with secret: true. See the note below.
loggerChildLoggerNoOptional logger for internal operations. When omitted, a default Logger at error level is created.

Customer-managed KMS key (kmsKeyArn): Provide a CMK ARN to control the decrypt/grant scope of a secret (e.g. cross-account access or a dedicated key policy). The construct grants the shared handler kms:Decrypt (plus kms:Encrypt for secrets it writes) on that specific key ARN. Because AWS Blocks does not own a bring-your-own key, the key's own key policy must also allow the app's Lambda execution role to use it (or delegate to IAM via the account-root statement CDK adds to same-account keys). Adding or changing kmsKeyArn on an already-deployed secret re-encrypts its current value under the new key on the next deploy, so decryption keeps working.

Naming: When you omit name, the framework derives a unique SSM path from the construct scope tree (/${fullId}). This is the recommended approach — it prevents collisions across stacks automatically. If you provide an explicit name, you are responsible for ensuring it is unique across all stacks deployed to the same AWS account and region.

Error Handling

import { isBlocksError } from '@aws-blocks/core';
import { AppSettingErrors } from '@aws-blocks/bb-app-setting';

try {
  await setting.put(value);
} catch (e: unknown) {
  if (isBlocksError(e, AppSettingErrors.ValidationFailed)) {
    // schema validation failed or value exceeds 4 KB
  }
  throw e;
}

try {
  const value = await setting.get();
} catch (e: unknown) {
  if (isBlocksError(e, AppSettingErrors.ParameterNotFound)) {
    // the parameter is missing (e.g., deleted out-of-band) or a secret has an empty value
  }
  throw e;
}

get() can throw AppSettingErrors.ParameterNotFound when the parameter does not exist or a secret parameter has an empty value (in both the AWS and mock runtimes).

Examples

Plain String Setting

// Recommended: let the framework manage the SSM name (unique by default)
const apiUrl = new AppSetting(scope, 'apiUrl', {
  value: 'https://api.example.com',
});

export const api = new ApiNamespace(scope, 'api', (context) => ({
  async getApiUrl() {
    return { url: await apiUrl.get() };
  },
  async setApiUrl(url: string) {
    await apiUrl.put(url);
  },
}));

Typed Object with Schema

import { z } from 'zod';

const config = new AppSetting(scope, 'config', {
  value: { maxRetries: 3, timeout: 5000 },
  schema: z.object({ maxRetries: z.number(), timeout: z.number() }),
});

export const api = new ApiNamespace(scope, 'api', (context) => ({
  async getConfig() {
    return await config.get(); // { maxRetries: number; timeout: number }
  },
  async updateConfig(cfg: { maxRetries: number; timeout: number }) {
    await config.put(cfg); // validated against schema
  },
}));

Secret

const stripeKey = new AppSetting(scope, 'stripeKey', {
  secret: true,
});

export const api = new ApiNamespace(scope, 'api', (context) => ({
  async charge(amount: number) {
    const key = await stripeKey.get(); // decrypted from SSM SecureString
    // use key...
  },
}));

Explicit Name (Cross-Service Reads)

Use an explicit name when the SSM parameter must be read by other services or follow a well-known path convention.

⚠️ You must ensure this name is unique across all stacks deployed to the same AWS account and region to avoid collisions.

const apiUrl = new AppSetting(scope, 'apiUrl', {
  name: '/my-app/production/api-url',
  value: 'https://api.example.com',
});

Validation Rules

These are enforced at CDK synth time and in local development (the mock rejects the same combinations at construction):

  • secret + schema → error (secrets are plain strings)
  • schema without value → error (parameter needs a valid initial value)
  • secret with value → error (don't put secrets in source code)
  • Non-secret without value → error (settings require an initial value)

Scaling & Cost (AWS)

  • String parameters: Standard tier, free, 4 KB limit, 10,000 per account/region
  • SecureString parameters: Standard tier, free, encrypted with aws/ssm KMS key
  • Throughput: 40 TPS default for GetParameter (can be increased)
  • Latency: Single-digit ms reads and writes

Local Development

Mock data persists to disk at .bb-data/settings/{fullId}/value.json across dev server restarts. Wipe with rm -rf .bb-data. Secrets generate a random value locally (no KMS encryption in mock mode).

版本与发布记录

@aws-blocks/bb-app-setting 共发布 14 个版本,首次发布于 2026/06/16;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.0latest2026/11/025 天前
0.3.22026/10/1622 天前
0.0.0-canary.esql-canary.c93c81b.1791235863canary-esql-canary2026/10/061 个月前
0.3.12026/10/051 个月前
0.0.0-canary.esql-canary.f7f39ac.17909891272026/10/031 个月前
0.0.0-canary.ci-publish-canary-npm.f53dfdc.1790988617canary-ci-publish-canary-npm2026/10/031 个月前

关于 @aws-blocks/bb-app-setting 的常见问题

@aws-blocks/bb-app-setting 是什么?

A single application configuration value backed by SSM Parameter Store.

如何安装 @aws-blocks/bb-app-setting?

在项目目录运行 npm install @aws-blocks/bb-app-setting,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:constructs(^10.6.0)、aws-cdk-lib(^2.257.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aws-blocks/bb-app-setting 支持 TypeScript 吗?

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

@aws-blocks/bb-app-setting 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@aws-blocks/bb-app-setting 可以商用吗?

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

@aws-blocks/bb-app-setting 的下载量有多少?

根据 npm 官方下载统计,@aws-blocks/bb-app-setting 在 2026/10/07 – 2026/11/05 这 30 天内被下载 67,149 次,日均约 2,238 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws-blocks/bb-app-setting 还在维护吗?

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