Skip to content

Commit 4092608

Browse files
author
Valentin Khramtsov
committed
Changes for ECR module
1 parent 9f1815a commit 4092608

File tree

8 files changed

+98
-105
lines changed

8 files changed

+98
-105
lines changed

terraform/modules/aws-ecr/main.tf

+27-22
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11

22
resource "aws_ecr_repository" "this" {
3-
count = var.create_ecr_repository ? 1 : 0
4-
name = var.name
5-
image_tag_mutability = var.image_tag_mutability
3+
for_each = var.repositories
4+
5+
name = each.key
6+
image_tag_mutability = each.value.image_tag_mutability
67
image_scanning_configuration {
7-
scan_on_push = var.scan_on_push
8+
scan_on_push = each.value.scan_on_push
89
}
910

1011
tags = var.tags
1112
}
1213

1314
resource "aws_ecr_lifecycle_policy" "this" {
14-
count = var.create_ecr_repository ? 1 : 0
15-
repository = aws_ecr_repository.this[0].name
16-
policy = <<POLICY
17-
{
18-
"rules": [
19-
{
20-
"rulePriority": 1,
21-
"description": "Expire untagged images older than 14 days",
22-
"selection": {
23-
"tagStatus": "untagged",
24-
"countType": "sinceImagePushed",
25-
"countUnit": "days",
26-
"countNumber": 14
15+
for_each = { for k, v in var.repositories : k => v if length(v.lifecycle_policies) > 0 }
16+
17+
repository = aws_ecr_repository.this[each.key].name
18+
19+
policy = jsonencode({
20+
rules = [
21+
for policy in each.value.lifecycle_policies : merge({
22+
rulePriority = index(each.value.lifecycle_policies, policy) + 1
23+
description = policy.description
24+
selection = merge({
25+
tagStatus = policy.tag_status
26+
countType = "sinceImagePushed"
27+
countUnit = policy.count_unit
28+
countNumber = policy.count_number
2729
},
28-
"action": {
29-
"type": "expire"
30+
# Conditionally add tagPrefixList only if tag_status is "tagged"
31+
policy.tag_status == "tagged" ? {
32+
tagPrefixList = "${policy.tagPrefixLists}"
33+
} : {})
34+
action = {
35+
type = "expire"
3036
}
31-
}
37+
})
3238
]
33-
}
34-
POLICY
39+
})
3540
}

terraform/modules/aws-ecr/outputs.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
output "ecr_repository_url" {
2-
value = var.create_ecr_repository ? aws_ecr_repository.this[0].repository_url : ""
2+
value = { for repository in aws_ecr_repository.this : repository.name => repository.repository_url }
33
description = "The URL of the ECR repository, or empty if not created."
44
}
55

66
output "ecr_repository_arn" {
7-
value = var.create_ecr_repository ? aws_ecr_repository.this[0].arn : ""
7+
value = { for repository in aws_ecr_repository.this : repository.name => repository.arn }
88
description = "The ARN of the ECR repository, or empty if not created."
99
}

terraform/modules/aws-ecr/variables.tf

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
variable "name" {
2-
description = "The name of the ECR repository"
3-
type = string
4-
default = "maddevs"
5-
}
6-
7-
variable "image_tag_mutability" {
8-
description = "The tag mutability setting for the repository"
9-
type = string
10-
default = "MUTABLE"
11-
}
12-
13-
variable "scan_on_push" {
14-
description = "Enable image scanning on push"
15-
type = bool
16-
default = true
17-
}
18-
19-
variable "create_ecr_repository" {
20-
description = "Enable or not create ECR repository"
21-
type = bool
22-
default = false
1+
variable "repositories" {
2+
type = map(object({
3+
image_tag_mutability = string
4+
scan_on_push = bool
5+
lifecycle_policies = list(object({
6+
tag_status = string
7+
count_unit = string
8+
tagPrefixLists = list(string)
9+
count_number = number
10+
description = string
11+
}))
12+
}))
2313
}
2414

2515
variable "tags" {

terragrunt/ACCOUNT_ID/us-east-1/demo/aws-ecr/terragrunt.hcl

-39
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
include "root" {
2+
path = find_in_parent_folders()
3+
expose = true
4+
}
5+
6+
include "env" {
7+
path = find_in_parent_folders("env.hcl")
8+
expose = true
9+
}
10+
11+
generate "providers_versions" {
12+
path = "versions.tf"
13+
if_exists = "overwrite"
14+
contents = <<EOF
15+
terraform {
16+
required_version = ">= 1.8.3"
17+
18+
required_providers {
19+
aws = {
20+
source = "hashicorp/aws"
21+
version = "${include.root.locals.tf_providers.aws}"
22+
}
23+
}
24+
}
25+
EOF
26+
}
27+
28+
terraform {
29+
source = "${get_path_to_repo_root()}/terraform//modules/aws-ecr"
30+
}
31+
32+
inputs = {
33+
repositories = {
34+
"${include.env.locals.name}" = {
35+
image_tag_mutability = "MUTABLE"
36+
scan_on_push = true
37+
38+
lifecycle_policies = [
39+
{
40+
tag_status = "tagged"
41+
count_unit = "days"
42+
count_number = 7
43+
tagPrefixLists = ["${include.env.locals.name}"]
44+
description = "Keep image for 24 hours"
45+
},
46+
{
47+
tag_status = "untagged"
48+
count_unit = "days"
49+
count_number = 1
50+
tagPrefixLists = []
51+
description = "Keep image for 7 days"
52+
}
53+
]
54+
}
55+
}
56+
}

terragrunt/ACCOUNT_ID/us-east-1/demo/k8s-addons/.terraform.lock.hcl

-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terragrunt/ACCOUNT_ID/us-east-1/demo/k8s-addons/terragrunt.hcl

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ dependency "aws-r53" {
5252
}
5353

5454
dependencies {
55-
paths = ["../karpenter", "../aws-ecr"]
55+
paths = ["../karpenter"]
5656
}
5757

5858
generate "providers_versions" {

0 commit comments

Comments
 (0)