Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforcing CloudWatch log group retention period throughout the AWS Organization #670

Open
robertcurcio opened this issue Dec 5, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@robertcurcio
Copy link

robertcurcio commented Dec 5, 2024

Is your feature request related to a problem? Please describe.

We would like to enforce the CloudWatch Log retention period throughout the AWS Organization. We have a lot of older CloudWatch log groups that have to be manually modified when we change our global retention settings.

  • Each CloudWatch log group(s) need their retention policies updated after the modification of the Global-config.yaml cloudwatchLogRetentionInDays has modified. Only newly created CloudWatch log groups receive the new log retention policy.

I can see the AWS Lambda function in every account "AWSAccelerator-LoggingSta-NewCloudWatchLogsCreate" adds the our global retention policy onto newly created CloudWatch log groups. This function is triggered by the event bridge rule "CreateLogGroup" from CloudTrail. Unfortunately, it does not modify existing log groups.

Describe the feature you'd like

  • A new event bus rule which triggers AWSAccelerator-LoggingSta-NewCloudWatchLogsCreate when it sees PutRetentionPolicy . This allows log group retention policy modifications by an AWS engineer to be reverted back to the global config.

  • A scheduled job running periodically triggering function "AWSAccelerator-LoggingSta-NewCloudWatchLogsCreate" to find and remediates modified out of scope retention policies, and revers them back to the Global configuration

@robertcurcio robertcurcio added the enhancement New feature or request label Dec 5, 2024
@richardkeit
Copy link
Contributor

Hello @robertcurcio ,

For restricting modification of LogGroups, suggest that a SCP is created to restrict modification. Note: Should allow the principals that the solution runs as (AND if you want your own internal automation roles to have this ability).

Correct, there is a custom resource that modifies new log groups, there is also another custom resource which modifies existing log groups.

// Run a custom resource to update subscription, KMS and retention for all existing log groups
const customResourceExistingLogs = new CloudWatchLogsSubscriptionFilter(this, 'LogsSubscriptionFilter', {
logDestinationArn: logsDestinationArnValue,
logsKmsKey: this.cloudwatchKey,
logArchiveAccountId: this.props.accountsConfig.getLogArchiveAccountId(),
logsRetentionInDays: this.props.globalConfig.cloudwatchLogRetentionInDays.toString(),
subscriptionFilterRoleArn: subscriptionFilterRole.roleArn,
logExclusionOption: accountRegionExclusion,
replaceLogDestinationArn: this.props.globalConfig.logging.cloudwatchLogs?.replaceLogDestinationArn,
acceleratorPrefix: this.props.prefixes.accelerator,
useExistingRoles: this.props.useExistingRoles ?? false,
});

It is important to note, for log groups: the code will not update the log groups if the existing retention is longer than the default retention - ie in summary, the higher retention wins

async function updateRetentionPolicy(acceleratorRetentionInDays: number, logGroup: LogGroup) {
const currentRetentionInDays = logGroup.retentionInDays;
if (!currentRetentionInDays) {
return;
}
if (acceleratorRetentionInDays > currentRetentionInDays) {
await throttlingBackOff(() =>
logsClient.send(
new PutRetentionPolicyCommand({
logGroupName: logGroup.logGroupName!,
retentionInDays: acceleratorRetentionInDays,
}),
),
);
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants