OnCallReadyStart learning

AWS Lesson 37.24 · AWS IV: Operations Automation · 22 min read

AWS Config: recording, rules, remediation and conformance packs

In plain words

AWS Config is a building inspector with a camera and a clipboard. Every time something in the account changes, it takes a photo, so you can see what any door or window looked like on any day. The clipboard holds the rules: every bucket must keep old versions, no port 22 open to the street, every box labelled with its team.

When the inspector finds a broken rule, it can call a repair robot - a runbook - to fix it, either straight away or after a person agrees.

AWS Config: recording, rules, remediation and conformance packs

CloudTrail tells you who did what. AWS Config tells you what things look like - and what they looked like last Tuesday. It records a configuration item every time a supported resource changes (a bucket's versioning, a security group's rules, an instance's profile), keeps the history, and evaluates rules against it: "every bucket is versioned", "no security group opens SSH to the world", "every resource has a team tag". A rule that finds a non-compliant resource can start a remediation - a Systems Manager runbook - by itself. That is "compliance as code": the policy is a rule, the evidence is the history, the fix is a runbook.

Need to know: Config is per Region and off until you turn it on: a configuration recorder (what to record, with the service-linked role AWSServiceRoleForConfig), a delivery channel (an S3 bucket - whose policy must let config.amazonaws.com write - and optionally an SNS topic), and starting the recorder. Managed rules are AWS-written checks referenced by an identifier (S3_BUCKET_VERSIONING_ENABLED), run on configuration changes or periodically; custom rules are Lambda functions or Guard policies. You pay per configuration item recorded and per rule evaluation - recording everything in a busy account is not free.

Turning it on

A rule needs something to evaluate, and Config refuses to pretend:

$ cd ~/oncall-lab/labs/aws/try4
$ aws configservice put-config-rule --config-rule '{"ConfigRuleName":"s3-versioning","Source":{"Owner":"AWS","SourceIdentifier":"S3_BUCKET_VERSIONING_ENABLED"}}'
aws: [ERROR]: An error occurred (NoAvailableConfigurationRecorderException) when calling the PutConfigRule operation: Configuration recorder is not available to put config rule.
$ aws configservice put-configuration-recorder --configuration-recorder name=default,roleARN=arn:aws:iam::111122223333:role/aws-service-role/config.amazonaws.com/AWSServiceRoleForConfig --recording-group allSupported=true,includeGlobalResourceTypes=true
$ aws configservice put-delivery-channel --delivery-channel name=default,s3BucketName=oncall-try-config-111122223333
aws: [ERROR]: An error occurred (InsufficientDeliveryPolicyException) when calling the PutDeliveryChannel operation: Insufficient delivery policy to s3 bucket: oncall-try-config-111122223333, unable to write to bucket, provided s3 key prefix is 'null'.

The delivery channel is checked when you create it: Config tries the bucket and fails if its policy does not let config.amazonaws.com check the ACL and write under AWSLogs/<account>/Config/. The policy (with AWS:SourceAccount, so no other account's Config can write here):

$ aws s3api put-bucket-policy --bucket oncall-try-config-111122223333 --policy file://config-bucket-policy.json
$ aws configservice put-delivery-channel --delivery-channel name=default,s3BucketName=oncall-try-config-111122223333
$ aws configservice start-configuration-recorder --configuration-recorder-name default
$ aws configservice describe-configuration-recorder-status --query 'ConfigurationRecordersStatus[0].[name,recording,lastStatus]' --output text
default	True	SUCCESS
$ aws configservice get-discovered-resource-counts --query 'resourceCounts[?starts_with(resourceType, `AWS::S3`) || starts_with(resourceType, `AWS::EC2::Security`)]' --output text
2	AWS::S3::Bucket
2	AWS::EC2::SecurityGroup

includeGlobalResourceTypes=true records IAM users, roles and policies too - turn it on in one Region only, or every Region records (and bills) the same IAM changes. Each resource now has a history, oldest first:

$ aws configservice get-resource-config-history --resource-type AWS::S3::Bucket --resource-id oncall-try-assets-111122223333 --query 'configurationItems[].[configurationItemCaptureTime,configurationItemStatus]' --output text
2026-09-22T20:00:04.700000+00:00	ResourceDiscovered

Rules and compliance

Three managed rules: versioning on buckets, SSH not open to the world, and a required tag. The scope limits a rule to resource types (or tags); InputParameters configure it:

$ aws configservice put-config-rule --config-rule '{"ConfigRuleName":"s3-versioning","Source":{"Owner":"AWS","SourceIdentifier":"S3_BUCKET_VERSIONING_ENABLED"},"Scope":{"ComplianceResourceTypes":["AWS::S3::Bucket"]}}'
$ aws configservice put-config-rule --config-rule '{"ConfigRuleName":"restricted-ssh","Source":{"Owner":"AWS","SourceIdentifier":"INCOMING_SSH_DISABLED"}}'
$ aws configservice put-config-rule --config-rule '{"ConfigRuleName":"required-tags","Source":{"Owner":"AWS","SourceIdentifier":"REQUIRED_TAGS"},"InputParameters":"{\"tag1Key\":\"team\"}","Scope":{"ComplianceResourceTypes":["AWS::S3::Bucket"]}}'
$ sleep 5; aws configservice describe-compliance-by-config-rule --query 'ComplianceByConfigRules[].[ConfigRuleName,Compliance.ComplianceType,Compliance.ComplianceContributorCount.CappedCount]' --output text
s3-versioning	NON_COMPLIANT	2
restricted-ssh	NON_COMPLIANT	1
required-tags	NON_COMPLIANT	2
$ aws configservice get-compliance-details-by-config-rule --config-rule-name restricted-ssh --compliance-types NON_COMPLIANT --query 'EvaluationResults[].[EvaluationResultIdentifier.EvaluationResultQualifier.ResourceId,Annotation]' --output text
sg-0e3e495101ef96a9e	The security group allows inbound SSH (port 22) from 0.0.0.0/0 or ::/0.
$ aws configservice get-compliance-details-by-config-rule --config-rule-name s3-versioning --compliance-types NON_COMPLIANT --query 'EvaluationResults[].[EvaluationResultIdentifier.EvaluationResultQualifier.ResourceId,Annotation]' --output text
oncall-try-assets-111122223333	Bucket versioning is not enabled.
oncall-try-config-111122223333	Bucket versioning is not enabled.

The rule's compliance is the worst of its resources: one NON_COMPLIANT resource makes the rule NON_COMPLIANT. The other direction answers "what is wrong with this one thing":

$ aws configservice get-compliance-details-by-resource --resource-type AWS::S3::Bucket --resource-id oncall-try-assets-111122223333 --query 'EvaluationResults[].[EvaluationResultIdentifier.EvaluationResultQualifier.ConfigRuleName,ComplianceType]' --output text
s3-versioning	NON_COMPLIANT
required-tags	NON_COMPLIANT

A change-triggered rule re-evaluates a resource when its configuration item changes; a periodic one (IAM_PASSWORD_POLICY, CLOUD_TRAIL_ENABLED) every 1, 3, 6, 12 or 24 hours. start-config-rules-evaluation forces a run when you cannot wait.

Remediation: the rule fixes what it finds

A remediation configuration attaches an SSM Automation runbook to a rule. Its parameters are static values or the non-compliant resource's ID (RESOURCE_ID); it runs with an AutomationAssumeRole that trusts ssm.amazonaws.com. Manual remediation waits for someone to press the button (or call start-remediation-execution); automatic runs on every non-compliant evaluation, with MaximumAutomaticAttempts and RetryAttemptSeconds:

$ aws iam create-role --role-name try-config-remediation --assume-role-policy-document file://ssm-trust.json --query Role.Arn --output text
arn:aws:iam::111122223333:role/try-config-remediation
$ aws iam put-role-policy --role-name try-config-remediation --policy-name remediate --policy-document file://remediate-policy.json
$ aws configservice put-remediation-configurations --remediation-configurations '[{"ConfigRuleName":"restricted-ssh","TargetType":"SSM_DOCUMENT","TargetId":"AWS-DisablePublicAccessForSecurityGroup","Parameters":{"AutomationAssumeRole":{"StaticValue":{"Values":["arn:aws:iam::111122223333:role/try-config-remediation"]}},"GroupId":{"ResourceValue":{"Value":"RESOURCE_ID"}}},"Automatic":false}]' --query FailedBatches
[]
$ SG=$(aws ec2 describe-security-groups --filters Name=group-name,Values=try-legacy-admin --query 'SecurityGroups[0].GroupId' --output text)
$ aws configservice start-remediation-execution --config-rule-name restricted-ssh --resource-keys resourceType=AWS::EC2::SecurityGroup,resourceId=$SG --query FailedItems
[]
$ sleep 10; aws configservice describe-remediation-execution-status --config-rule-name restricted-ssh --query 'RemediationExecutionStatuses[].[ResourceKey.resourceId,State,StepDetails[0].Name]' --output text
sg-0e3e495101ef96a9e	SUCCEEDED	DisableSSHFromIpV4
$ aws ec2 describe-security-groups --group-ids $SG --query 'SecurityGroups[0].IpPermissions[].[FromPort,IpRanges[0].CidrIp]' --output text
443	0.0.0.0/0

Port 443 stayed; only SSH (and RDP, had it been open) went. For the buckets, automatic - a bucket without versioning is never right here:

$ aws configservice put-remediation-configurations --remediation-configurations '[{"ConfigRuleName":"s3-versioning","TargetType":"SSM_DOCUMENT","TargetId":"AWS-ConfigureS3BucketVersioning","Parameters":{"AutomationAssumeRole":{"StaticValue":{"Values":["arn:aws:iam::111122223333:role/try-config-remediation"]}},"BucketName":{"ResourceValue":{"Value":"RESOURCE_ID"}}},"Automatic":true,"MaximumAutomaticAttempts":5,"RetryAttemptSeconds":60}]' --query FailedBatches
[]
$ sleep 15; aws s3api get-bucket-versioning --bucket oncall-try-assets-111122223333 --query Status --output text
Enabled
$ aws configservice get-compliance-details-by-config-rule --config-rule-name s3-versioning --query 'EvaluationResults[?EvaluationResultIdentifier.EvaluationResultQualifier.ResourceId==`oncall-try-assets-111122223333`].ComplianceType' --output text
COMPLIANT
$ aws configservice get-resource-config-history --resource-type AWS::S3::Bucket --resource-id oncall-try-assets-111122223333 --query 'length(configurationItems)'
2

The fix is itself a change, so it is a new configuration item, and the re-evaluation turns the bucket COMPLIANT. Automatic remediation is powerful and blunt: it runs whoever made the change and whatever they intended - including the Terraform pipeline that owns the bucket and is about to set it back. Use it where there is exactly one right answer, and make the owning tool agree.

Conformance packs, aggregators, queries

A conformance pack deploys a set of rules (and remediations) as one unit from a template - AWS publishes packs for CIS, PCI DSS, NIST and "Operational Best Practices for ...". In an organization it is deployed to every account from the management or delegated administrator account. The rule names get a suffix so packs never collide with your own rules:

$ cat pack.yaml
Resources:
  BucketVersioning:
    Type: AWS::Config::ConfigRule
    Properties:
      ConfigRuleName: s3-bucket-versioning-enabled
      Source:
        Owner: AWS
        SourceIdentifier: S3_BUCKET_VERSIONING_ENABLED
  CloudTrailEnabled:
    Type: AWS::Config::ConfigRule
    Properties:
      ConfigRuleName: cloudtrail-enabled
      Source:
        Owner: AWS
        SourceIdentifier: CLOUD_TRAIL_ENABLED
  PasswordPolicy:
    Type: AWS::Config::ConfigRule
    Properties:
      ConfigRuleName: iam-password-policy
      InputParameters:
        MinimumPasswordLength: '14'
      Source:
        Owner: AWS
        SourceIdentifier: IAM_PASSWORD_POLICY
$ aws configservice put-conformance-pack --conformance-pack-name try-baseline --template-body file://pack.yaml --query ConformancePackArn --output text
arn:aws:config:eu-central-1:111122223333:conformance-pack/try-baseline/conformance-pack-463656919
$ sleep 5; aws configservice describe-conformance-pack-compliance --conformance-pack-name try-baseline --query 'ConformancePackRuleComplianceList[].[ConfigRuleName,ComplianceType]' --output text
s3-bucket-versioning-enabled-conformance-pack-fc0792d3	COMPLIANT
cloudtrail-enabled-conformance-pack-fc0792d3	NON_COMPLIANT
iam-password-policy-conformance-pack-fc0792d3	NON_COMPLIANT

The versioning rule is already COMPLIANT - the automatic remediation fixed both buckets a minute ago. cloudtrail-enabled reports whether this Region has a logging trail (AWS III's trail, if your box still has it). iam-password-policy fails because the account has no password policy at all (IAM users with console passwords may then use any password AWS accepts). Setting one is a single call - and a 14-character minimum is what CIS asks for:

$ aws iam get-account-password-policy
aws: [ERROR]: An error occurred (NoSuchEntity) when calling the GetAccountPasswordPolicy operation: The Password Policy with domain name 111122223333 cannot be found.
$ aws iam update-account-password-policy --minimum-password-length 14 --require-symbols --require-numbers --require-uppercase-characters --require-lowercase-characters --password-reuse-prevention 24 --max-password-age 90
$ aws configservice start-config-rules-evaluation --config-rule-names $(aws configservice describe-config-rules --query 'ConfigRules[?starts_with(ConfigRuleName, `iam-password-policy`)].ConfigRuleName' --output text); sleep 5
$ aws configservice describe-compliance-by-config-rule --config-rule-names $(aws configservice describe-config-rules --query 'ConfigRules[?starts_with(ConfigRuleName, `iam-password-policy`)].ConfigRuleName' --output text) --query 'ComplianceByConfigRules[0].Compliance.ComplianceType' --output text
COMPLIANT

An aggregator collects configuration and compliance from many accounts and Regions into one (the security or audit account): --account-aggregation-sources for a list of accounts, or --organization-aggregation-source for the whole organization. Advanced queries run SQL over the recorded configuration - in one account, or across an aggregator:

$ aws configservice put-configuration-aggregator --configuration-aggregator-name try-all --account-aggregation-sources AccountIds=111122223333,AllAwsRegions=true --query 'ConfigurationAggregator.ConfigurationAggregatorName' --output text
try-all
$ aws configservice select-resource-config --expression "SELECT resourceId, resourceType WHERE resourceType = 'AWS::EC2::SecurityGroup' AND configuration.groupName = 'try-legacy-admin'" --query Results --output text
{"resourceId":"sg-0e3e495101ef96a9e","resourceType":"AWS::EC2::SecurityGroup"}

Config, CloudTrail, Security Hub - which one?

questionservice
who deleted the bucket policy, from which IP?CloudTrail
what did the security group look like before Tuesday's change?Config (history)
is every bucket versioned, right now and continuously?Config (rule)
put it back automatically when it driftsConfig remediation (or EventBridge -> runbook)
one score across Config, GuardDuty, Inspector and the CIS checksSecurity Hub (later in this chapter)

On the exam (SOA-C03 4.1, 3.2, 1.2; SAA-C03 1.x): "implement and manage security and compliance tools and policies" - Config rules (managed, change-triggered vs periodic), remediation with SSM Automation (manual vs automatic, the remediation role), conformance packs, aggregators, the delivery channel's bucket policy, and the IAM password policy. The classic distractor: "use CloudTrail to check whether resources are compliant" - that is Config's job.

In an interview: "How would you make sure no S3 bucket in the company is ever left without versioning?" - "An AWS Config managed rule, S3_BUCKET_VERSIONING_ENABLED, deployed to every account through a conformance pack from the delegated administrator, with an automatic remediation that runs AWS-ConfigureS3BucketVersioning through a role that can only do that. An aggregator in the security account shows compliance across the organization - and I'd make sure Terraform modules enable versioning too, so the remediation and the IaC never fight."

You can now: turn Config on with a recorder, a delivery channel and the bucket policy it needs, read a resource's configuration history, add managed rules with scope and parameters, read compliance by rule and by resource, attach manual and automatic remediation with a role, deploy a conformance pack, set the account password policy, aggregate across accounts and Regions, query the recorded configuration with SQL, and say when to use Config instead of CloudTrail.

Why it helps

Auditors want evidence, not promises, and on-call engineers want to know what a resource looked like before last Tuesday's change. AWS Config gives both: configuration history per resource and continuous compliance against rules. The lesson sets up the recorder and the delivery channel (with the bucket policy that usually trips people up), managed rules with scope and parameters, automatic and manual remediation with SSM runbooks, conformance packs, aggregators across accounts, advanced queries, and the IAM password policy - and places Config next to CloudTrail and Security Hub.

Commands in this lesson

cd aws sleep cat

FAQ

Why does put-delivery-channel fail with InsufficientDeliveryPolicyException?

Config checks the bucket when you create the channel. Its policy must let config.amazonaws.com read the bucket ACL, list the bucket, and put objects under AWSLogs/<account>/Config/ with the bucket-owner-full-control ACL. Add those statements, ideally with an AWS:SourceAccount condition, and create the channel again.

What is the difference between change-triggered and periodic rules?

A change-triggered rule evaluates a resource whenever its configuration item changes, so it reacts within minutes. A periodic rule runs on a schedule - every 1, 3, 6, 12 or 24 hours - for checks that are not about one resource changing, such as the account's password policy or whether a trail exists.

Does Config fix things by itself?

Only with a remediation configuration. Config runs an SSM Automation runbook for a non-compliant resource, either automatically with retries or manually when someone starts it. The runbook needs an AutomationAssumeRole with the permissions to fix the resource, and the fix is recorded as a new configuration item.

Why record global resource types in one Region only?

IAM users, roles and policies are global, so every Region that records global types records the same changes again and bills for them. Turn includeGlobalResourceTypes on in your home Region and off everywhere else; rules about IAM then run in that Region.

What is a conformance pack?

A set of Config rules, and optionally remediations, deployed together from one template. AWS publishes packs for CIS, PCI DSS, NIST and operational best practices. In an organization a conformance pack is deployed to every account from the management account or a delegated administrator, and its rule names get a suffix so they never collide.

In an interview Mid

How would you make sure no S3 bucket in the company is ever left without versioning?

An AWS Config managed rule, S3_BUCKET_VERSIONING_ENABLED, deployed to every account through a conformance pack from the delegated administrator, with an automatic remediation that runs AWS-ConfigureS3BucketVersioning through a role that can only do that. An aggregator in the security account shows compliance across the organization. And the Terraform or CloudFormation modules enable versioning too, so the remediation and the IaC never fight over the same setting.

Also asked: What is the difference between AWS Config and CloudTrail? · When would you choose manual over automatic remediation? · What does a Config aggregator do?

Practise this lesson in the terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.