-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
83 lines (71 loc) · 2.59 KB
/
main.tf
File metadata and controls
83 lines (71 loc) · 2.59 KB
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
locals {
account_id = data.aws_caller_identity.current.account_id
region = var.region != "" ? var.region : data.aws_region.current.name
default_lifecycle_policy = {
rules = [
{
rulePriority = 1
description = "Keep last 3 images"
selection = {
tagStatus = "any"
countType = "imageCountMoreThan"
countNumber = 3
}
action = {
type = "expire"
}
}
]
}
repository_read_access_arns = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"]
}
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
data "aws_partition" "current" {}
module "pull_through_cache_repository_template" {
for_each = var.registries
source = "terraform-aws-modules/ecr/aws//modules/repository-template"
version = "2.3.1"
# Template
description = "Pull through cache repository template for ${each.key} artifacts"
prefix = each.key
resource_tags = var.tags
repository_read_access_arns = concat(coalesce(lookup(each.value, "repository_read_access_arns", []), []), local.repository_read_access_arns)
image_tag_mutability = coalesce(lookup(each.value, "image_tag_mutability", "MUTABLE"), "MUTABLE")
lifecycle_policy = jsonencode(coalesce(lookup(each.value, "lifecycle_policy", local.default_lifecycle_policy), local.default_lifecycle_policy))
# Pull through cache rule
create_pull_through_cache_rule = true
upstream_registry_url = each.value.registry
credential_arn = contains(keys(module.secrets_manager_credentials), each.key) ? module.secrets_manager_credentials[each.key].secret_arn : null
tags = var.tags
}
module "secrets_manager_credentials" {
for_each = {
for key, registry in var.registries : key => registry
if registry.username != null && registry.accessToken != null
}
source = "terraform-aws-modules/secrets-manager/aws"
version = "1.3.1"
name_prefix = "ecr-pullthroughcache/${each.key}"
description = "${each.key} credentials"
recovery_window_in_days = 0
secret_string = jsonencode({
username = each.value.username
accessToken = each.value.accessToken
})
# Policy
create_policy = true
block_public_policy = true
policy_statements = {
read = {
sid = "AllowAccountRead"
principals = [{
type = "AWS"
identifiers = ["arn:${data.aws_partition.current.partition}:iam::${local.account_id}:root"]
}]
actions = ["secretsmanager:GetSecretValue"]
resources = ["*"]
}
}
tags = var.tags
}