2
votes

CodeBuild Logs Config - CloudWatch supports Time-based log retention.

For CodeBuild logs - Can we achieve Log Retention which says like - "Retain the latest 3 Successful build logs" + "Retain the latest 3 Failed build logs" ?

3

3 Answers

0
votes

Unfortunately, no. This feature isn't supported natively in CodeBuild.

The workaround would be to set the retention policy for your log group associated with your CodeBuild project on the CloudWatch console though.

0
votes

In my case I use this method:

  • In the same file .tf I created the resource loggroup with retention:
resource "aws_cloudwatch_log_group" "loggroup" {
  name = "/aws/codebuild/test"
  retention_in_days = 30
}
  • And the I pass the variable to my codebuild project:
    logs_config {
    cloudwatch_logs {
      group_name  = aws_cloudwatch_log_group.loggroup.name
    }
  }

And then I apply my terraform code.

0
votes

More for future reference here on how to configure time-based retention periods in CloudFormation since there is already a TerraForm example :

AWSTemplateFormatVersion: "2010-09-09"
Resources:
  # CodeBuild
  # ------------------------------------------------------------
  CodeBuildProject:
    Type: AWS::CodeBuild::Project
  # Logs
  # ------------------------------------------------------------
  LogsLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub "/aws/codebuild/${CodeBuildProject}"
      RetentionInDays: 7

Reference : https://medium.com/allermedia-techblog/cloudformation-example-log-retention-for-lambda-and-codebuild-a11e74516bb6

If you want to retain loggroups like you asked you'd have to do it yourself with a Lambda function.