Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing ECR lifecycle policy as a variable #110

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ data "aws_iam_policy_document" "additional_eks" {
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_enable_registry_scanning"></a> [enable\_registry\_scanning](#input\_enable\_registry\_scanning) | Whether to enable continuous registry scanning | `bool` | n/a | yes |
| <a name="input_max_tagged_image_count"></a> [max\_tagged\_image\_count](#input\_max\_tagged\_image\_count) | The maximum number of tagged images to keep for each repository | `number` | n/a | yes |
| <a name="input_max_tagged_image_count"></a> [max\_tagged\_image\_count](#input\_max\_tagged\_image\_count) | The maximum number of tagged images to keep for each repository | `number` | `100` | no |
| <a name="input_max_untagged_image_count"></a> [max\_untagged\_image\_count](#input\_max\_untagged\_image\_count) | The maximum number of untagged images to keep for each repository | `number` | `1` | no |
| <a name="input_pull_accounts"></a> [pull\_accounts](#input\_pull\_accounts) | List of accounts that can pull | `list(string)` | n/a | yes |
| <a name="input_pull_and_push_accounts"></a> [pull\_and\_push\_accounts](#input\_pull\_and\_push\_accounts) | List of accounts that can pull and push | `list(string)` | n/a | yes |
Expand Down
33 changes: 33 additions & 0 deletions aws/modules/infrastructure_modules/container_registry/locals.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
locals {
pull_through_cache_accounts = length(var.pull_through_cache_accounts) > 0 ? var.pull_through_cache_accounts : ["arn:aws:iam::${data.aws_caller_identity.this.account_id}:root"]

repository_lifecycle_default_policy = jsonencode({
rules = [
{
rulePriority = 1
description = "Keep the last '${var.max_untagged_image_count}' untagged images"

selection = {
tagStatus = "untagged"
countType = "imageCountMoreThan"
countNumber = var.max_untagged_image_count
}

action = {
type = "expire"
}
},
{
rulePriority = 2,
description = "Keep the last '${var.max_tagged_image_count}' tagged images"

selection = {
tagStatus = "any"
countType = "imageCountMoreThan"
countNumber = var.max_tagged_image_count
}

action = {
type = "expire"
}
},
]
})
}
33 changes: 1 addition & 32 deletions aws/modules/infrastructure_modules/container_registry/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,7 @@ module "ecr" {
# Managed below in `ecr_registry_scanning_rules`
manage_registry_scanning_configuration = false

repository_lifecycle_policy = jsonencode({
rules = [
{
rulePriority = 1
description = "Keep the last '${var.max_untagged_image_count}' untagged images"

selection = {
tagStatus = "untagged"
countType = "imageCountMoreThan"
countNumber = var.max_untagged_image_count
}

action = {
type = "expire"
}
},
{
rulePriority = 2,
description = "Keep the last '${var.max_tagged_image_count}' tagged images"

selection = {
tagStatus = "any"
countType = "imageCountMoreThan"
countNumber = var.max_tagged_image_count
}

action = {
type = "expire"
}
},
]
})
repository_lifecycle_policy = var.repository_lifecycle_policy == "default-policy" ? local.repository_lifecycle_default_policy : var.repository_lifecycle_policy
}

## Pull Through Cache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ variable "max_untagged_image_count" {
variable "max_tagged_image_count" {
type = number
description = "The maximum number of tagged images to keep for each repository"

default = 100
}

variable "pull_accounts" {
Expand Down Expand Up @@ -64,3 +66,9 @@ variable "pull_through_cache_accounts" {
description = "A default list of accounts for the Pull Through Cache if not configured in the `pull_through_cache_setup`. Defaults to the calling account root"
default = []
}

variable "repository_lifecycle_policy" {
type = any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This possibly should be a strongly typed object and if null is passed use the default?

description = "ECR repository lifestyle policy rules"
default = "default-policy"
}