-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecr.tf
73 lines (65 loc) · 1.69 KB
/
ecr.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
locals {
push_account_arn_list = formatlist("arn:aws:iam::%s:root", var.push_account_list)
pull_account_arn_list = formatlist("arn:aws:iam::%s:root", var.pull_account_list)
}
resource "aws_ecr_repository" "repos" {
for_each = var.repository_list
name = each.value
}
resource "aws_ecr_lifecycle_policy" "lifecycle_policy" {
for_each = var.repository_list
repository = each.value
depends_on = [aws_ecr_repository.repos]
policy = jsonencode({
rules = [
{
rulePriority = 1
description = "Keep last ${var.images_to_keep} images with `any` tag"
selection = {
tagStatus = "any"
countType = "imageCountMoreThan"
countNumber = var.images_to_keep
}
action = {
type = "expire"
}
}
]
})
}
data "aws_iam_policy_document" "policy_document" {
statement {
sid = "AllowCrossAccountPull"
effect = "Allow"
principals {
type = "AWS"
identifiers = local.pull_account_arn_list
}
actions = [
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:BatchGetImage",
"ecr:BatchCheckLayerAvailability"
]
}
statement {
sid = "AllowCrossAccountPush"
effect = "Allow"
principals {
type = "AWS"
identifiers = local.push_account_arn_list
}
actions = [
"ecr:PutImage",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload"
]
}
}
resource "aws_ecr_repository_policy" "policy" {
for_each = var.repository_list
repository = each.value
depends_on = [aws_ecr_repository.repos]
policy = data.aws_iam_policy_document.policy_document.json
}