Start with one question when a CloudWatch Logs bill rises. Are you paying more to ingest logs, to keep them, or to query them? A retention policy addresses stored logs. If an application suddenly starts writing twice as much data every day, changing the retention period will leave that new ingestion volume alone.
This walkthrough inventories log groups by stored bytes, applies a 30-day policy to one disposable group, and verifies which retention policy actually governs that group.
Prerequisites
- AWS CLI v2 and credentials for a sandbox account. Use
aws sts get-caller-identityto confirm the account. - Permission to describe log groups and, for the lab, to create a group, set retention, and delete the lab group. Reading account-level policies also requires
logs:DescribeAccountPolicies. - One AWS Region to start. Repeat the read-only inventory in each Region where your workloads run.
Inventory by stored bytes
Start with a read-only command. Leave pagination enabled so the ranking includes every group returned in the Region.
REGION=us-east-1
aws logs describe-log-groups --region "$REGION" \
--query 'reverse(sort_by(logGroups,&storedBytes))[:15].{Name:logGroupName,Bytes:storedBytes,Days:retentionInDays,AccountPolicy:retentionPolicyName}' \
--output json
Look for large groups with a missing retention value, then confirm the effective setting in the console and find the workload owner. For each candidate, record its purpose, required incident lookback, and any audit or contractual retention requirement. Some groups need a longer investigation window even if they look expensive. A policy name in AccountPolicy indicates that a broader account policy governs expiry. AWS CLI reference for describing log groups.
Apply a policy to a canary log group
Use a new, empty group for this exercise. Keep the same shell open for the rest of the walkthrough so REGION and GROUP stay available. Continue only after the create command succeeds.
GROUP="/techmandra/lab/retention-$(date -u +%Y%m%dT%H%M%SZ)-$$"
aws logs create-log-group --region "$REGION" --log-group-name "$GROUP"
aws logs put-retention-policy --region "$REGION" \
--log-group-name "$GROUP" \
--retention-in-days 30
For a real workload, put the approved value in the system that owns the log group, such as the CloudFormation resource or Terraform configuration. That keeps the next deployment from resetting a change made through the CLI. Shortening retention can expire historical data, so get approval for the effective date and preserve records the team still needs before reducing a live group’s setting. AWS CLI reference for setting retention.
Verify the policy actually in effect
aws logs describe-log-groups --region "$REGION" \
--log-group-name-prefix "$GROUP" \
--query "logGroups[?logGroupName=='$GROUP'].{Name:logGroupName,Days:retentionInDays,AccountPolicy:retentionPolicyName}" \
--output json
The expected result for the canary is one group with Days set to 30 and no account policy name. If the group is covered by an account policy, CloudWatch can accept the group-level update while still reporting the account policy as effective. You can inspect account-level policies with the following read-only command if your role permits it.
aws logs describe-account-policies --region "$REGION" \
--policy-type RETENTION_POLICY
CloudWatch generally takes up to 72 hours to physically remove events after they reach the retention threshold and may take longer in rare cases. Events marked for deletion stop contributing to archival storage charges and are excluded from storedBytes. Recheck the effective setting, storage trend, and billing usage after the change. The canary verifies policy behavior, while measurements on an approved workload show whether retained volume actually fell. CloudWatch Logs retention and account policies.
Troubleshoot before rolling out
- The inventory cannot find the group. Check the account, Region, and exact group path. Log groups are Regional.
- The update succeeds but reports another number of days. Check
AccountPolicyand review its scope with the owner of the account policy. - The bill remains high. Compare ingest, archival storage, and Logs Insights usage separately. If ingest dominates, start by identifying the noisiest sources and reviewing which events the team still needs.
Use your Region’s rates when estimating savings. Multiply the projected reduction in billable stored volume by the applicable storage rate, then compare that estimate with actual billing data over a representative interval. Amazon CloudWatch pricing.
Clean up the canary
Delete the disposable group after the drill. Run this only in the same shell, after checking that GROUP is the empty lab group you just created.
printf 'Lab group: %s in %s\n' "$GROUP" "$REGION"
aws logs delete-log-group --region "$REGION" --log-group-name "$GROUP"
For the first production rollout, record the effective policy and baseline stored bytes for one approved group. Recheck both before expanding to other workloads.







Leave a Reply