# @aws-cdk/aws-config 中文文档

规范地址：https://npmcn.com/package/%40aws-cdk/aws-config/

版本：1\.204\.0
npm 元数据更新时间：2023-11-21T10:14:47.009Z
中文译文更新时间：2026-11-07T16:16:57.179Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40aws-cdk/aws-config
原文对照：https://npmcn.com/package/%40aws-cdk/aws-config/?lang=original
项目主页：https://github.com/aws/aws-cdk
源码仓库：https://github.com/aws/aws-cdk

弃用说明：AWS CDK v1 has reached End-of-Support on 2023-06-01\. This package is no longer being updated, and users should migrate to AWS CDK v2\. For more information on how to migrate, see https://docs\.aws\.amazon\.com/cdk/v2/guide/migrating-v2\.html

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：1.9 MB，17 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>= 14\.15\.0
- 运行依赖：6 个
- peer 依赖：@aws-cdk/aws-events 1\.204\.0，@aws-cdk/aws-iam 1\.204\.0，@aws-cdk/aws-lambda 1\.204\.0，@aws-cdk/aws-sns 1\.204\.0，@aws-cdk/core 1\.204\.0，constructs ^3\.3\.69
- 首次发布：2018-07-31T13:32:04.636Z
- 最新版本发布：2023-06-19T21:11:17.475Z
- 近 30 天下载量：5135（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：288（近 12 个月 0 个）

## 中文简介

AWS: Config 的 CDK 构造库

## 安装

```sh
npm install @aws-cdk/aws-config
```

也可以使用：`pnpm add @aws-cdk/aws-config`、`yarn add @aws-cdk/aws-config`、`bun add @aws-cdk/aws-config`

## README

# AWS 配置构造库

* * *

![End-of-Support](https://img.shields.io/badge/End--of--Support-critical.svg?style=for-the-badge)

> AWS CDK v1在2023-06-01上达到了支持结束. 此软件包不再更新,用户应该迁移到 AWS CDK v2.
> 
> 关于如何移徙的更多信息,请参见: [_移动到 AWS CDK v2_ 指南](https://docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html)。 。 。 .

* * *

| 特征 | 稳定 |
| --- | --- |
| CFN 资源 | ![Stable](https://img.shields.io/badge/stable-success.svg?style=for-the-badge) |
| 配置规则高级构件 | ![Stable](https://img.shields.io/badge/stable-success.svg?style=for-the-badge) |
| 初始设置( 交付通道和配置记录器) 更高层次的构造 | ![Not Implemented](https://img.shields.io/badge/not--implemented-black.svg?style=for-the-badge) |

> **CFN 资源:** 所有课程与 `Cfn` 此模块中的前缀( n)[CFN 资源](https://docs.aws.amazon.com/cdk/latest/guide/constructs.html#constructs_lib)总是 稳定安全使用.

> **稳定 :** 本单元中标出稳定的更高层次的构造将不进行任何 打破更改。 他们将严格遵循 [语义版本](https://semver.org/) 型号.

* * *

[AWS 配置](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html) 提供您 AWS 账户中 AWS 资源配置的详细视图. 这包括资源如何相互关联以及资源如何配置 以查看配置和关系如何随时间变化.

这个模块是 [AWS 云开发套件](https://github.com/aws/aws-cdk) 项目.

## 初始设置

在使用此模块提供的构造之前, 您需要设置 AWS 配置 (b) 将使用该物质的区域。 这一设置包括一次性创建 按区域分列的资源情况:

-   `ConfigurationRecorder`: 配置将记录哪些资源用于配置修改 .
-   `DeliveryChannel`: 配置存储所记录数据的地点.

以下指南提供了启动AWS配置的步骤:

-   [使用 AWS 控制台](https://docs.aws.amazon.com/config/latest/developerguide/gs-console.html)
-   [使用 AWS CLI](https://docs.aws.amazon.com/config/latest/developerguide/gs-cli.html)

## 规则

AWS 配置可以通过创建 AWS 配置规则来评价您的 AWS 资源的配置设置, 它代表您的理想配置设置.

见 [用 AWS 配置规则评估资源](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config.html) 学习更多关于 AWS 配置规则.

### AWS 管理规则

AWS 配置提供了 AWS 管理的规则, 这些规则是 AWS 配置的预定义和自定义规则 用于评估您的 AWS 资源是否符合共同的最佳做法.

例如,您可以创建管理规则,检查活动访问密钥是否旋转 在指定的天数内.

```ts
// https://docs.aws.amazon.com/config/latest/developerguide/access-keys-rotated.html
new config.ManagedRule(this, 'AccessKeysRotated', {
  identifier: config.ManagedRuleIdentifiers.ACCESS_KEYS_ROTATED,
  inputParameters: {
    maxAccessKeyAge: 60, // default is 90 days
  },

  // default is 24 hours
  maximumExecutionFrequency: config.MaximumExecutionFrequency.TWELVE_HOURS,
});
```

AWS 管理规则的标识符可以通过静态常数在 `ManagedRuleIdentifiers` 课. 您可以在 [AWS 配置管理规则列表](https://docs.aws.amazon.com/config/latest/developerguide/managed-rules-by-aws-config.html)。 。 。 .

以下关于AWS管理规则的更高层次构件可供使用.

#### 访问密钥旋转

检查您的活动访问密钥是否在指定的天数内旋转 .

```ts
// compliant if access keys have been rotated within the last 90 days
new config.AccessKeysRotated(this, 'AccessKeyRotated');
```

#### 堆积漂移检测

检查您的 CloudFormation 堆栈的实际配置是否不同, 或者是否漂移, 从它的预期配置.

```ts
// compliant if stack's status is 'IN_SYNC'
// non-compliant if the stack's drift status is 'DRIFTED'
new config.CloudFormationStackDriftDetectionCheck(this, 'Drift', {
  ownStackOnly: true, // checks only the stack containing the rule
});
```

#### 云格式堆栈通知

检查您的 CloudFormation 堆栈是否向 SNS 话题发送事件通知 .

```ts
// topics to which CloudFormation stacks may send event notifications
const topic1 = new sns.Topic(this, 'AllowedTopic1');
const topic2 = new sns.Topic(this, 'AllowedTopic2');

// non-compliant if CloudFormation stack does not send notifications to 'topic1' or 'topic2'
new config.CloudFormationStackNotificationCheck(this, 'NotificationCheck', {
  topics: [topic1, topic2],
});
```

### 自定义规则

可以开发自定义规则并添加到AWS配置中. 您将每个自定义规则与 AWS Lambda 函数,其中包含评价您的 AWS 资源是否符合的逻辑 与规则.

### 触发器

AWS Lambda针对由AWS Service发布的事件执行功能. 自定义配置规则的函数接收由 AWS 配置发布的事件, 并负责评价该规则的遵守情况.

评价可以通过配置的定期改变或两者兼而有之来触发. 要创建自定义规则,请定义 `CustomRule` 并指定 Lambda 函数 以运行和触发类型.

```ts
declare const evalComplianceFn: lambda.Function;

new config.CustomRule(this, 'CustomRule', {
  lambdaFunction: evalComplianceFn,
  configurationChanges: true,
  periodic: true,

  // default is 24 hours
  maximumExecutionFrequency: config.MaximumExecutionFrequency.SIX_HOURS,
});
```

当规则的触发发生时,Lambda函数通过发布事件被引用. 见 [aWS 配置规则实例事件](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules_example-events.html)

AWS文件有Lambda用于评价的功能的例子,即: [由配置变化所触发](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules_nodejs-sample.html#event-based-example-rule) 财务报告和审定财务报表 [定期触发](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules_nodejs-sample.html#periodic-example-rule)

### 范围

默认规则会因全部更改而触发 [资源](https://docs.aws.amazon.com/config/latest/developerguide/resource-config-reference.html#supported-resources)。 。 。 .

使用 `RuleScope` APIs (英语:`fromResource()`, (中文(简体) ), `fromResources()` 或者说 `fromTag()`限制 管理规则和习惯规则的范围:

```ts
const sshRule = new config.ManagedRule(this, 'SSH', {
  identifier: config.ManagedRuleIdentifiers.EC2_SECURITY_GROUPS_INCOMING_SSH_DISABLED,
  ruleScope: config.RuleScope.fromResource(config.ResourceType.EC2_SECURITY_GROUP, 'sg-1234567890abcdefgh'), // restrict to specific security group
});

declare const evalComplianceFn: lambda.Function;
const customRule = new config.CustomRule(this, 'Lambda', {
  lambdaFunction: evalComplianceFn,
  configurationChanges: true,
  ruleScope: config.RuleScope.fromResources([config.ResourceType.CLOUDFORMATION_STACK, config.ResourceType.S3_BUCKET]), // restrict to all CloudFormation stacks and S3 buckets
});

const tagRule = new config.CustomRule(this, 'CostCenterTagRule', {
  lambdaFunction: evalComplianceFn,
  configurationChanges: true,
  ruleScope: config.RuleScope.fromTag('Cost Center', 'MyApp'), // restrict to a specific tag
});
```

### 活动

您可以定义当合规检查失败时触发的 Amazon EventBridge 事件规则 或者当一个规则被重新评价.

使用 `onComplianceChange()` 当检查合规性时触发事件Bridge的 API 您的 AWS 配置规则失败 :

```ts
// Topic to which compliance notification events will be published
const complianceTopic = new sns.Topic(this, 'ComplianceTopic');

const rule = new config.CloudFormationStackDriftDetectionCheck(this, 'Drift');
rule.onComplianceChange('TopicEvent', {
  target: new targets.SnsTopic(complianceTopic),
});
```

使用 `onReEvaluationStatus()` 当 AWS 配置时触发事件Bridge 状态 规则被重新评价.

```ts
// Topic to which re-evaluation notification events will be published
const reEvaluationTopic = new sns.Topic(this, 'ComplianceTopic');

const rule = new config.CloudFormationStackDriftDetectionCheck(this, 'Drift');
rule.onReEvaluationStatus('ReEvaluationEvent', {
  target: new targets.SnsTopic(reEvaluationTopic),
});
```

### 示例

以下实例创建了一条习惯规则,用以评价EC2实例是否合规. 遵约活动将发表在SNS专题上.

```ts
// Lambda function containing logic that evaluates compliance with the rule.
const evalComplianceFn = new lambda.Function(this, 'CustomFunction', {
  code: lambda.AssetCode.fromInline('exports.handler = (event) => console.log(event);'),
  handler: 'index.handler',
  runtime: lambda.Runtime.NODEJS_14_X,
});

// A custom rule that runs on configuration changes of EC2 instances
const customRule = new config.CustomRule(this, 'Custom', {
  configurationChanges: true,
  lambdaFunction: evalComplianceFn,
  ruleScope: config.RuleScope.fromResource(config.ResourceType.EC2_INSTANCE),
});

// A rule to detect stack drifts
const driftRule = new config.CloudFormationStackDriftDetectionCheck(this, 'Drift');

// Topic to which compliance notification events will be published
const complianceTopic = new sns.Topic(this, 'ComplianceTopic');

// Send notification on compliance change events
driftRule.onComplianceChange('ComplianceChange', {
  target: new targets.SnsTopic(complianceTopic),
});
```

## 常见问题

### @aws-cdk/aws-config 是什么？

AWS: Config 的 CDK 构造库注意：该包已被作者标记为弃用——AWS CDK v1 has reached End-of-Support on 2023-06-01\. This package is no longer being updated, and users should migrate to AWS CDK v2\. For more information on how to migrate, see https://docs\.aws\.amazon\.com/cdk/v2/guide/migrating-v2\.html

### 如何安装 @aws-cdk/aws-config？

在项目目录运行 npm install @aws-cdk/aws-config，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@aws-cdk/aws-events（1\.204\.0）、@aws-cdk/aws-iam（1\.204\.0）、@aws-cdk/aws-lambda（1\.204\.0）、@aws-cdk/aws-sns（1\.204\.0）、@aws-cdk/core（1\.204\.0）、constructs（^3\.3\.69）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @aws-cdk/aws-config 支持 TypeScript 吗？

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

### @aws-cdk/aws-config 支持 ESM 还是 CommonJS？

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

### @aws-cdk/aws-config 需要什么版本的 Node\.js？

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

### @aws-cdk/aws-config 可以商用吗？

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

### @aws-cdk/aws-config 的下载量有多少？

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

### @aws-cdk/aws-config 还在维护吗？

最新版本 1\.204\.0 发布于 2023/06/20（3 年前）；至今共发布 288 个版本，其中过去 12 个月发布了 0 个；首次发布于 2018/07/31。作者已将其标记为弃用，新项目建议寻找替代方案。

