-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.tf
111 lines (94 loc) · 2.87 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
locals {
metadata = {
package = "terraform-aws-container"
version = trimspace(file("${path.module}/../../VERSION"))
module = basename(path.module)
name = var.name
}
module_tags = var.module_tags_enabled ? {
"module.terraform.io/package" = local.metadata.package
"module.terraform.io/version" = local.metadata.version
"module.terraform.io/name" = local.metadata.module
"module.terraform.io/full-name" = "${local.metadata.package}/${local.metadata.module}"
"module.terraform.io/instance" = local.metadata.name
} : {}
}
###################################################
# ECR Repository
###################################################
resource "aws_ecr_repository" "this" {
name = local.metadata.name
force_delete = var.force_delete
image_tag_mutability = var.image_tag_immutable_enabled ? "IMMUTABLE" : "MUTABLE"
image_scanning_configuration {
scan_on_push = var.image_scan_on_push_enabled
}
encryption_configuration {
encryption_type = var.encryption.type
kms_key = var.encryption.kms_key
}
tags = merge(
{
"Name" = local.metadata.name
},
local.module_tags,
var.tags,
)
}
###################################################
# Repository Policy
###################################################
resource "aws_ecr_repository_policy" "this" {
count = length(var.policy) > 0 ? 1 : 0
repository = aws_ecr_repository.this.name
policy = var.policy
}
###################################################
# Lifecycle Policy
###################################################
locals {
lifecycle_rules = [
for rule in var.lifecycle_rules : {
rulePriority = rule.priority
description = rule.description
selection = {
for k, v in {
tagStatus = rule.target.status
tagPatternList = (rule.target.status == "tagged" && length(rule.target.tag_patterns) > 0
? rule.target.tag_patterns
: null
)
tagPrefixList = (rule.target.status == "tagged" && length(rule.target.tag_prefixes) > 0
? rule.target.tag_prefixes
: null
)
countType = (rule.expiration.count != null
? "imageCountMoreThan"
: "sinceImagePushed"
)
countUnit = (rule.expiration.count != null
? null
: "days"
)
countNumber = (rule.expiration.count != null
? rule.expiration.count
: rule.expiration.days
)
} :
k => v
if v != null
}
action = {
type = "expire"
}
}
]
lifecycle_policy = jsonencode({
rules = local.lifecycle_rules
})
}
resource "aws_ecr_lifecycle_policy" "this" {
count = length(local.lifecycle_policy) >= 100 ? 1 : 0
repository = aws_ecr_repository.this.name
policy = local.lifecycle_policy
}