Package software.amazon.awscdk.services.config
AWS Config Construct Library
AWS Config provides 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 the AWS Cloud Development Kit project.
Initial Setup
Before 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:
Rules
AWS Config can evaluate the configuration settings of your AWS resources by creating AWS Config rules, which represent your ideal configuration settings.
See Evaluating Resources with AWS Config Rules to learn more about AWS Config rules.
AWS Managed Rules
AWS 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.html // https://docs.aws.amazon.com/config/latest/developerguide/access-keys-rotated.html ManagedRule.Builder.create(this, "AccessKeysRotated") .identifier(ManagedRuleIdentifiers.ACCESS_KEYS_ROTATED) .inputParameters(Map.of( "maxAccessKeyAge", 60)) // default is 24 hours .maximumExecutionFrequency(MaximumExecutionFrequency.TWELVE_HOURS) .build();
Identifiers for AWS managed rules are available through static constants in the ManagedRuleIdentifiers
class.
You can find supported input parameters in the List of AWS Config Managed Rules.
The following higher level constructs for AWS managed rules are available.
Access Key rotation
Checks whether your active access keys are rotated within the number of days specified.
// compliant if access keys have been rotated within the last 90 days // compliant if access keys have been rotated within the last 90 days new AccessKeysRotated(this, "AccessKeyRotated");
CloudFormation Stack drift detection
Checks 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' // compliant if stack's status is 'IN_SYNC' // non-compliant if the stack's drift status is 'DRIFTED' CloudFormationStackDriftDetectionCheck.Builder.create(this, "Drift") .ownStackOnly(true) .build();
CloudFormation Stack notifications
Checks whether your CloudFormation stacks are sending event notifications to a SNS topic.
// topics to which CloudFormation stacks may send event notifications Topic topic1 = new Topic(this, "AllowedTopic1"); Topic topic2 = new Topic(this, "AllowedTopic2"); // non-compliant if CloudFormation stack does not send notifications to 'topic1' or 'topic2' // non-compliant if CloudFormation stack does not send notifications to 'topic1' or 'topic2' CloudFormationStackNotificationCheck.Builder.create(this, "NotificationCheck") .topics(List.of(topic1, topic2)) .build();
Custom rules
You can develop custom rules and add them to AWS Config. You associate each custom rule with an AWS Lambda function and Guard.
Custom Lambda Rules
Lambda function which contains the logic that evaluates whether your AWS resources comply with the rule.
// Lambda function containing logic that evaluates compliance with the rule. Function evalComplianceFn = Function.Builder.create(this, "CustomFunction") .code(AssetCode.fromInline("exports.handler = (event) => console.log(event);")) .handler("index.handler") .runtime(Runtime.NODEJS_18_X) .build(); // A custom rule that runs on configuration changes of EC2 instances CustomRule customRule = CustomRule.Builder.create(this, "Custom") .configurationChanges(true) .lambdaFunction(evalComplianceFn) .ruleScope(RuleScope.fromResource(ResourceType.EC2_INSTANCE)) .build();
Custom Policy Rules
Guard which contains the logic that evaluates whether your AWS resources comply with the rule.
String samplePolicyText = "\n# This rule checks if point in time recovery (PITR) is enabled on active Amazon DynamoDB tables\nlet status = ['ACTIVE']\n\nrule tableisactive when\n resourceType == \"AWS::DynamoDB::Table\" {\n configuration.tableStatus == %status\n}\n\nrule checkcompliance when\n resourceType == \"AWS::DynamoDB::Table\"\n tableisactive {\n let pitr = supplementaryConfiguration.ContinuousBackupsDescription.pointInTimeRecoveryDescription.pointInTimeRecoveryStatus\n %pitr == \"ENABLED\"\n}\n"; CustomPolicy.Builder.create(this, "Custom") .policyText(samplePolicyText) .enableDebugLog(true) .ruleScope(RuleScope.fromResources(List.of(ResourceType.DYNAMODB_TABLE))) .build();
Triggers
AWS 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 a CustomRule
and specify the Lambda Function
to run and the trigger types.
Function evalComplianceFn; CustomRule.Builder.create(this, "CustomRule") .lambdaFunction(evalComplianceFn) .configurationChanges(true) .periodic(true) // default is 24 hours .maximumExecutionFrequency(MaximumExecutionFrequency.SIX_HOURS) .build();
When the trigger for a rule occurs, the Lambda function is invoked by publishing an event. See example events for AWS Config Rules
The AWS documentation has examples of Lambda functions for evaluations that are triggered by configuration changes and triggered periodically
Scope
By default rules are triggered by changes to all resources.
Use the RuleScope
APIs (fromResource()
, fromResources()
or fromTag()
) to restrict
the scope of both managed and custom rules:
Function evalComplianceFn; ManagedRule sshRule = ManagedRule.Builder.create(this, "SSH") .identifier(ManagedRuleIdentifiers.EC2_SECURITY_GROUPS_INCOMING_SSH_DISABLED) .ruleScope(RuleScope.fromResource(ResourceType.EC2_SECURITY_GROUP, "sg-1234567890abcdefgh")) .build(); CustomRule customRule = CustomRule.Builder.create(this, "Lambda") .lambdaFunction(evalComplianceFn) .configurationChanges(true) .ruleScope(RuleScope.fromResources(List.of(ResourceType.CLOUDFORMATION_STACK, ResourceType.S3_BUCKET))) .build(); CustomRule tagRule = CustomRule.Builder.create(this, "CostCenterTagRule") .lambdaFunction(evalComplianceFn) .configurationChanges(true) .ruleScope(RuleScope.fromTag("Cost Center", "MyApp")) .build();
Evaluation Mode
You can specify the evaluation mode for a rule to determine when AWS Config runs evaluations.
Use the evaluationModes
property to specify the evaluation mode:
Function fn; String samplePolicyText; ManagedRule.Builder.create(this, "ManagedRule") .identifier(ManagedRuleIdentifiers.API_GW_XRAY_ENABLED) .evaluationModes(EvaluationMode.DETECTIVE_AND_PROACTIVE) .build(); CustomRule.Builder.create(this, "CustomRule") .lambdaFunction(fn) .evaluationModes(EvaluationMode.PROACTIVE) .build(); CustomPolicy.Builder.create(this, "CustomPolicy") .policyText(samplePolicyText) .evaluationModes(EvaluationMode.DETECTIVE) .build();
Note: Proactive evaluation mode is not supported for all rules. See AWS Config documentation for more information.
Events
You can define Amazon EventBridge event rules which trigger when a compliance check fails or when a rule is re-evaluated.
Use the onComplianceChange()
APIs to trigger an EventBridge event when a compliance check
of your AWS Config Rule fails:
// Topic to which compliance notification events will be published Topic complianceTopic = new Topic(this, "ComplianceTopic"); CloudFormationStackDriftDetectionCheck rule = new CloudFormationStackDriftDetectionCheck(this, "Drift"); rule.onComplianceChange("TopicEvent", OnEventOptions.builder() .target(new SnsTopic(complianceTopic)) .build());
Use the onReEvaluationStatus()
status to trigger an EventBridge event when an AWS Config
rule is re-evaluated.
// Topic to which re-evaluation notification events will be published Topic reEvaluationTopic = new Topic(this, "ComplianceTopic"); CloudFormationStackDriftDetectionCheck rule = new CloudFormationStackDriftDetectionCheck(this, "Drift"); rule.onReEvaluationStatus("ReEvaluationEvent", OnEventOptions.builder() .target(new SnsTopic(reEvaluationTopic)) .build());
Example
The 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. Function evalComplianceFn = Function.Builder.create(this, "CustomFunction") .code(AssetCode.fromInline("exports.handler = (event) => console.log(event);")) .handler("index.handler") .runtime(Runtime.NODEJS_18_X) .build(); // A custom rule that runs on configuration changes of EC2 instances CustomRule customRule = CustomRule.Builder.create(this, "Custom") .configurationChanges(true) .lambdaFunction(evalComplianceFn) .ruleScope(RuleScope.fromResource(ResourceType.EC2_INSTANCE)) .build(); // A rule to detect stack drifts CloudFormationStackDriftDetectionCheck driftRule = new CloudFormationStackDriftDetectionCheck(this, "Drift"); // Topic to which compliance notification events will be published Topic complianceTopic = new Topic(this, "ComplianceTopic"); // Send notification on compliance change events driftRule.onComplianceChange("ComplianceChange", OnEventOptions.builder() .target(new SnsTopic(complianceTopic)) .build());
-
ClassDescriptionChecks whether the active access keys are rotated within the number of days specified in
maxAge
.A fluent builder forAccessKeysRotated
.Construction properties for a AccessKeysRotated.A builder forAccessKeysRotatedProps
An implementation forAccessKeysRotatedProps
An object that represents the authorizations granted to aggregator accounts and regions.A fluent builder forCfnAggregationAuthorization
.Properties for defining aCfnAggregationAuthorization
.A builder forCfnAggregationAuthorizationProps
An implementation forCfnAggregationAuthorizationProps
A fluent builder forCfnConfigRule
.Indicates whether an AWS resource or AWS Config rule is compliant and provides the number of contributors that affect the compliance.A builder forCfnConfigRule.ComplianceProperty
An implementation forCfnConfigRule.ComplianceProperty
Provides the CustomPolicyDetails, the rule owner (AWS
for managed rules,CUSTOM_POLICY
for Custom Policy rules, andCUSTOM_LAMBDA
for Custom Lambda rules), the rule identifier, and the events that cause the evaluation of your AWS resources.A builder forCfnConfigRule.CustomPolicyDetailsProperty
An implementation forCfnConfigRule.CustomPolicyDetailsProperty
The configuration object for AWS Config rule evaluation mode.A builder forCfnConfigRule.EvaluationModeConfigurationProperty
An implementation forCfnConfigRule.EvaluationModeConfigurationProperty
Defines which resources trigger an evaluation for an AWS Config rule.A builder forCfnConfigRule.ScopeProperty
An implementation forCfnConfigRule.ScopeProperty
Provides the source and the message types that trigger AWS Config to evaluate your AWS resources against a rule.A builder forCfnConfigRule.SourceDetailProperty
An implementation forCfnConfigRule.SourceDetailProperty
Provides the CustomPolicyDetails, the rule owner (AWS
for managed rules,CUSTOM_POLICY
for Custom Policy rules, andCUSTOM_LAMBDA
for Custom Lambda rules), the rule identifier, and the events that cause the evaluation of your AWS resources.A builder forCfnConfigRule.SourceProperty
An implementation forCfnConfigRule.SourceProperty
Properties for defining aCfnConfigRule
.A builder forCfnConfigRuleProps
An implementation forCfnConfigRuleProps
The details about the configuration aggregator, including information about source accounts, regions, and metadata of the aggregator.A collection of accounts and regions.An implementation forCfnConfigurationAggregator.AccountAggregationSourceProperty
A fluent builder forCfnConfigurationAggregator
.This object contains regions to set up the aggregator and an IAM role to retrieve organization details.An implementation forCfnConfigurationAggregator.OrganizationAggregationSourceProperty
Properties for defining aCfnConfigurationAggregator
.A builder forCfnConfigurationAggregatorProps
An implementation forCfnConfigurationAggregatorProps
TheAWS::Config::ConfigurationRecorder
resource type describes the AWS resource types that AWS Config records for configuration changes.A fluent builder forCfnConfigurationRecorder
.Specifies whether the configuration recorder excludes certain resource types from being recorded.An implementation forCfnConfigurationRecorder.ExclusionByResourceTypesProperty
Specifies which resource types AWS Config records for configuration changes.A builder forCfnConfigurationRecorder.RecordingGroupProperty
An implementation forCfnConfigurationRecorder.RecordingGroupProperty
An object for you to specify your overrides for the recording mode.A builder forCfnConfigurationRecorder.RecordingModeOverrideProperty
An implementation forCfnConfigurationRecorder.RecordingModeOverrideProperty
Specifies the default recording frequency that AWS Config uses to record configuration changes.A builder forCfnConfigurationRecorder.RecordingModeProperty
An implementation forCfnConfigurationRecorder.RecordingModeProperty
Specifies the recording strategy of the configuration recorder.A builder forCfnConfigurationRecorder.RecordingStrategyProperty
An implementation forCfnConfigurationRecorder.RecordingStrategyProperty
Properties for defining aCfnConfigurationRecorder
.A builder forCfnConfigurationRecorderProps
An implementation forCfnConfigurationRecorderProps
A conformance pack is a collection of AWS Config rules and remediation actions that can be easily deployed in an account and a region.A fluent builder forCfnConformancePack
.Input parameters in the form of key-value pairs for the conformance pack, both of which you define.A builder forCfnConformancePack.ConformancePackInputParameterProperty
An implementation forCfnConformancePack.ConformancePackInputParameterProperty
This API allows you to create a conformance pack template with an AWS Systems Manager document (SSM document).A builder forCfnConformancePack.TemplateSSMDocumentDetailsProperty
An implementation forCfnConformancePack.TemplateSSMDocumentDetailsProperty
Properties for defining aCfnConformancePack
.A builder forCfnConformancePackProps
An implementation forCfnConformancePackProps
Specifies a delivery channel object to deliver configuration information to an Amazon S3 bucket and Amazon SNS topic.A fluent builder forCfnDeliveryChannel
.Provides options for how often AWS Config delivers configuration snapshots to the Amazon S3 bucket in your delivery channel.An implementation forCfnDeliveryChannel.ConfigSnapshotDeliveryPropertiesProperty
Properties for defining aCfnDeliveryChannel
.A builder forCfnDeliveryChannelProps
An implementation forCfnDeliveryChannelProps
Adds or updates an AWS Config rule for your entire organization to evaluate if your AWS resources comply with your desired configurations.A fluent builder forCfnOrganizationConfigRule
.An object that specifies metadata for your organization's AWS Config Custom Policy rule.An implementation forCfnOrganizationConfigRule.OrganizationCustomPolicyRuleMetadataProperty
An object that specifies organization custom rule metadata such as resource type, resource ID of AWS resource, Lambda function ARN, and organization trigger types that trigger AWS Config to evaluate your AWS resources against a rule.An implementation forCfnOrganizationConfigRule.OrganizationCustomRuleMetadataProperty
An object that specifies organization managed rule metadata such as resource type and ID of AWS resource along with the rule identifier.An implementation forCfnOrganizationConfigRule.OrganizationManagedRuleMetadataProperty
Properties for defining aCfnOrganizationConfigRule
.A builder forCfnOrganizationConfigRuleProps
An implementation forCfnOrganizationConfigRuleProps
OrganizationConformancePack deploys conformance packs across member accounts in an AWS Organizations .A fluent builder forCfnOrganizationConformancePack
.Input parameters in the form of key-value pairs for the conformance pack, both of which you define.An implementation forCfnOrganizationConformancePack.ConformancePackInputParameterProperty
Properties for defining aCfnOrganizationConformancePack
.A builder forCfnOrganizationConformancePackProps
An implementation forCfnOrganizationConformancePackProps
An object that represents the details about the remediation configuration that includes the remediation action, parameters, and data to execute the action.A fluent builder forCfnRemediationConfiguration
.An ExecutionControls object.A builder forCfnRemediationConfiguration.ExecutionControlsProperty
An implementation forCfnRemediationConfiguration.ExecutionControlsProperty
The value is either a dynamic (resource) value or a static value.An implementation forCfnRemediationConfiguration.RemediationParameterValueProperty
Example:A builder forCfnRemediationConfiguration.ResourceValueProperty
An implementation forCfnRemediationConfiguration.ResourceValueProperty
AWS Systems Manager (SSM) specific remediation controls.A builder forCfnRemediationConfiguration.SsmControlsProperty
An implementation forCfnRemediationConfiguration.SsmControlsProperty
Example:A builder forCfnRemediationConfiguration.StaticValueProperty
An implementation forCfnRemediationConfiguration.StaticValueProperty
Properties for defining aCfnRemediationConfiguration
.A builder forCfnRemediationConfigurationProps
An implementation forCfnRemediationConfigurationProps
Provides the details of a stored query.A fluent builder forCfnStoredQuery
.Properties for defining aCfnStoredQuery
.A builder forCfnStoredQueryProps
An implementation forCfnStoredQueryProps
Checks whether your CloudFormation stacks' actual configuration differs, or has drifted, from its expected configuration.A fluent builder forCloudFormationStackDriftDetectionCheck
.Construction properties for a CloudFormationStackDriftDetectionCheck.A builder forCloudFormationStackDriftDetectionCheckProps
An implementation forCloudFormationStackDriftDetectionCheckProps
Checks whether your CloudFormation stacks are sending event notifications to a SNS topic.A fluent builder forCloudFormationStackNotificationCheck
.Construction properties for a CloudFormationStackNotificationCheck.A builder forCloudFormationStackNotificationCheckProps
An implementation forCloudFormationStackNotificationCheckProps
A new custom policy.A fluent builder forCustomPolicy
.Construction properties for a CustomPolicy.A builder forCustomPolicyProps
An implementation forCustomPolicyProps
A new custom rule.A fluent builder forCustomRule
.Construction properties for a CustomRule.A builder forCustomRuleProps
An implementation forCustomRuleProps
The mode of evaluation for the rule.Interface representing an AWS Config rule.Internal default implementation forIRule
.A proxy class which represents a concrete javascript instance of this type.A new managed rule.A fluent builder forManagedRule
.Managed rules that are supported by AWS Config.Construction properties for a ManagedRule.A builder forManagedRuleProps
An implementation forManagedRuleProps
The maximum frequency at which the AWS Config rule runs evaluations.Resources types that are supported by AWS Config.Construction properties for a new rule.A builder forRuleProps
An implementation forRuleProps
Determines which resources trigger an evaluation of an AWS Config rule.