Skip to content

Commit 91b586c

Browse files
ivan-pinattiaknysh
authored andcommitted
Added lifecycle policy (#7)
* Added lifecycle policy * Renamed variable from rotate_number to max_image_number * Applied terraform fmt * Renamed to max_image_count + updated README * Modified the logic to be tagged by default * Applied Terraform fmt
1 parent 168c499 commit 91b586c

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module "ecr" {
4040
| `stage` | `default` | Stage (e.g. `prod`, `dev`, `staging`) | Yes |
4141
| `name` | `admin` | The Name of the application or solution (e.g. `bastion` or `portal`) | Yes |
4242
| `roles` | `[]` | List of IAM role names that will be granted permissions to use the container registry | No (optional) |
43+
| `max_image_count` | `7` | How many Docker Image versions AWS ECR will store | Yes |
4344

4445

4546
## Outputs

main.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,25 @@ resource "aws_iam_instance_profile" "default" {
143143
name = "${module.label.id}"
144144
role = "${aws_iam_role.default.name}"
145145
}
146+
147+
resource "aws_ecr_lifecycle_policy" "default" {
148+
repository = "${aws_ecr_repository.default.name}"
149+
150+
policy = <<EOF
151+
{
152+
"rules": [{
153+
"rulePriority": 1,
154+
"description": "Rotate images when reach ${var.max_image_count} images stored",
155+
"selection": {
156+
"tagStatus": "tagged",
157+
"tagPrefixList": ["${var.stage}"],
158+
"countType": "imageCountMoreThan",
159+
"countNumber": ${var.max_image_count}
160+
},
161+
"action": {
162+
"type": "expire"
163+
}
164+
}]
165+
}
166+
EOF
167+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ variable "tags" {
2424
type = "map"
2525
default = {}
2626
}
27+
28+
variable "max_image_count" {
29+
type = "string"
30+
description = "How many Docker Image versions AWS ECR will store"
31+
default = "7"
32+
}

0 commit comments

Comments
 (0)