Customize logs retention period in your CodeBuild project via Terraform

Myron Zaiets
Terraform/AWS tips
Published in
1 min readApr 4, 2023

Hi,

Now, it will be a quick story. When you add a CodeBuild project via Terraform, you will find this block:

logs_config {
cloudwatch_logs {
group_name = aws_cloudwatch_log_group.test.name
stream_name = "log-test

}
}

By default, it will set a retention period as “never expire”. And we don’t want it, so let’s customize it a bit. By setting up the CodeBuild project in Terraform, you can’t change the retention period for logs. There is a workaround to do that.

We will create a custom log group:

resource "aws_cloudwatch_log_group" "test" {
name = join("-", ["test", "logs"])
retention_in_days = 30
}

When we add it this way, we can change the retention period. If you already have a log group created and want to change a time, it’s required to import a log group to a terraform state. You can do this by using this command, in our case:

terraform import aws_cloudwatch_log_group.test test-logs

aws_cloudwatch_log_group.test is a name in Terraform

test-logs is a name already created log group in an AWS console

That’s all.

Have a nice day.

--

--