Skip to content

Commit

Permalink
Added lifecycle policy (#7)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ivan-pinatti authored and aknysh committed Jan 31, 2018
1 parent 168c499 commit 91b586c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module "ecr" {
| `stage` | `default` | Stage (e.g. `prod`, `dev`, `staging`) | Yes |
| `name` | `admin` | The Name of the application or solution (e.g. `bastion` or `portal`) | Yes |
| `roles` | `[]` | List of IAM role names that will be granted permissions to use the container registry | No (optional) |
| `max_image_count` | `7` | How many Docker Image versions AWS ECR will store | Yes |


## Outputs
Expand Down
22 changes: 22 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,25 @@ resource "aws_iam_instance_profile" "default" {
name = "${module.label.id}"
role = "${aws_iam_role.default.name}"
}

resource "aws_ecr_lifecycle_policy" "default" {
repository = "${aws_ecr_repository.default.name}"

policy = <<EOF
{
"rules": [{
"rulePriority": 1,
"description": "Rotate images when reach ${var.max_image_count} images stored",
"selection": {
"tagStatus": "tagged",
"tagPrefixList": ["${var.stage}"],
"countType": "imageCountMoreThan",
"countNumber": ${var.max_image_count}
},
"action": {
"type": "expire"
}
}]
}
EOF
}
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ variable "tags" {
type = "map"
default = {}
}

variable "max_image_count" {
type = "string"
description = "How many Docker Image versions AWS ECR will store"
default = "7"
}

0 comments on commit 91b586c

Please sign in to comment.