-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
84 lines (75 loc) · 2.03 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
resource "aws_ecr_repository" "this" {
count = var.enabled ? 1 : 0
name = var.name
force_delete = var.force_delete
image_tag_mutability = var.image_tag_mutability
encryption_configuration {
encryption_type = var.encryption_type
kms_key = var.kms_key
}
image_scanning_configuration {
scan_on_push = var.image_scan_on_push
}
tags = var.tags
}
resource "aws_ecr_repository_policy" "this" {
count = ((length(var.push_arns) > 0 || length(var.pull_arns) > 0) && var.enabled) ? 1 : 0
policy = data.aws_iam_policy_document.this.json
repository = aws_ecr_repository.this[0].name
}
data "aws_iam_policy_document" "this" {
override_policy_documents = var.ecr_policy
dynamic "statement" {
for_each = length(var.pull_arns) > 0 ? [""] : []
content {
actions = [
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"ecr:BatchCheckLayerAvailability",
]
principals {
type = "AWS"
identifiers = var.pull_arns
}
}
}
dynamic "statement" {
for_each = length(var.push_arns) > 0 ? [""] : []
content {
actions = [
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"ecr:BatchCheckLayerAvailability",
"ecr:PutImage",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
"ecr:GetAuthorizationToken"
]
principals {
type = "AWS"
identifiers = var.push_arns
}
}
}
}
resource "aws_ecr_lifecycle_policy" "default" {
count = var.enabled ? 1 : 0
repository = aws_ecr_repository.this[0].name
policy = jsonencode({
rules = [
{
rulePriority = 1
description = "Keep last ${var.max_any_image_count} images"
selection = {
tagStatus = "any"
countType = "imageCountMoreThan"
countNumber = var.max_any_image_count
}
action = {
type = "expire"
}
},
]
})
}