Skip to content

Commit 577b077

Browse files
authored
feat: Add ECR Lifecycle Policy Option to docker-build module (#243)
1 parent 258e82b commit 577b077

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

examples/container-image/main.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,22 @@ module "docker_image" {
3838
build_args = {
3939
FOO = "bar"
4040
}
41+
ecr_repo_lifecycle_policy = <<EOF
42+
{
43+
"rules": [
44+
{
45+
"rulePriority": 1,
46+
"description": "Keep only the last 2 images",
47+
"selection": {
48+
"tagStatus": "any",
49+
"countType": "imageCountMoreThan",
50+
"countNumber": 2
51+
},
52+
"action": {
53+
"type": "expire"
54+
}
55+
}
56+
]
57+
}
58+
EOF
4159
}

modules/docker-build/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ No modules.
6161

6262
| Name | Type |
6363
|------|------|
64+
| [aws_ecr_lifecycle_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_lifecycle_policy) | resource |
6465
| [aws_ecr_repository.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_repository) | resource |
6566
| [docker_registry_image.this](https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/registry_image) | resource |
6667
| [aws_caller_identity.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
@@ -76,6 +77,7 @@ No modules.
7677
| <a name="input_docker_file_path"></a> [docker\_file\_path](#input\_docker\_file\_path) | Path to Dockerfile in source package | `string` | `"Dockerfile"` | no |
7778
| <a name="input_ecr_address"></a> [ecr\_address](#input\_ecr\_address) | Address of ECR repository for cross-account container image pulling (optional). Option `create_ecr_repo` must be `false` | `string` | `null` | no |
7879
| <a name="input_ecr_repo"></a> [ecr\_repo](#input\_ecr\_repo) | Name of ECR repository to use or to create | `string` | `null` | no |
80+
| <a name="input_ecr_repo_lifecycle_policy"></a> [ecr\_repo\_lifecycle\_policy](#input\_ecr\_repo\_lifecycle\_policy) | A JSON formatted ECR lifecycle policy to automate the cleaning up of unused images. | `string` | `null` | no |
7981
| <a name="input_ecr_repo_tags"></a> [ecr\_repo\_tags](#input\_ecr\_repo\_tags) | A map of tags to assign to ECR repository | `map(string)` | `{}` | no |
8082
| <a name="input_image_tag"></a> [image\_tag](#input\_image\_tag) | Image tag to use. If not specified current timestamp in format 'YYYYMMDDhhmmss' will be used. This can lead to unnecessary rebuilds. | `string` | `null` | no |
8183
| <a name="input_image_tag_mutability"></a> [image\_tag\_mutability](#input\_image\_tag\_mutability) | The tag mutability setting for the repository. Must be one of: `MUTABLE` or `IMMUTABLE` | `string` | `"MUTABLE"` | no |

modules/docker-build/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,10 @@ resource "aws_ecr_repository" "this" {
4141

4242
tags = var.ecr_repo_tags
4343
}
44+
45+
resource "aws_ecr_lifecycle_policy" "this" {
46+
count = var.ecr_repo_lifecycle_policy != null ? 1 : 0
47+
48+
policy = var.ecr_repo_lifecycle_policy
49+
repository = local.ecr_repo
50+
}

modules/docker-build/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ variable "build_args" {
5858
type = map(string)
5959
default = {}
6060
}
61+
62+
variable "ecr_repo_lifecycle_policy" {
63+
description = "A JSON formatted ECR lifecycle policy to automate the cleaning up of unused images."
64+
type = string
65+
default = null
66+
}

0 commit comments

Comments
 (0)