Skip to content

Commit f492095

Browse files
osvaldotCopilot
andauthored
refs sparkfabrik-innovation-team/board#3552 - Create ECR lifecycle po… (#64)
* refs sparkfabrik-innovation-team/board#3552 - Create ECR lifecycle policy * Update CHANGELOG.md --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent b904a0b commit f492095

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [4.5.0] - 2025-05-16
9+
10+
### Added
11+
12+
- Add repository lifecycle policy and `repository_expiration_days` variable
13+
814
## [4.4.0] - 2025-04-29
915

1016
[Compare with previous version](https://github.com/sparkfabrik/terraform-aws-eks-bootstrap/compare/4.3.0...4.4.0)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ The patches will add the special toleration to the resources, allowing them to b
103103
| <a name="input_private_subnet_ids"></a> [private\_subnet\_ids](#input\_private\_subnet\_ids) | n/a | `list(string)` | n/a | yes |
104104
| <a name="input_project"></a> [project](#input\_project) | Project name | `string` | n/a | yes |
105105
| <a name="input_prometheus_stack_additional_values"></a> [prometheus\_stack\_additional\_values](#input\_prometheus\_stack\_additional\_values) | Additional values for Kube Prometheus Stack | `list(string)` | `[]` | no |
106+
| <a name="input_repository_expiration_days"></a> [repository\_expiration\_days](#input\_repository\_expiration\_days) | Value to set the expiration days for the application repositories, null means no expiration | `number` | `null` | no |
106107
| <a name="input_velero_bucket_expiration_days"></a> [velero\_bucket\_expiration\_days](#input\_velero\_bucket\_expiration\_days) | n/a | `number` | `90` | no |
107108
| <a name="input_velero_bucket_glacier_days"></a> [velero\_bucket\_glacier\_days](#input\_velero\_bucket\_glacier\_days) | n/a | `number` | `60` | no |
108109
| <a name="input_velero_bucket_infrequently_access_days"></a> [velero\_bucket\_infrequently\_access\_days](#input\_velero\_bucket\_infrequently\_access\_days) | n/a | `number` | `30` | no |
@@ -130,6 +131,7 @@ The patches will add the special toleration to the resources, allowing them to b
130131

131132
| Name | Type |
132133
|------|------|
134+
| [aws_ecr_lifecycle_policy.project_image](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_lifecycle_policy) | resource |
133135
| [aws_ecr_repository.repository](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository) | resource |
134136
| [aws_iam_policy.aws_ebs_csi_driver](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
135137
| [aws_s3_bucket.velero](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |

ecr.tf

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,56 @@ locals {
55
customer_application_repositories = distinct(flatten([
66
for k, app in var.customer_application : [
77
for repo in app.repositories : {
8-
app_name = k
8+
app_name = k
99
repo_name = repo
1010
}
1111
]]))
1212
}
1313

1414
## Create ECR repository
1515
resource "aws_ecr_repository" "repository" {
16-
for_each = { for entry in local.customer_application_repositories : "${entry.app_name}-${entry.repo_name}" => entry }
17-
name = each.key
16+
for_each = { for entry in local.customer_application_repositories : "${entry.app_name}-${entry.repo_name}" => entry }
17+
name = each.key
1818

1919
tags = {
20-
Cluster = var.cluster_name
20+
Cluster = var.cluster_name
2121
Application = each.key
2222
}
2323
}
24+
25+
resource "aws_ecr_lifecycle_policy" "project_image" {
26+
for_each = var.repository_expiration_days != null ? { for entry in local.customer_application_repositories : "${entry.app_name}-${entry.repo_name}" => entry } : {}
27+
28+
repository = each.key
29+
30+
policy = jsonencode({
31+
rules = [
32+
{
33+
"rulePriority" : 1,
34+
"description" : "Keep image tagged with main, master, stage, dev*, review*",
35+
"selection" : {
36+
"tagStatus" : "tagged",
37+
"tagPrefixList" : ["main", "master", "stage", "dev*", "review*"],
38+
"countType" : "imageCountMoreThan",
39+
"countNumber" : 9999
40+
},
41+
"action" : {
42+
"type" : "expire"
43+
}
44+
},
45+
{
46+
"rulePriority" : 2,
47+
"description" : "Remove images older than ${var.repository_expiration_days} days",
48+
"selection" : {
49+
"tagStatus" : "any",
50+
"countType" : "sinceImagePushed",
51+
"countUnit" : "days",
52+
"countNumber" : var.repository_expiration_days
53+
},
54+
"action" : {
55+
"type" : "expire"
56+
}
57+
}
58+
]
59+
})
60+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,12 @@ variable "customer_application" {
302302
}))
303303
}
304304

305+
variable "repository_expiration_days" {
306+
type = number
307+
description = "Repository expiration days, used for lifecycle policy. Null to disable."
308+
default = null
309+
}
310+
305311
# Velero
306312
variable "enable_velero" {
307313
type = bool

0 commit comments

Comments
 (0)