Package: aws-cdk.aws-config
AWS Config Construct Library---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 theMigrating to AWS CDK v2guide.FeaturesStabilityCFN ResourcesHigher level constructs for Config RulesHigher level constructs for initial set-up (delivery channel & configuration recorder)CFN Resources:All classes with theCfnprefix in this module (CFN Resources) are always
stable and safe to use.Stable:Higher level constructs in this module that are marked stable will not undergo any
breaking changes. They will strictly follow theSemantic Versioningmodel.AWS Configprovides a detailed view of the configuration of AWS resources in your AWS account.
This includes how the resources are related to one another and how they were configured in the
past so that you can see how the configurations and relationships change over time.This module is part of theAWS Cloud Development Kitproject.Initial SetupBefore using the constructs provided in this module, you need to set up AWS Config
in the region in which it will be used. This setup includes the one-time creation of the
following resources per region:ConfigurationRecorder: Configure which resources will be recorded for config changes.DeliveryChannel: Configure where to store the recorded data.The following guides provide the steps for getting started with AWS Config:Using the AWS ConsoleUsing the AWS CLIRulesAWS Config can evaluate the configuration settings of your AWS resources by creating AWS Config rules,
which represent your ideal configuration settings.SeeEvaluating Resources with AWS Config Rulesto learn more about AWS Config rules.AWS Managed RulesAWS Config provides AWS managed rules, which are predefined, customizable rules that AWS Config
uses to evaluate whether your AWS resources comply with common best practices.For example, you could create a managed rule that checks whether active access keys are rotated
within the number of days specified.# https://docs.aws.amazon.com/config/latest/developerguide/access-keys-rotated.htmlconfig.ManagedRule(self,"AccessKeysRotated",identifier=config.ManagedRuleIdentifiers.ACCESS_KEYS_ROTATED,input_parameters={"max_access_key_age":60},# default is 24 hoursmaximum_execution_frequency=config.MaximumExecutionFrequency.TWELVE_HOURS)Identifiers for AWS managed rules are available through static constants in theManagedRuleIdentifiersclass.
You can find supported input parameters in theList of AWS Config Managed Rules.The following higher level constructs for AWS managed rules are available.Access Key rotationChecks whether your active access keys are rotated within the number of days specified.# compliant if access keys have been rotated within the last 90 daysconfig.AccessKeysRotated(self,"AccessKeyRotated")CloudFormation Stack drift detectionChecks whether your CloudFormation stack's actual configuration differs, or has drifted,
from it's expected configuration.# compliant if stack's status is 'IN_SYNC'# non-compliant if the stack's drift status is 'DRIFTED'config.CloudFormationStackDriftDetectionCheck(self,"Drift",own_stack_only=True)CloudFormation Stack notificationsChecks whether your CloudFormation stacks are sending event notifications to a SNS topic.# topics to which CloudFormation stacks may send event notificationstopic1=sns.Topic(self,"AllowedTopic1")topic2=sns.Topic(self,"AllowedTopic2")# non-compliant if CloudFormation stack does not send notifications to 'topic1' or 'topic2'config.CloudFormationStackNotificationCheck(self,"NotificationCheck",topics=[topic1,topic2])Custom rulesYou can develop custom rules and add them to AWS Config. You associate each custom rule with an
AWS Lambda function, which contains the logic that evaluates whether your AWS resources comply
with the rule.TriggersAWS Lambda executes functions in response to events that are published by AWS Services.
The function for a custom Config rule receives an event that is published by AWS Config,
and is responsible for evaluating the compliance of the rule.Evaluations can be triggered by configuration changes, periodically, or both.
To create a custom rule, define aCustomRuleand specify the Lambda Function
to run and the trigger types.# eval_compliance_fn: lambda.Functionconfig.CustomRule(self,"CustomRule",lambda_function=eval_compliance_fn,configuration_changes=True,periodic=True,# default is 24 hoursmaximum_execution_frequency=config.MaximumExecutionFrequency.SIX_HOURS)When the trigger for a rule occurs, the Lambda function is invoked by publishing an event.
Seeexample events for AWS Config RulesThe AWS documentation has examples of Lambda functions for evaluations that aretriggered by configuration changesandtriggered periodicallyScopeBy default rules are triggered by changes to allresources.Use theRuleScopeAPIs (fromResource(),fromResources()orfromTag()) to restrict
the scope of both managed and custom rules:# eval_compliance_fn: lambda.Functionssh_rule=config.ManagedRule(self,"SSH",identifier=config.ManagedRuleIdentifiers.EC2_SECURITY_GROUPS_INCOMING_SSH_DISABLED,rule_scope=config.RuleScope.from_resource(config.ResourceType.EC2_SECURITY_GROUP,"sg-1234567890abcdefgh"))custom_rule=config.CustomRule(self,"Lambda",lambda_function=eval_compliance_fn,configuration_changes=True,rule_scope=config.RuleScope.from_resources([config.ResourceType.CLOUDFORMATION_STACK,config.ResourceType.S3_BUCKET]))tag_rule=config.CustomRule(self,"CostCenterTagRule",lambda_function=eval_compliance_fn,configuration_changes=True,rule_scope=config.RuleScope.from_tag("Cost Center","MyApp"))EventsYou can define Amazon EventBridge event rules which trigger when a compliance check fails
or when a rule is re-evaluated.Use theonComplianceChange()APIs to trigger an EventBridge event when a compliance check
of your AWS Config Rule fails:# Topic to which compliance notification events will be publishedcompliance_topic=sns.Topic(self,"ComplianceTopic")rule=config.CloudFormationStackDriftDetectionCheck(self,"Drift")rule.on_compliance_change("TopicEvent",target=targets.SnsTopic(compliance_topic))Use theonReEvaluationStatus()status to trigger an EventBridge event when an AWS Config
rule is re-evaluated.# Topic to which re-evaluation notification events will be publishedre_evaluation_topic=sns.Topic(self,"ComplianceTopic")rule=config.CloudFormationStackDriftDetectionCheck(self,"Drift")rule.on_re_evaluation_status("ReEvaluationEvent",target=targets.SnsTopic(re_evaluation_topic))ExampleThe following example creates a custom rule that evaluates whether EC2 instances are compliant.
Compliance events are published to an SNS topic.# Lambda function containing logic that evaluates compliance with the rule.eval_compliance_fn=lambda_.Function(self,"CustomFunction",code=lambda_.AssetCode.from_inline("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 instancescustom_rule=config.CustomRule(self,"Custom",configuration_changes=True,lambda_function=eval_compliance_fn,rule_scope=config.RuleScope.from_resource(config.ResourceType.EC2_INSTANCE))# A rule to detect stack driftsdrift_rule=config.CloudFormationStackDriftDetectionCheck(self,"Drift")# Topic to which compliance notification events will be publishedcompliance_topic=sns.Topic(self,"ComplianceTopic")# Send notification on compliance change eventsdrift_rule.on_compliance_change("ComplianceChange",target=targets.SnsTopic(compliance_topic))