AWS 配置构造库
AWS CDK v1在2023-06-01上达到了支持结束. 此软件包不再更新,用户应该迁移到 AWS CDK v2.
关于如何移徙的更多信息,请参见: 移动到 AWS CDK v2 指南。 。 。 .
| 特征 | 稳定 |
|---|---|
| CFN 资源 | |
| 配置规则高级构件 | |
| 初始设置( 交付通道和配置记录器) 更高层次的构造 |
CFN 资源: 所有课程与
Cfn此模块中的前缀( n)CFN 资源总是 稳定安全使用.
稳定 : 本单元中标出稳定的更高层次的构造将不进行任何 打破更改。 他们将严格遵循 语义版本 型号.
AWS 配置 提供您 AWS 账户中 AWS 资源配置的详细视图. 这包括资源如何相互关联以及资源如何配置 以查看配置和关系如何随时间变化.
这个模块是 AWS 云开发套件 项目.
初始设置
在使用此模块提供的构造之前, 您需要设置 AWS 配置 (b) 将使用该物质的区域。 这一设置包括一次性创建 按区域分列的资源情况:
ConfigurationRecorder: 配置将记录哪些资源用于配置修改 .DeliveryChannel: 配置存储所记录数据的地点.
以下指南提供了启动AWS配置的步骤:
规则
AWS 配置可以通过创建 AWS 配置规则来评价您的 AWS 资源的配置设置, 它代表您的理想配置设置.
见 用 AWS 配置规则评估资源 学习更多关于 AWS 配置规则.
AWS 管理规则
AWS 配置提供了 AWS 管理的规则, 这些规则是 AWS 配置的预定义和自定义规则 用于评估您的 AWS 资源是否符合共同的最佳做法.
例如,您可以创建管理规则,检查活动访问密钥是否旋转 在指定的天数内.
// 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 配置管理规则列表。 。 。 .
以下关于AWS管理规则的更高层次构件可供使用.
访问密钥旋转
检查您的活动访问密钥是否在指定的天数内旋转 .
// compliant if access keys have been rotated within the last 90 days
new config.AccessKeysRotated(this, 'AccessKeyRotated');
堆积漂移检测
检查您的 CloudFormation 堆栈的实际配置是否不同, 或者是否漂移, 从它的预期配置.
// 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 话题发送事件通知 .
// 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 函数
以运行和触发类型.
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 配置规则实例事件
AWS文件有Lambda用于评价的功能的例子,即: 由配置变化所触发 财务报告和审定财务报表 定期触发
范围
默认规则会因全部更改而触发 资源。 。 。 .
使用 RuleScope APIs (英语:fromResource(), (中文(简体) ), fromResources() 或者说 fromTag()限制
管理规则和习惯规则的范围:
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 配置规则失败 :
// 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 状态
规则被重新评价.
// 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专题上.
// 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),
});