Skip to content

Commit 0a87adf

Browse files
authored
feat: Pull Through Cache for ECR (#15)
* pull through cache for ECR * Add pull through cache configuration for Docker Hub
1 parent 33b297f commit 0a87adf

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ aws-assumed-role/
3636
**/.module/
3737
**/.helmfile/
3838

39+
# We pull account-map from a remote repository in Github Actions
40+
# However, we want to locally to validate tflint
41+
account-map/
3942

4043
# Draft or auto-saved version
4144
# Note that the leading "**/" appears necessary for Docker even if not for Git

src/README.md

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.tf

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
locals {
2+
enabled = module.this.enabled
3+
}
4+
15
module "full_access" {
26
source = "../account-map/modules/roles-to-principals"
37

@@ -15,7 +19,7 @@ module "readonly_access" {
1519
}
1620

1721
locals {
18-
ecr_user_arn = join("", aws_iam_user.ecr.*.arn)
22+
ecr_user_arn = join("", aws_iam_user.ecr[*].arn)
1923
}
2024

2125
module "ecr" {
@@ -35,3 +39,21 @@ module "ecr" {
3539

3640
context = module.this.context
3741
}
42+
43+
data "aws_secretsmanager_secret" "cache_credentials" {
44+
for_each = local.enabled ? {
45+
for key, rule in var.pull_through_cache_rules :
46+
key => rule.secret
47+
if length(rule.secret) > 0
48+
} : {}
49+
50+
name = each.value
51+
}
52+
53+
resource "aws_ecr_pull_through_cache_rule" "this" {
54+
for_each = local.enabled ? var.pull_through_cache_rules : {}
55+
56+
ecr_repository_prefix = each.key
57+
upstream_registry_url = each.value.registry
58+
credential_arn = length(each.value.secret) > 0 ? data.aws_secretsmanager_secret.cache_credentials[each.key].arn : null
59+
}

src/outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ output "ecr_repo_url_map" {
1414
}
1515

1616
output "ecr_user_name" {
17-
value = join("", aws_iam_user.ecr.*.name)
17+
value = join("", aws_iam_user.ecr[*].name)
1818
description = "ECR user name"
1919
}
2020

2121
output "ecr_user_arn" {
22-
value = join("", aws_iam_user.ecr.*.arn)
22+
value = join("", aws_iam_user.ecr[*].arn)
2323
description = "ECR user ARN"
2424
}
2525

2626
output "ecr_user_unique_id" {
27-
value = join("", aws_iam_user.ecr.*.unique_id)
27+
value = join("", aws_iam_user.ecr[*].unique_id)
2828
description = "ECR user unique ID assigned by AWS"
2929
}

src/variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,12 @@ variable "principals_lambda" {
5858
description = "Principal account IDs of Lambdas allowed to consume ECR"
5959
default = []
6060
}
61+
62+
variable "pull_through_cache_rules" {
63+
type = map(object({
64+
registry = string
65+
secret = optional(string, "")
66+
}))
67+
description = "Map of pull through cache rules to configure"
68+
default = {}
69+
}

0 commit comments

Comments
 (0)