forked from cloudposse/terraform-aws-ecr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
executable file
·168 lines (145 loc) · 4.81 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
locals {
principals_readonly_access_non_empty = length(var.principals_readonly_access) > 0 ? true : false
principals_full_access_non_empty = length(var.principals_full_access) > 0 ? true : false
ecr_need_policy = length(var.principals_full_access) + length(var.principals_readonly_access) > 0 ? true : false
}
module "label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0"
enabled = var.enabled
namespace = var.namespace
stage = var.stage
environment = var.environment
name = var.name
delimiter = var.delimiter
attributes = var.attributes
tags = var.tags
regex_replace_chars = var.regex_replace_chars
}
locals {
_name = var.use_fullname ? module.label.id : module.label.name
image_names = length(var.image_names) > 0 ? var.image_names : [local._name]
}
resource "aws_ecr_repository" "name" {
for_each = toset(var.enabled ? local.image_names : [])
name = each.value
image_tag_mutability = var.image_tag_mutability
image_scanning_configuration {
scan_on_push = var.scan_images_on_push
}
tags = module.label.tags
}
locals {
untagged_image_rule = [{
rulePriority = length(var.protected_tags) + 1
description = "Remove untagged images"
selection = {
tagStatus = "untagged"
countType = "imageCountMoreThan"
countNumber = 1
}
action = {
type = "expire"
}
}]
remove_old_image_rule = [{
rulePriority = length(var.protected_tags) + 2
description = "Rotate images when reach ${var.max_image_count} images stored",
selection = {
tagStatus = "any"
countType = "imageCountMoreThan"
countNumber = var.max_image_count
}
action = {
type = "expire"
}
}]
protected_tag_rules = [
for index, tagPrefix in zipmap(range(length(var.protected_tags)), tolist(var.protected_tags)) :
{
rulePriority = tonumber(index) + 1
description = "Protects images tagged with ${tagPrefix}"
selection = {
tagStatus = "tagged"
tagPrefixList = [tagPrefix]
countType = "imageCountMoreThan"
countNumber = 999999
}
action = {
type = "expire"
}
}
]
}
resource "aws_ecr_lifecycle_policy" "name" {
for_each = toset(var.enabled && var.enable_lifecycle_policy ? local.image_names : [])
repository = aws_ecr_repository.name[each.value].name
policy = jsonencode({
rules = concat(local.protected_tag_rules, local.untagged_image_rule, local.remove_old_image_rule)
})
}
data "aws_iam_policy_document" "empty" {
count = var.enabled ? 1 : 0
}
data "aws_iam_policy_document" "resource_readonly_access" {
count = var.enabled ? 1 : 0
statement {
sid = "ReadonlyAccess"
effect = "Allow"
principals {
type = "AWS"
identifiers = var.principals_readonly_access
}
actions = [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:DescribeImages",
"ecr:BatchGetImage",
"ecr:DescribeImageScanFindings",
]
}
}
data "aws_iam_policy_document" "resource_full_access" {
count = var.enabled ? 1 : 0
statement {
sid = "FullAccess"
effect = "Allow"
principals {
type = "AWS"
identifiers = var.principals_full_access
}
actions = [
"ecr:GetAuthorizationToken",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
"ecr:PutImage",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:DescribeImages",
"ecr:BatchGetImage",
"ecr:DescribeImageScanFindings",
"ecr:StartImageScan",
"ecr:BatchDeleteImage",
"ecr:SetRepositoryPolicy",
"ecr:DeleteRepositoryPolicy",
"ecr:DeleteRepository",
]
}
}
data "aws_iam_policy_document" "resource" {
count = var.enabled ? 1 : 0
source_json = local.principals_readonly_access_non_empty ? join("", [data.aws_iam_policy_document.resource_readonly_access[0].json]) : join("", [data.aws_iam_policy_document.empty[0].json])
override_json = local.principals_full_access_non_empty ? join("", [data.aws_iam_policy_document.resource_full_access[0].json]) : join("", [data.aws_iam_policy_document.empty[0].json])
}
resource "aws_ecr_repository_policy" "name" {
for_each = toset(local.ecr_need_policy && var.enabled ? local.image_names : [])
repository = aws_ecr_repository.name[each.value].name
policy = join("", data.aws_iam_policy_document.resource.*.json)
}